Struts2無重新整理實現登陸退出操作
阿新 • • 發佈:2018-12-26
運用的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>