struts2 拦截器拦截action中指定方法
1.继承类MethodFilterInterceptor(此类是类AbstractInterceptor的子类) import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; /* *拦截指定方法 */ public class MyFilterInterceptor extends MethodFilterInterceptor{ private static final long serialVersionUID = 1L; private String name; public void setName(String name) { this.name = name; } @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //取得请求相关的ActionContext实例 ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); //取出名为user的Session属性 String user = (String)session.get("user"); //如果没有登陆,或者登陆所用的用户名不是scott,都返回重新登陆 if (user != null && user.equals("scott") ) { return invocation.invoke(); } //没有登陆,将服务器提示设置成一个HttpServletRequest属性 ctx.put("tip" , "您还没有登陆,请输入scott,tiger登陆系统"); //直接返回login的逻辑视图 return Action.LOGIN; } } 2.struts.xml配置 <package name="site" extends="struts-default" namespace="/site"> <interceptors> <!-- 定义了一个名为authority的拦截器 --> <interceptor name="authority"/> <!--上面自定义的拦截器类--> <interceptor-stack name="myDefault"> <interceptor-ref name="authority"> <!-- 引用拦截器 --> <param name="includeMethods">getALL,getPart,listUser</param> <!-- 设置需要拦截的方法,多个以逗号隔开 --> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myDefault"></default-interceptor-ref> <!-- 全局 --> <global-results> <!-- 当返回login视图名时,转入/login.jsp页面 --> <result name="login">/login.jsp</result> </global-results> <action name="site"> <!--省略跳转--> </action> </package>
标签:
struts2strut2拦截器interceptor拦截器 |
分类: Struts |
配置1. 普通配置法
1: <struts>
2: <package name="struts2" extends="struts-default">
3: <interceptors>
4: <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
5: </interceptors>
6:
7: <action name="register" class="edu.hust.action.RegisterAction">
8: <result name="input">/register.jsp</result>
9: <result>/result.jsp</result>
10:
11: <!-- 在自定义interceptor并将其ref时, 系统会覆盖掉默认的interceptor-stack(defaultStack), 为了保证系统默认的defaultStack不受印象, 我们需要显式的将其引入 -->
12: <!-- 注意两个interceptor-ref的顺序, 顺序不同, 执行效果也不同: 先配置的先执行/后配置的先退出(先进后出) -->
13: <interceptor-ref name="defaultStack"></interceptor-ref>
14: <interceptor-ref name="myInterceptor"></interceptor-ref>
15: </action>
16: </package>
17: </struts>
配置2. 配置拦截器栈(即将多个interceptor串联的一种元素)。然后在<action>中引入该拦截器栈就可以了。
1: <struts>
2: <package name="struts2" extends="struts-default">
3:
4: <interceptors>
5: <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
6:
7: <interceptor-stack name="myInterceptorStack">
8: <interceptor-ref name="myInterceptor"></interceptor-ref>
9: <interceptor-ref name="defaultStack"></interceptor-ref>
10: </interceptor-stack>
11: </interceptors>
12:
13: <action name="register" class="edu.hust.action.RegisterAction">
14: <result name="input">/register.jsp</result>
15: <result>/result.jsp</result>
16:
17: <interceptor-ref name="myInterceptorStack"></interceptor-ref>
18: </action>
19: </package>
20: </struts>
21:
配置3. 修改默认拦截器,将自定义的拦截器栈定义为struts2的默认拦截器。
1: <struts>
2: <package name="struts2" extends="struts-default">
3:
4: <interceptors>
5: <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
6: <interceptor-stack name="myInterceptorStack">
7: <interceptor-ref name="myInterceptor"></interceptor-ref>
8: <interceptor-ref name="defaultStack"></interceptor-ref>
9: </interceptor-stack>
10: </interceptors>
11:
12: <!-- 此默认interceptor是针对所有action的 -->
13: <!-- 如果某个action中引入了interceptor, 则在这个action中此默认interceptor就会失效 -->
14: <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
15:
16: <action name="register" class="edu.hust.action.RegisterAction">
17: <result name="input">/register.jsp</result>
18: <result>/result.jsp</result>
19: </action>
20:
21: </package>
22: </struts>
23:
1、implements Interceptor接口-拦截角色对象
(1)拦截目标对象(被代理对象),这里目标对象就是action;
(2)拦截器(一个类,动态的将某些方法插入到目标对象的某方法的before、after);
(3)对目标对象生成的(动态)代理对象(代理对象内部方法综合了目标对象方法+拦截器方法)。程序最终执行的是目标对象的代理,而这个代理已经插入了interceptor。
拦截器作用:拦截action。interceptor相当于一个入口和出口,通过interceptor进入action,执行完action的代码再通过interceptor出去。
针对”struts2 — interceptor(Interceptor怎么写)”这篇文章的MyInterceptor.class和MyInterceptor2.class。根据下面的配置文件执行程序
1: <struts>
2: <package name="struts2" extends="struts-default">
3:
4: <interceptors>
5: <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
6: <interceptor name="myInterceptor2" class="edu.hust.interceptor.MyInterceptor2"></interceptor>
7: <interceptor-stack name="myInterceptorStack">
8: <interceptor-ref name="myInterceptor"></interceptor-ref>
9: <interceptor-ref name="myInterceptor2"></interceptor-ref>
10: <interceptor-ref name="defaultStack"></interceptor-ref>
11: </interceptor-stack>
12: </interceptors>
13:
14: <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
15:
16: <action name="register" class="edu.hust.action.RegisterAction">
17: <result name="input">/register.jsp</result>
18: <result>/result.jsp</result>
19: </action>
20:
21: </package>
22: </struts>
23:
Console会得到以下打印输出
intercept start
intercept2 start
2008-9-19 19:42:06 com.opensymphony.xwork2.validator.ActionValidatorManagerFa
信息: Detected AnnotationActionValidato
intercept2 finish
intercept finish
这个结果解释了”interceptor相当于一个入口和出口,通过interceptor进入action,执行完action的代码再通过interceptor出去”这句话。
2、Extends AbstractInterceptor抽象类
3、Extends MethodFilterInterceptor类的拦截器如何配置哪些方法该拦截、哪些方法不该拦截(针对方法拦截的配置)
1: <struts>
2: <package name="struts2" extends="struts-default">
3:
4: <interceptors>
5: <interceptor name="myInterceptor3" class="edu.hust.interceptor.MyInterceptor3"></interceptor>
6: </interceptors>
7:
8: <action name="register" class="edu.hust.action.RegisterAction" method="queryAll">
9: <result name="input">/register.jsp</result>
10: <result>/result.jsp</result>
11: <!-- myInterceptor3拦截器只对RegisterAction中的queryAll()方法和insert()方法进行了拦截, 其他方法未进行拦截 -->
12: <interceptor-ref name="myInterceptor3">
13: <param name="includeMethods">queryAll, execute</param>
14: </interceptor-ref>
15: <interceptor-ref name="defaultStack"></interceptor-ref>
16: </action>
17:
18: <action name="register" class="edu.hust.action.RegisterAction" method="insert">
19: <result name="input">/register.jsp</result>
20: <result>/result.jsp</result>
21: <interceptor-ref name="myInterceptor3">
22: <param name="includeMethods">queryAll, insert</param>
23: </interceptor-ref>
24: <interceptor-ref name="defaultStack"></interceptor-ref>
25: </action>
26:
27: <action name="register" class="edu.hust.action.RegisterAction" method="update">
28: <result name="input">/register.jsp</result>
29: <result>/result.jsp</result>
30: <interceptor-ref name="myInterceptor3">
31: <param name="includeMethods">queryAll, insert</param>
32: </interceptor-ref>
33: <interceptor-ref name="defaultStack"></interceptor-ref>
34: </action>
35:
36: </package>
37: </struts>
38:
转载请注明出处,在此前提下可任意修改.
struts2 在单独action里配置拦截器
- |
- 浏览:363
- |
- 更新:
- 原来struts2配置拦截器是现在struts.xml里配置好拦截器,例如tianqueStack:
<interceptors>
<interceptor name=”isLoginInterceptor” />
<interceptor-stack name=”tianqueStack”>
<interceptor-ref name=”isLoginInterceptor”>
<param name=”excludeMethods”>login</param>
</interceptor-ref>
<interceptor-ref name=”defaultStack” /><!– 添加这个后 会自动传参数继续–>
</interceptor-stack>
</interceptors>
- 然后在具体的userManage.xml里package标签下加入
<default-interceptor-ref name=”tianqueStack” /> 即表示整个xml的action都会被tianqueStack拦截
- 如果因为业务需要 在单个拦截器里配置 就可以把上面的tianqueStack去掉,再到具体action里加入拦截器标签,例如:
<action name=”ChangePwd” method=”changePwd”>
<interceptor-ref name=”tianqueStack” />
<result type=”json”>
<param name=”excludeNullProperties”>true</param>
</result>
</action>