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>