struts2拦截器

来源:互联网 发布:11对战平台 mac版 编辑:程序博客网 时间:2024/06/03 18:07

一、拦截器的设计思想

拦截器是Struts2中重要的组成部分,可以把Action认为是一个空的容器,通过拦截器实现了Struts2的大部分通用功能。例如params拦截器将请求参数解析,并设置成Action的属性。servlet-config拦截器将HTTP请求中的HttpServletRequest和HttpServletResponse对象传递给Action对象等。

拦截器的设计思想是当前比较流行的AOP面向方面编程思想的应用,其实现模式是代理模式和反射机制。关于代理模式和反射机制资料点击

二、如何自定义拦截器

1.自定义拦截器如何起拦截作用?

经过如下步骤,自定义拦截器在Action之前,默认拦截器之后进行拦截操作。主要就是处理一些业务逻辑
a)Web浏览器发送请求
b)首先通过一组Struts2默认的拦截栈 dispatcher (或者 ServletFilter)
c)定义interceptor(拦截器)
d)Action
e)Result

2.如何定义一个拦截器

a)创建一个Action,继承AbstractInterceptor类并且实现intercept方法。

b)在intercept中处理某些逻辑

c)配置拦截器

三、拦截器配置示例

下面讲一个小例子,在与用户登陆相关的页面,当用户登陆一段时间我们就会清空session,那么就需要用户重新登陆。admin.jsp是用户已经登陆的后台管理页面,当过一段时间之后,后台清空session。这时候就需要用户重新登陆,就会跳转到登陆页面重新登陆。(博主的例子没有添加跳转url,大家可以自行设计)

首先创建一个登陆页面login.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.   </head>  
  11.   <body>  
  12.   ${tips}  
  13.     <form id="login" name="login" action="/login.action" method="post">  
  14.         <table border="0">  
  15.             <tr>  
  16.                 <td>name:</td>  
  17.                 <td><input type="text" name="username" id="username"/></td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td>password:</td>  
  21.                 <td><input type="password" name="password" id="password"/></td>  
  22.             </tr>  
  23.             <tr>  
  24.                 <td colspan="2" align="center"><input type="submit" value="login"/></td>  
  25.             </tr>  
  26.         </table>  
  27.     </form>  
  28.   </body>  
  29. </html>  
一个登陆成功页面Success.jsp
[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.   </head>  
  12.     
  13.   <body>  
  14.     hello world!,${username}  
  15.   </body>  
  16. </html>  

后台管理页面admin.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.   </head>  
  12.     
  13.   <body>  
  14.     <a href="<%=path%>/admin.action">后台管理<a/>  
  15.   </body>  
  16. </html>  
然后定义一个登陆LoginAction,登陆成功就跳转成功页面。用户名密码都为admin
[java] view plain copy
  1. package com.study.action;  
  2.   
  3. import java.util.Map;  
  4. import com.opensymphony.xwork2.ActionContext;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class LoginAction extends ActionSupport{  
  8.     private String username;  
  9.     private String password;  
  10.     private String tips;  
  11.     public String execute() throws Exception {  
  12.         if(username.equals("admin")&&password.equals("admin")){  
  13.             ActionContext ac = ActionContext.getContext();  
  14.             Map session = ac.getSession();  
  15.             session.put("username", username);  
  16.             return SUCCESS;  
  17.         }else{  
  18.             tips = "用户名或密码错误!";  
  19.             return INPUT;  
  20.         }  
  21.     }  
  22.     public String getUsername() {  
  23.         return username;  
  24.     }  
  25.     public void setUsername(String username) {  
  26.         this.username = username;  
  27.     }  
  28.     public String getPassword() {  
  29.         return password;  
  30.     }  
  31.     public void setPassword(String password) {  
  32.         this.password = password;  
  33.     }  
  34.     public String getTips() {  
  35.         return tips;  
  36.     }  
  37.     public void setTips(String tips) {  
  38.         this.tips = tips;  
  39.     }  
  40.   
  41.   
  42. }  
定义一个后台管理AdminAction,只负责简单的页面转发
[java] view plain copy
  1. package com.study.action;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. public class AdminAction extends ActionSupport{  
  4.     public String execute() throws Exception {  
  5.         return "admin";  
  6.     }  
  7. }  

定义一个拦截器AdminInterceptor,验证管理页面是否是为admin用户,并且已经登陆

[java] view plain copy
  1. package com.study.interceptor;  
  2. import java.util.Map;  
  3. import com.opensymphony.xwork2.Action;  
  4. import com.opensymphony.xwork2.ActionContext;  
  5. import com.opensymphony.xwork2.ActionInvocation;  
  6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  7.   
  8. public class AdminInterceptor extends AbstractInterceptor{  
  9.     public String intercept(ActionInvocation ai) throws Exception {  
  10.         ActionContext ac = ai.getInvocationContext();  
  11.         Map session = ac.getSession();  
  12.         String username = (String) session.get("username");  
  13.         if(username!=null&&username.equals("admin")){  
  14.             return ai.invoke();  
  15.         }else{  
  16.             String tips = "请先使用管理员身份登录!";  
  17.             session.put("tips", tips);  
  18.             return Action.INPUT;  
  19.         }  
  20.     }  
  21. }  

最后就是配置我们的自定义拦截器,注意,重点是不要覆盖默认拦截器

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <package name="example" namespace="/" extends="struts-default">  
  5.         <interceptors>  
  6.             <interceptor name="admininterceptor" class="com.study.interceptor.AdminInterceptor"></interceptor>  
  7.         </interceptors>  
  8.         <action name="login" class="com.study.action.LoginAction">  
  9.             <result name="success">/Success.jsp</result>  
  10.             <result name="input">/login.jsp</result>  
  11.         </action>  
  12.         <action name="admin" class="com.study.action.AdminAction">  
  13.             <result name="admin">/admin.jsp</result>  
  14.             <result name="input">/login.jsp</result>  
  15.             <interceptor-ref name="defaultStack"></interceptor-ref>  
  16.             <interceptor-ref name="admininterceptor"></interceptor-ref>  
  17.         </action>  
  18.     </package>  
  19.       
  20. </struts>     
原创粉丝点击