在Struts 2中的拦截器,要实现com.opensymphony.xwork2.interceptor.Interceptor接口,在struts.xml中配置。可以用拦截器来完成调用Action业务逻辑之前的预处理或是之后的善后处理。还可以通过配置多个拦截器来满足action需求。
com.opensymphony.xwork2.interceptor.Interceptor接口, 一般不直接使用它.
com.opensymphony.xwork2.interceptor.AbstractInterceptor 抽象类(空实现了Interceptor接口)
自己写拦截器可继承这个抽象类, 重写intercept()方法.实现对某个Action的拦截
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor 抽象类(继承AbstractInterceptor)
自己写拦截器 可继承这个抽象类, 重写doIntercept()方法. 实现对Action的的某个方法的拦截.
excludeMethods(不拦截的方法),includeMethods(要拦截的方法), 多个方法名用豆号隔开
简单例子 --- 自定义拦截器
public class MyInterceptor2 extends AbstractInterceptor {
private String userName; //生成其get/set方法
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("intercept2");
String result = invocation.invoke();
return result;
}
}
//权限验证拦截器
public class AuthInterceptor extends AbstractInterceptor {
@Override
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Map map = invocation.getInvocationContext().getSession();
if (map.get("user") == null) {
return Action.LOGIN;
} else {
return invocation.invoke();
}
}
}
在struts.xml中配置自定义拦截器 --- 代码如下
<interceptors>
<interceptor name="myInterceptor" class="com.stu.interceptor.MyInterceptor"></interceptor>
<interceptor name="myInterceptor2" class="com.stu.interceptor.MyInterceptor2">
<param name="userName">Smiles</param> <!--给拦截器中参数附默认值,可以在引用时修改-->
</interceptor>
<interceptor name="myInterceptor3" class="com.stu.interceptor.MyInterceptor3"></interceptor>
<!-- 配置权限拦截器 -->
<interceptor name="auth" class="com.stu.interceptor.AuthInterceptor"></interceptor>
<!-- 配置一个拦截器栈 -->
<interceptor-stack name="myStack">
<!-- <interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="myInterceptor2"></interceptor-ref>
-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<global-results> <!-- 配置全局返回页面 -->
<result name="login" type="redirect">/login.jsp</result> <!--type=”redirect“重定向到login页面-->
</global-results>
<action name="register" class="com.stu.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
<!--<interceptor-ref name="myInterceptor3">
<param name="includeMethods">execute</param> 配置要拦截的方法
<param name="excludeMethods">execute</param> 配置不拦截的方法
</interceptor-ref>-->
<!-- <interceptor-ref name="auth"></interceptor-ref> 配置权限验证拦截器-->
<interceptor-ref name="myStack"></interceptor-ref> <!--引入拦截器栈-->
</action>
action中引用单个自定义拦截器时,要引入struts的默认拦截器<interceptor-ref name="defaultStack"></interceptor-ref>
分享到:
相关推荐
Struts2_自定义拦截器 struts2_3500_my_interceptor
JSP 开发之Struts2内建自定义拦截器.docx
struts2的自定义拦截器代码
struts2的自定义拦截器代码例子
strut2 配置自定义拦截器 包括struts2.xml 和 action对应的xml 以及拦截器 可能有点简单 但是基本配置都有
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
简单的struts2自定义拦截器 很适合web初学者
struts2的一个简单的拦截器例子
Struts2全局、局部自定义拦截器源码。
1.eclipse环境下运行 2.源码 3.自定义拦截器,对用户权限认证 4.各个action之间的跳转
struts2自定义拦截器.pdf
通过struts2来自定义一个拦截器,需要定义struts2.xml和web.xml文件,详细代码见下载。
由于struts2标签的性能不好,项目组决定不使用,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现
Struts2自定义拦截器实现防止重复提交
http://blog.csdn.net/zhiweiv/archive/2008/09/19/2954047.aspx
NULL 博文链接:https://llxhna.iteye.com/blog/1067851
struts2 用拦截器 实现用户权限登录 可以直接运行,只单单用到struts的东西。
自定义拦截器的实现以及拦截器的详细配置,配置文件里面有特别详细的注释,希望对刚接触拦截器的同学有帮助