Struts2拦截器

阅读更多

 

Struts 2框架本身内置了可实现多种功能的拦截器 。这些拦截器可以在Struts 2的配置文件struts-default.xml中。

我们可以在struts2-core-2.1.6.jar中找到struts-default.xml,这个文件中配置了所有的struts2已经定义好的拦截器interceptor。

 

 拦截器是在struts2的配置文件struts.xml中进行配置的,在struts.xml中可以配置多个拦截器,它们被包在之间,例如下面的配置:

 

 




	
		
		
		
			...
		
			
		
		...//action配置	
	

 

 

 拦截器的配置是在struts.xml中完成的,定义一个拦截器使用标签,其格式如下:

这种情况的应用非常广。有的时候,如果需要在配置拦截器时就为其传入拦截器参数,只要在与之间配置标签即可传入相应的参数。其格式如下:

 参数值
  ...//如果需要传入多个参数,可以一并设置

 

 

 使用拦截器,步骤如下:

 

 (1)创建使用拦截器的Action类

 

public class TimerAction extends ActionSupport{
	public String execute() throws Exception {		
		System.out.println("使用拦截器的例子");
		return SUCCESS;
	}
}

 

 (2)部署拦截器

 


	
    
		
			
		
		
	

 

 

(3)为Action添加拦截器

要在某个Action类中使用拦截器,必须为该Action添加指定的拦截器。


	

		
		
			/success.jsp
			
		
	

 

 

拦截器栈:
在Struts 2应用程序中,很多情况下一个Action要同时使用多个拦截器,如果单独为每个Action配置这些拦截器,无论管理还是维护都会变得非常困难,此时可使用拦截器栈。实际上拦截器栈也可被看做是一个拦截器,只不过它是一群拦截器的集合。
Struts 2框架本身内置了可实现多种功能的拦截器栈。

如:fileUploadStace拦截器栈,包含了fileUpload和basicStack两个拦截器,功能包含了basicStack提供的所有功能,此外还添加对文件上传的支持。
defaultStack连接器栈,包含了exception,alias,servletCofig,prepare,i18n,chain,debugging,profiling,scopeModeDriven,modeDriven,fileUpload,checkbox,staticparams,params,conversionError,validation,vorkflow这些拦截器,这个defaultStack是默认的拦截器栈,为Struts2框架中绝大多数的应用提供了所需的功能。

 

因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。


如果希望包下的所有action都使用自定义的拦截器,可以通过把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器不会起作用。

 

 

 自定义拦截器类:

 

 Struts 2提供了一些接口或类供程序员自定义拦截器。
例如,Struts 2提供了com.opensymphony.xwork2.interceptor.Interceptor接口,程序员只要实现该接口就可完成自定义拦截器类的编写。该接口的代码如下:

 

 

public interface Interceptor extends Serializable{
	void init();
	String intercept(ActionInvocation invocation) throws Exception;
	void destroy();
}

 

 该接口中有如下三种方法。
init():该方法在拦截器被实例化之后、拦截器执行之前调用。该方法只被执行一次,主要用于初始化资源。
intercept(ActionInvocation invocation):该方法用于实现拦截的动作。该方法有个参数,用该参数调用invoke()方法,将控制权交给下一个拦截器,或者交给Action类的方法。
destroy():该方法与init()方法对应,拦截器实例被销毁之前调用。用于销毁在init()方法中打开的资源。

 

 除了Interceptor接口之外,Struts 2框架还提供了AbstractInterceptor类,该类实现了Interceptor接口,并提供了init()方法和destroy()方法的空实现。在一般的拦截器实现中,都会继承该类,因为一般实现的拦截器是不需要打开资源的,故无须实现这两种方法,继承该类会更简洁。该类的代码实现为:

 

 

public interface AbstractInterceptor implements Interceptor{
	public AbstractInterceptor();
	public void init();
	public void destroy();
	public abstract String intercept(ActionInvocation invocation) throws Exception;
}

 

 

 

案例:自定义拦截器的创建:
首先创建一个web项目,然后添加struts2的jar包,并在web.xml中进行配置struts2。
1、web.xml:



  testInterceptor002
  
    index.jsp
  
  
  
 	struts2.0
 	org.apache.struts2.dispatcher.FilterDispatcher
  
  
    struts2.0
    /*  
  
  

 

 

然后我们创建一个action:
2、MyAction:

package com.yun.action;

import com.opensymphony.xwork2.ActionSupport;

public class MyAction extends ActionSupport {
	
	
	@Override
	public String execute() throws Exception {

		System.out.println("action执行。。。");
		return NONE;
	}

}

 

在struts.xml中把这个action配置上:

3、struts.xml:

 




	

		 
		
		


 

然后我们建立一个页面,用于请求这个action:

4、index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



	myAction


 


这样,我们就可以从前台请求到后台的action了。现在我们需要把拦截器加上:

 

创建一个类,让这个类继承com.opensymphony.xwork2.interceptor.AbstractInterceptor
这样,这个类就是一个拦截器了,

在这个类中就有实现的父类的intercept()方法:

 

package com.yun.interceptor;

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

public class MyInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		return null;
	}

}

 

我们在这个intercept()方法中写两个输出进行区分什么时候执行:

 

package com.yun.interceptor;

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

public class MyInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		System.out.println("在action前执行。。。");
		
		String result=invocation.invoke();
		
		System.out.println("在action后执行。。。");
		
		return result;
	}

}

 

invocation.invoke();表示将控制权交给下一个拦截器或者action中。即执行下一个拦截器或action。

然后需要在struts.xml中把这个拦截器配置上,在package标签中进行配置:

 

	    
	        
	          
	    

 

我们需要在执行MyAction的时候要使用这个拦截器,那么还需要再MyAction 中进行引用这个拦截器才可以:


    
    
    

 

注意,当我们为action指定要引用的action的时候,需要加上对默认的拦截器栈的引用,因为在action中一旦加上对拦截器的引用之后,默认的拦截器就不起作用了,我们为了让action的默认拦截器起作用,就要手动的去引用默认的拦截器栈defaultStack。

这样这个拦截器就被配置上了,我们在请求myAction的时候,就会听过这个拦截器的处理,我们可以运行一下代码,点击index.jsp页面的链接,或者直接在地址栏请求myAction,然后我们就看到控制台打印的信息了。

 

我们看到当我们请求myAction 的时候,先执行了MyInterceptor类,然后我们在该类中调用了invocation.invoke()这个方法,就去执行MyAction,然后在MyAction执行完之后,又回到MyInterceptor的invoke()方法,接着invoke()方法往下执行。

这个自定义的拦截器就完成了。

 

 

 

你可能感兴趣的:(Struts2,拦截器,笔记)