Struts2拦截器总结
来源:互联网 发布:编程原本 前言 编辑:程序博客网 时间:2024/06/05 03:34
Struts2拦截器总结:
一、编写拦截器
1、 实现接口com.opensymphony.xwork2.Intercepter(或继承com.opensymphony.xwork2.AbstractInterceptor)
2、 在interceptor方法中加入如下代码:
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("Before"); //在Action之前调用
String result = arg0.invoke(); //如果此拦截器之后还有拦截器,则调用下个拦截器的intercept方法
//如果之后没有了拦截器,则调用Action的execute方法
System.out.println("After");
return result;
}
二、在Struts.xml中配置拦截器
1、 在struts.xml中声明拦截器和拦截器Stack,拦截器Stack可以包括多个拦截器和其他Stack。
<interceptors>
<!-- 拦截器 -->
<interceptor name="MyInterceptor"class="com.test.interceptor.MyInterceptor"></interceptor>
<!-- 拦截器Stack -->
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
2、 将拦截器配置到单个Action中,只拦截此Action中的execute方法。
<action name="register" class="com.test.action.RegisterAction" method="test">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
<interceptor-ref name="MyInterceptor"></interceptor-ref>
</action>
3、 将拦截器配置到所有Action中,拦截所有Action中的execute方法。
<default-interceptor-ref name="MyInterceptor"></default-interceptor-ref>
对已经单独配置了拦截器的Action不起作用
三、拦截Action中指定的方法
1、 继承com.opensymphony.xwork2.interceptor.MethodFilterInterceptor。
2、 因为是针对某个Action的方法,所以只能配置在Action内部
<action name="register" class="com.test.action.RegisterAction" method="test">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
<interceptor-ref name="MyInterceptor">
<param name="includeMethod">test,execute</param> <!-- 拦截text和execute方法,方法间用逗号分隔 -->
<param name="excludeMethod">myfun</param> <!-- 不拦截myfun方法 -->
</interceptor-ref>
</action>
四、struts2拦截器的interceptor方法中,参数ActionInvocation可用来获取页面用户输入的信息。
public String intercept(ActionInvocation arg0) throws Exception {
Map map = arg0.getInvocationContext().getSession();
if(map.get("user") == null) {
return Action.LOGIN;
} else {
return arg0.invoke();
}
}
- Struts2拦截器总结
- Struts2 拦截器总结
- Struts2拦截器总结
- Struts2的拦截器总结
- Struts2总结之拦截器
- Struts2的拦截器总结
- Struts2的拦截器总结
- Struts2的拦截器总结
- Struts2拦截器总结学习
- struts2 拦截器: 自己总结 经典~~~
- struts2第五天总结(拦截器)
- struts2要点总结 通配符、OGNL、拦截器
- struts2要点总结 通配符、OGNL、拦截器
- Struts2权威指南总结7:拦截器
- struts2复习(五)拦截器总结
- Struts2 拦截器的使用总结
- struts2 学习总结 ---- 拦截器(interceptor)
- 【转载】struts2 学习总结 ---- 拦截器(interceptor)
- JDK中的URLConnection参数详解
- stagefright框架(二)- 和OpenMAX的運作
- 矩阵特征值的理解
- 构造函数练习
- HTML CSS 编码规范
- Struts2拦截器总结
- 注意内存越界
- apk 反编译以及反编译后重新生成apk --apktool工具的使用
- SQL语法、语句
- ASIHTTPRequest 详解
- Hibernate 另一个作业:Org树状结构设计
- asp.net邮箱源码
- instanceof
- 了解 XA 事务