您的位置:首页 > 编程语言 > Java开发

Struts2无刷新实现登陆退出操作

2015-09-05 16:42 381 查看
运用的jar包:

commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
log4j-1.2.17.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
struts2-dojo-plugin-2.1.8.1.jar
xwork-2.1.2.jar


源代码:

Struts2Test.java

package com.test;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Struts2Test extends ActionSupport{
	
	private String userName;
	private String password;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return "success";
	}
	
	public String login() {
		// TODO Auto-generated method stub
		if(userName.equals("admin")&&password.equals("123456")){
			ServletActionContext.getRequest().getSession().setAttribute("userName",userName);
			ServletActionContext.getRequest().setAttribute("log", "logIn");
		}else{
			ServletActionContext.getRequest().getSession().removeAttribute("userName");
			ServletActionContext.getRequest().setAttribute("log", "failed");
		}
		return SUCCESS;
	}
	
	public String out() {
		// TODO Auto-generated method stub
		ServletActionContext.getRequest().getSession().removeAttribute("userName");
		ServletActionContext.getRequest().setAttribute("log", "out");		
		return SUCCESS;
	}
}


struts.xml源代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default" namespace="/">
	<action name="log" class="com.test.Struts2Test" >
		<result name="success">/success.jsp</result>
	</action>
</package> 
</struts>


web.xml源代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter> 
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>


index.jsp源代码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>      
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
  	<s:head/>
	<sx:head parseContent="true"/>
  </head>
<body>
	<s:form action="#" id="formDiv" name="formDiv">
		<div id="errorMessage"></div>
		<s:textfield id="userName" name="userName" label="用户名"></s:textfield>
		<s:password id="password" name="password" label="密码"></s:password>
		<!-- s:url标签必须放在sx:submit标签前边,否则无法使用 -->
		<s:url action="log!login" id="loginAction"></s:url>
		<!-- %{}是OGNL表达式的写法 -->
		<sx:submit value="提交" href="%{loginAction}" targets="successDiv" executeScripts="true"></sx:submit>
		<!-- targets="successDiv":意思是连接到id为successDiv的元素 -->
		<!-- executeScripts="true":目的是使连接到的页面的js也顺带执行 -->
	</s:form>
	<s:div id="successDiv"></s:div>
</body>
</html>


success.jsp源代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<s:head/>
  	<sx:head parseContent="true"/>
    <base href="<%=basePath%>">
    <title>SUCCESS</title>
    <s:if test="%{#request.log=='logIn'}">
    	<script type="text/javascript">
	  		document.getElementById("errorMessage").innerHTML="";
	  		document.getElementById("formDiv").style.display="none";
	  		document.getElementById("successDiv").style.display="";
  		</script>
    </s:if>
    <s:if test="%{#request.log=='failed'}">
    	<script type="text/javascript">
   			document.getElementById("errorMessage").innerHTML="用户名或密码错误!";
  	    </script>
    </s:if>
    <s:elseif test="%{#request.log=='out'}">
	    <script type="text/javascript">
		    document.getElementById('formDiv').style.display='';
		    document.getElementById("successDiv").style.display="none";
	    </script>
    </s:elseif>
  </head>
  <body>
  	<s:property value="userName"/>,欢迎你!
  	<s:url action="log!out" id="logOut"></s:url>
  	<sx:a href="%{#logOut}" executeScripts="true">退出</sx:a>
  </body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: