Struts2学习笔记五(拦截器)

1、拦截器类似于servlet中的Filter。Interceptor1 before----- Interceptor2 before------execute--Interceptor2 after---Interceptor1 after

2、自定义拦截器,实现Interceptor接口,或者定义AbstractInterceptor。

  public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("before");
		System.out
		.println("interceptor1: " + invocation.getAction().getClass());
		String result = invocation.invoke();
		System.out.println("after");
		return result;
	}

3、在struts.xml中定义

		<interceptors>
			<span style="background-color: rgb(255, 0, 0);"><interceptor name="theIntercepter1"
				class="com.yanlei.interceptor.TheIntercepter1"></interceptor></span>
				<interceptor name="theIntercepter2"
				class="com.yanlei.interceptor.TheInterceptor2"></interceptor>
		</interceptors>

3、在action中使用,可以用 param 传参数,  拦截器中testname 接受。
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">		<action name="login" class="com.yanlei.struts2.LoginAction"></span>

			<interceptor-ref name="theIntercepter1">
			<param name="test">yanlei</param>
			</interceptor-ref>
			<interceptor-ref name="theIntercepter2"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>

   注意:

      <interceptor-ref name="defaultStack"></interceptor-ref>  默认拦截器站必须有,一般默认有,但写了新的会覆盖。

4、注意  ActionInvocation接口,invocation.getAction().getClass() 方法 可以得到  需要拦截的action。


5、方法拦截器,继承 MethodFilterInterceptor ,与Interceptor接口,AbstractInterceptor一样编写,注册,使用时

	                <interceptor-ref name="theIntercepter3">
				<param name="includeMethods">execute</param>
				<param name="excludeMethods">myExecute</param>
			</interceptor-ref>
    针对method="myExecute"  拦截includeMethod  ,不拦截excludeMethod

6、配置监听,实现PreResultListener接口,在Action执行后,到达result之前。

     然后再拦截器中 方法中  :  invocation.addPreResultListener(new TheLisetener());   注册该监听。


7、配置自己的默认拦截器栈,如对于没有session 内容的页面,提示没有登录,跳转到错误页面,但loginAction不拦截

    方法:先配置拦截器,在配置拦截器,再配置自己的拦截器栈,然后设置为默认。      所有action在不写拦截器的情况下,默认即为mydefaultStack

public class LoginInterceptor extends MethodFilterInterceptor {
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		if (invocation.getAction().getClass() == LoginAction.class) {   //如果是LoginAction的,直接可以进入,直接invoke
			return invocation.invoke();
		}

		Map map = invocation.getInvocationContext().getSession();
		if (map.get("userInfo") == null) {
			return Action.ERROR;                         //如果是Session中行没有消息,就返回错误页面,全局的result中配置
		} else {
			String result = invocation.invoke();          //否则正常流程	
                	return result;
		}

	}

}


	<interceptor name="loginInterceptor"
				class="com.yanlei.interceptor.LoginInterceptor">
			</interceptor>
			<interceptor-stack name="mydefaultStack">   <span style="background-color: rgb(255, 0, 0);">  //自己的默认拦截器,就是系统默认的和 自己的拦截器</span>
				<interceptor-ref name="loginInterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
<span style="white-space:pre">		</span><default-interceptor-ref name="mydefaultStack"></default-interceptor-ref> <span style="color:#ff0000;">设置默认拦截器</span>


9、可以配置多个包,拆分struts.xml
<struts>
<span style="white-space:pre">	</span><package name="struts2" extends="struts-default"><pre name="code" class="html" style="font-size: 13.3333px;"><span style="white-space:pre">	</span> </package>
 
<span style="white-space:pre">	</span><package name="struts3" extends="struts-default">
<pre name="code" class="html" style="font-size: 13.3333px;"><span style="white-space:pre">	</span> </package>
 
  <span style="white-space:pre">	</span><include file="struts-1.xml"></include>
<span style="white-space:pre">	</span><include file="struts-2.xml"></include>
</struts>














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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值