12 struts2拦截器
2014-10-09 10:31
337 查看
自己写拦截器, 要认识下面接口 与 类
com.opensymphony.xwork2.interceptor.Interceptor
接口
这是最重要的接口, 一般不直接使用它.
com.opensymphony.xwork2.interceptor.AbstractInterceptor
抽象类(空实现了Interceptor接口)
自己写拦截器 可继承这个抽象类, 重写intercept()方法. 实现对某个Action的拦截
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
抽象
类 (继承AbstractInterceptor)
自己写拦截器 可继承这个抽象类, 重写doIntercept()方法. 实现对某个Action的的某个方法的拦截.
excludeMethods(排除哪方法),includeMethods(拦截哪些方法), 多个方法名用豆号隔开.
-----------------------------------
自定义拦截器 :MyInterceptor
struts.xml
----------------------------------------------------------------------
在struts2-core-2.1.2.jar 包中 , 根目录有一个struts-default.xml 定义默认拦截器: <default-interceptor-ref name="defaultStack"/>
内有大量的拦截器, 我们要扩展, 比如 :
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
---------------------------------------------------------------------
附件中有 eclipse3.3 工程源码, 写的有点乱, 对付看吧.
struts2.rar (2.1 MB)
下载次数: 61
com.opensymphony.xwork2.interceptor.Interceptor
接口
这是最重要的接口, 一般不直接使用它.
com.opensymphony.xwork2.interceptor.AbstractInterceptor
抽象类(空实现了Interceptor接口)
自己写拦截器 可继承这个抽象类, 重写intercept()方法. 实现对某个Action的拦截
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
抽象
类 (继承AbstractInterceptor)
自己写拦截器 可继承这个抽象类, 重写doIntercept()方法. 实现对某个Action的的某个方法的拦截.
excludeMethods(排除哪方法),includeMethods(拦截哪些方法), 多个方法名用豆号隔开.
<interceptor-ref name="myInterceptor3"> <param name="excludeMethods">test,execute</param> <param name="includeMethods">test</param> </interceptor-ref>
-----------------------------------
自定义拦截器 :MyInterceptor
package ssh.org.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; /** * 自定义拦截器,实现Interceptor接口 * * 要是改为继承 AbstractInterceptor 抽象类, 下面destroy(),init()方法就这用写了 */ public class MyInterceptor implements Interceptor { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(final ActionInvocation actioninvocation) throws Exception { System.out.println("方法执行前插入 代码"); //执行目标方法 (调用下一个拦截器, 或执行Action) final String res = actioninvocation.invoke(); System.out.println("方法执行后插入 代码"); return res; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <interceptors> <!-- 配置自己的拦截器 --> <interceptor name="myInterception" class="ssh.org.interceptor.MyInterceptor"/> <!-- 配置自己的拦截器栈 --> <interceptor-stack name="myInterceptionStack"> <!-- 加入默认的拦截器栈 --> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterception"></interceptor-ref> </interceptor-stack> </interceptors> <!--用户 --> <action name="user_*" class="ssh.org.web.UserAction" method="{1}"> <result name="success">/user/success.jsp</result> <result name="input">/user/user.jsp</result> <!-- 应用我自己的拦截器 --> <interceptor-ref name="myInterceptionStack"/> </action> </package> </struts>
----------------------------------------------------------------------
在struts2-core-2.1.2.jar 包中 , 根目录有一个struts-default.xml 定义默认拦截器: <default-interceptor-ref name="defaultStack"/>
内有大量的拦截器, 我们要扩展, 比如 :
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
---------------------------------------------------------------------
附件中有 eclipse3.3 工程源码, 写的有点乱, 对付看吧.
struts2.rar (2.1 MB)
下载次数: 61
相关文章推荐
- Struts2学习笔记12:Struts2的拦截器【续】
- Struts2系列:(12)拦截器
- Struts2笔记――12.拦截器机制
- .Java程序员从笨鸟到菜鸟之(四十七)细谈struts2(九)内置拦截器和自定义拦截器详解(附源码) 分类: 学习专区 框架Struts2 Java程序员从笨鸟到菜鸟 2012-05-08 12:
- 12.Struts2自定义拦截器
- Struts2使用12 -- 拦截器2 -- 配置
- 7、struts2 案例( 模型驱动、 防止表单重复提交--拦截器 、数据回显 、值栈 、 OGNL表达式综合运用)
- Struts2——"token"拦截器(防止表单重复提交)
- 《Java从入门到放弃》框架入门篇:Struts2的拦截器基本语法
- struts2 使用拦截器 实现用户权限的验证
- 在Struts2下使用[拦截器]配合[session监听器]实现在线会员统计与防止重复登录
- Struts2的整体回顾(Action, 拦截器, 值栈, OGNL表示式, ModelDriven)
- 《Java从入门到放弃》入门篇:Struts2的拦截器基本语法
- Struts2中的拦截器与过滤器学习
- struts2拦截器的问题
- Struts2实现第一个简单拦截器
- Struts2自定义拦截器实例—Session超时的处理
- struts2拦截器不执行问题
- struts2默认拦截器(Interceptor)讲解(八)
- Struts2拦截器总结