struts2从零开始(三)

   拦截器



    1、拦截器设置令牌
      1.1、页面中添加设置令牌的标签(代码如下)
          
<form action="token.action" method="post">
      <s:token></s:token>
     <input type="submit" value="令牌测试"/>
  
      </form>


      1.2、配置action时需要引用token拦截器(代码如下)
      
<action name="token" class="cn.zhuojingxinxi.web.TokenAction">
	             <interceptor-ref name="token"></interceptor-ref>
	             <result name="success">/success.jsp</result>
	             <result name="invalid.token">/error.jsp</result>
	         </action>

      需要注意的是:需要配置的是当令牌失效的时候所要跳转的页面,其中名称必须是--invalid.token
    
   2、国际化
     2.1、在src下新建三个资源文件
    

    struts.properties中的内容是:struts.custom.i18n.resources=mymessage
    mymessage_zh_CN.properties中的内容是:messages.invalid.token=\u8868\u5355\u91CD\u590D\u63D0\u4EA4\uFF0C\u8BF7\u68C0\u67E5
mymessage_en_US.properties中的内容是:messages.invalid.token=The form has already been processed or no token was supplied, please try again.

   注意:如果配置了拦截器请必须加上默认的
   <interceptor-ref name="defaultStack"></interceptor-ref>

   3、自定义拦截器
    3.1、可是实现Interceptor接口(代码如下)
package cn.zhuojingxinxi.util;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class FirstInterceptor implements Interceptor {

	public void destroy() {
	System.out.println("销毁了");
		
	}

	public void init() {
		System.out.println("初始化");
		
	}

	public String intercept(ActionInvocation arg0) throws Exception {
		
		System.out.println("自定义的拦截器 begin");
		String str=  arg0.invoke();
		System.out.println("自定义的拦截器 end");
		return null;
	}

}

  3.2、继承AbstractInterceptor(代码如下)
  
package cn.zhuojingxinxi.util;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SecondInterceptor extends AbstractInterceptor {

	public void destroy() {
	System.out.println("销毁了");
		
	}

	public void init() {
		System.out.println("初始化");
		
	}

	public String intercept(ActionInvocation arg0) throws Exception {
		
		System.out.println("自定义的拦截器2 begin");
		String str=  arg0.invoke();
		System.out.println("自定义的拦截器2 end");
		return null;
	}

}

     3.3、在sruts.xml中申明拦截器(代码如下)
   
 <interceptors>
	          <interceptor name="first" class="cn.zhuojingxinxi.util.FirstInterceptor"></interceptor>
	           <interceptor name="second" class="cn.zhuojingxinxi.util.SecondInterceptor"></interceptor>
	       </interceptors>

    3.4、action中调用(代码如下)
    
<action name="my" class="cn.zhuojingxinxi.web.MyAction">
	             <interceptor-ref name="first"></interceptor-ref>
	             <interceptor-ref name="second"></interceptor-ref>
	             <interceptor-ref name="defaultStack"></interceptor-ref>
	             <result name="success">/success.jsp</result>
	        
	         </action>

    以下拦截器是针对action。以下这种拦截器是针对action中的方法拦截器:

    3.5、实现MethodFilterInterceptor接口(代码如下)
   
package cn.zhuojingxinxi.util;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class ThirdInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation arg0) throws Exception {
		
		System.out.println("自定义的拦截器3 begin");
		String str=  arg0.invoke();
		System.out.println("自定义的拦截器3 end");
		return str;
	}

	

}


   在action中的配置(代码如下)
 
<action name="my" class="cn.zhuojingxinxi.web.MyAction">
	             <interceptor-ref name="first"></interceptor-ref>
	             <interceptor-ref name="second"></interceptor-ref>
	             <interceptor-ref name="third">
	                <param name="includeMethods">save,delete</param>
	             </interceptor-ref>
	             <interceptor-ref name="defaultStack"></interceptor-ref>
	             <result name="success">/success.jsp</result>
	        
	         </action>

 
    <param name="includeMethods">save,delete</param>这句代码的意思是:配置的是白名单,只有其中配置的方法才会背自定义的拦截器所拦截

  3.6、当我们自定义了很多拦截器时,而我们有时候需要这些拦截器同时使用,就可以将这下拦截器配置成栈的形式(代码如下)
   
<interceptor-stack name="myInterceptorStack">
	             <interceptor-ref name="first"></interceptor-ref>
	             <interceptor-ref name="second"></interceptor-ref>
	             <interceptor-ref name="third"></interceptor-ref>
	             <interceptor-ref name="defaultStack"></interceptor-ref>
	           </interceptor-stack>

  值得注意的是:拦截器是有顺序的
                拦截器的返回值与跳转的页面无关
                拦截器的invoke表示往后传递调用,invokeActionOnly则会跳过后续拦截器直接调用action


源码下载请点这里:


   

猜你喜欢

转载自1136051009.iteye.com/blog/1768830