声明自定义拦截器:
<!-- 用户功能 --> <package name="user" extends="struts-default"> <interceptors> <!-- 声明自定义拦截器 --> <interceptor name="userAuthority" class="com.sichang.authority.AuthorityInterceptor" /> <!-- 声明自定义拦截器栈 --> <interceptor-stack name="myStack"> <!-- 配置自定义拦截器栈 --> <interceptor-ref name="userAuthority" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 --> <default-interceptor-ref name="myStack" /> <!-- 根据设备IMSI查询设备ID --> <action name="Login" class="com.sichang.action.user.LoginAction" method="Login"> <result>/manage/homePage.jsp</result> </action> </package>
自定义拦截器类:AuthorityInterceptor
package com.sichang.authority; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; @SuppressWarnings("serial") public class AuthorityInterceptor implements Interceptor { public void init() { // TODO Auto-generated method stub System.out.println("AuthorityInterceptor----------------- init()"); } public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub System.out.println("AuthorityInterceptor----------------- intercept()"); // com.sichang.action.user.LoginAction@1288131, 动作类的对象 System.out.println("invocation.getAction():"+invocation.getAction()); // com.sichang.action.user.LoginAction@1288131,与invocation.getAction()方法获取的是同一的对象 System.out.println("invocation.getProxy().getAction():"+invocation.getProxy().getAction()); // Login,自定义配置文件中的action标签的name属性的值 System.out.println("invocation.getProxy().getActionName():"+invocation.getProxy().getActionName()); // Login,对象动作类指定要执行的方法名称 System.out.println("invocation.getProxy().getMethod():"+invocation.getProxy().getMethod()); // 自定义配置文件中的package标签的namespace属性的值,缺省为空 System.out.println("invocation.getProxy().getNamespace():"+invocation.getProxy().getNamespace()); // null,返回结果 System.out.println("invocation.getResult():"+invocation.getResult()); return null; } public void destroy() { // TODO Auto-generated method stub System.out.println("AuthorityInterceptor----------------- destroy()"); } }
如何自定义拦截器:
* 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类
* 要重写init()、intercept()、destroy()方法
* init()是在struts2框架运行时执行,在拦截器的生命周期中只执行一次,可以做必要的内容的初始化工作
* intercept(),是每一次请求就执行一次,做相关处理工作。
* intercept()方法接收一个ActionInvocation接口的实例
* 通过这个接口的实例,可以获取以下内容
:
// com.sichang.action.user.LoginAction@1288131, 动作类的对象
System.out.println("invocation.getAction():"+invocation.getAction());
// com.sichang.action.user.LoginAction@1288131,与invocation.getAction()方法获取的是同一的对象
System.out.println("invocation.getProxy().getAction():"+invocation.getProxy().getAction());
// Login,自定义配置文件中的action标签的name属性的值
System.out.println("invocation.getProxy().getActionName():"+invocation.getProxy().getActionName());
// Login,对象动作类指定要执行的方法名称
System.out.println("invocation.getProxy().getMethod():"+invocation.getProxy().getMethod());
// 自定义配置文件中的package标签的namespace属性的值,缺省为空
System.out.println("invocation.getProxy().getNamespace():"+invocation.getProxy().getNamespace());
// null,返回结果
System.out.println("invocation.getResult():"+invocation.getResult());
* destroy()是在拦截器销毁前执行,在拦截器的声明周期中只执行一次。
* 在struts.xml配置文件中,进行注册
* 在配置文件中的package标签下,进行相关配置:
<interceptors>
<!-- 声明自定义拦截器 -->
<interceptor name="userAuthority" class="com.sichang.authority.AuthorityInterceptor" />
<!-- 声明自定义拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 配置自定义拦截器栈 -->
<interceptor-ref name="userAuthority" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->
<default-interceptor-ref name="myStack" />