Controller.UserAction.javacss
package Controller; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class UserAction { private String username; private String pass; //|POST 外部注入 UserLog方法! public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } //|登陸頁 public String UserLogin() { return "success"; } //|登陸結果頁 public String UserLog() { System.out.println(this.pass); System.out.println(this.username); if(this.username.equals("admin")&&this.pass.equals("admin")) { ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); sessionMap.put("username", "admin"); return "success"; } return "failure"; } //|用戶中心頁面 public String UserCenter() { return "success"; } //|用戶註銷 public String UserLogOut() { ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); //|清空session(註銷) sessionMap.clear(); return "success"; } }
package Controller; /******* * 自定義攔截器 攔截 沒有登陸的用戶 Author:邱於涵 * ******/ import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; /*AbstractClass Use extends ,Interface Use implements *AbstractInterceptor繼承於MethodFilterInterceptor *使用MethodFilterInterceptor才能設置<param name="excludeMethods" >method1,...</param> *實現doIntercept方法 */ public class UserInterceptor extends MethodFilterInterceptor{ @Override protected String doIntercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); System.out.println("test攔截器 正在過濾..."); System.out.println(sessionMap.get("username")); if(sessionMap.get("username")!=null) //|session有值 就 執行下一步 invocation.invoke(); //|不然就返回 failure做爲 result 根據struts.xml返回<result name="failure">/User/UserLogin.jsp</result> return "failure"; } }
struts.xml
html
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="User" extends="struts-default" namespace="/User"> <!-- 註冊攔截器 --> <interceptors> <interceptor name="test" class="Controller.UserInterceptor"> </interceptor> </interceptors> <!-- -通配符映射 --> <action name="User*" class="Controller.UserAction" method="User{1}"> <!-- 引用攔截器 --> <interceptor-ref name="test"> <!-- 不攔截的方法 UserLogi用於登陸 --> <param name="excludeMethods">UserLogin,UserLog</param> </interceptor-ref> <!-- 沒有默認的攔截器棧 Action沒法接收POST GET --> <interceptor-ref name="defaultStack"/> <result name="success">/User/User{1}.jsp</result> <result name="failure">/User/UserLogin.jsp</result> </action> </package> </struts>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> This is WebApp Directory:<br/> <%=request.getContextPath() %><br/> <a href="User/UserLogin.action">登陸用戶</a> <br/> Powered By 涵涵 </body> </html>User/UserLogin.jsp用戶登陸表單頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>用戶登陸</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 這是通配符映射<a href="<%=request.getContextPath() %>">返回到首頁</a><br/> <form action="<%=request.getContextPath() %>/User/UserLog.action" method="POST" > <input type="text" name="username"/> <input type="password" name="pass"/> <input type="submit" value="登陸"/> </form> </body> </html>
User/UserLog.jsp用戶登陸結果頁面java
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-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>用戶登陸結果頁</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:debug></s:debug> 登陸成功!<br/> <s:property value="#session.username"/><br/> <a href="<%=request.getContextPath() %>/User/UserCenter.action">進入用戶中心</a> </body> </html>
User/UserCenter.jsp用戶中心頁面web
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-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>用戶中心</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 這是用戶中心<br/> 用戶名:<s:property value="#session.username"/><br/> <a href="<%=request.getContextPath()%>/User/UserLogOut.action">註銷</a> </body> </html>
User/UserLogOut.jsp用戶註銷頁面apache
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>註銷成功</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 註銷成功歡迎下次回來<br/> <a href="<%=request.getContextPath() %>">返回首頁</a> </body> </html>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <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>