struts2自定义拦截器

 声明自定义拦截器:

<!-- 用户功能 -->
	<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" />


转载于:https://my.oschina.net/hehongbo/blog/607299

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值