struts2自定义拦截器
整个过程是:
通过user.jsp登陆,就是在session中添加(user,name)使用的命令是:request.getSession().setAttribute("user", "du");然后登到MyDefInterceptor,则进入到hello.jsp,通过quit.jsp退出,即request.getSession().removeAttribute("user");
拦截器PermissionInterceptor的代码
1 package cn.itcast.interceptor; 2 3 import org.xml.sax.SAXException; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionInvocation; 7 import com.opensymphony.xwork2.interceptor.Interceptor; 8 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.State; 9 10 //自定义的拦截器 11 12 public class PermissionInterceptor implements Interceptor { 13 14 @Override 15 public void destroy() { 16 // TODO Auto-generated method stub 17 18 } 19 20 @Override 21 public void init() { 22 // TODO Auto-generated method stub 23 24 } 25 26 @Override 27 public String intercept(ActionInvocation invocation) throws Exception { 28 Object user=ActionContext.getContext().getSession().get("user"); 29 System.out.println(user); 30 if(user!=null) return invocation.invoke();//如果user不为null,代表用户已经登录 31 32 ActionContext.getContext().put("message", "你没有该权限"); 33 return "success"; 34 } 35 36 }
使用该拦截器的Action(MyDefInterceptorAction)代码:
package cn.itcast.action; //使用自定义拦截器PermissionInterceptor的action public class MyDefInterceptorAction { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute(){ this.message="execute"; 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> <package name="itcast" namespace ="/test" extends="struts-default"> <interceptors> <interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor" /> <interceptor-stack name="permissionstack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="permission"/> </interceptor-stack> </interceptors> <global-results> <result name="success">/WEB-INF/page/hello.jsp</result> </global-results> <action name="MyDefInterceptor" class="cn.itcast.action.MyDefInterceptorAction" method="execute"> <interceptor-ref name="permissionstack" /> </action> </package> </struts>
登陆网页user.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.getSession().setAttribute("user", "du"); %> 用户已登录
退出网页quit.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.getSession().removeAttribute("user"); %> 退出
/WEB-INF/page/hello.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>My JSP 'hello.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> ${message} </body> </html>