Struts2的拦截器_小例子
Struts2拦 的截器小例子2008-11-07 10:38Struts2拦拦拦 的截器
Struts2拦拦拦拦拦拦拦拦的截器体系是一AOP拦拦哲学。Strut2拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦的截器可以地截送到指定Action拦拦拦拦的求,通拦拦截器机制,可以在Action拦拦拦拦拦拦拦拦拦拦拦拦拦拦行前后插入某些代,通方式可以把多个Action拦拦拦拦中需要重指定的代提取出来,在截器中定,从而提供更好的代重用。截器体系是拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦Struts2拦拦拦拦的一个重要成部分,Struts2拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦框架就是由大量的内置截器来的。另外,它是即插即用的,即当我需要使用某个截器,只需要在配置文件中用中截器即可,如果不需要使用截器,也只需要取消在配置文件中拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦截器,不管是否用某个截器,于整个Strut2框架不会有任何影响的。
Struts拦拦拦拦拦拦拦框架提供了很多截器,它Struts2拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦的大部分功能,能意大部分用的能用功能,但要系相的功能,需要使用用自定截器:拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦
如果用要自己的截,需要拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦Interceptor接口,它包括三个方法:
Init()
destroy()
intercept(ActionInvocation action)
一般来只需要最后一个方法即可,所以拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦Struts2提供了一个AbstractInterceptor拦,它提供了init
和destory拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦方法的空。在的中,只需要承AbstractInterceptor拦拦拦拦拦拦拦来自定截器。下
面一个的截器:拦拦拦拦拦拦拦拦拦拦拦
拦拦拦拦截器(SimpleInterceptor.java):
package my;
import java.util.Date;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ActionInvocation;public class SimpleInterceptor extends AbstractInterceptor
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String intercept(ActionInvocation invocation)throws Exception
{
LoginAction action = (LoginAction)invocation.getAction();
System.out.println(name+"拦拦拦截器的作:拦拦拦拦拦始行登Action拦拦拦拦的:"+new Date());
if(action.getTip().equals("flying"))
{
System.out.println("user name error");
return "input";
}
long start = System.currentTimeMillis();
String result = invocation.invoke();
System.out.println(name+"拦拦拦拦拦拦拦拦拦截器的作:行完登Action拦拦拦拦的:" + new Date());
long end = System.currentTimeMillis();
System.out.println("拦拦拦拦拦拦截器的作:行action拦拦拦事件的是:"+(end-start)+"毫秒");
return result;
}
}拦拦拦配置截器(Struts.xml):
simple
/login.jsp
/success.jsp
my
拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦即一个的截器,默下,譔截器截了Action拦拦拦拦拦拦拦拦中的所有方法,如果我只需要截某个方法,Struts2提供了一个MethodFilterInterceptor拦拦Action拦拦拦拦拦拦中方法的功能,
MethodFilterInterceptor是AbstractInterceptor拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦的子,如果要截器方法功
能,需要拦拦拦拦
承MethodFilterInterceptor拦拦拦拦拦拦。用只需要重写MethodFilterInteceptor中的doInterceptor(ActionInvocation action)拦拦拦拦即可。其内容上与interceptor拦拦一。
拦拦拦拦拦拦拦拦拦拦拦了方法只需要在interceptor拦拦拦拦拦拦拦的配置文件里置两个属性:
excludeMethods拦拦拦拦拦拦:指定不需要截的方法名
includeMethods拦拦拦拦拦拦:指定需要截的方法名
若一个方法同出中,方法将被截。拦拦拦拦拦拦拦拦拦拦拦拦拦拦
一个方法的截器拦拦拦拦拦拦拦拦(SimpleInterceptor.java)拦拦与上面的一:
package my;
import java.util.Date;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.ActionInvocation;public class SimpleInterceptor extends MethodFilterInterceptor{
private String name;
public void setName(String name)
{
this.name = name;
}
public String doIntercept(ActionInvocation invocation)throws Exception
{
LoginAction action = (LoginAction)invocation.getAction();
System.out.println(name+"拦拦拦截器的作:拦拦拦拦拦始行登Action拦拦拦拦的:"+new Date());
if(action.getTip().equals("flying"))
{
System.out.println("user name error");
return "input";
}
long start = System.currentTimeMillis();
String result = invocation.invoke();
System.out.println(name+"拦拦拦拦拦拦拦拦拦截器的作:行完登Action拦拦拦拦的:" + new Date());
long end = System.currentTimeMillis();
System.out.println("拦拦拦拦拦拦截器的作:行action拦拦拦事件的是:"+(end-start)+"毫秒");
return result;
}
}
配置方法截器拦拦拦拦拦(Struts.xml):
simple
/login.jsp
/success.jsp
my
execute
拦拦拦拦拦拦拦拦上面只在引用截器注入一个excludeMethods拦拦属性,其它的与上面的配置一。
Struts2 拦拦拦拦拦拦拦截器配置程2010-01-28 18:421:拦拦拦拦拦拦拦拦所有截器的超接口Interceptor ,Action拦去拦拦个接口;
Interceptor 它其中有三个方法(init(),destroy() ,interceptor()):
Init()方法:拦拦拦拦拦拦拦拦拦拦拦在服器起的候加一次,拦拦拦并且只加一次;
Destroy()方法:拦拦拦拦拦拦拦拦当截器行的方法毁;
Interceptor()方法:拦拦拦拦拦拦其中里有一个参数invocation
public String intercept(ActionInvocation invocation) throws xception {
System.out.println("interceptor!!");
String result=invocation.invoke();
return result;
}
Invocation.invoke()拦拦拦拦拦拦拦拦拦拦拦是如果只有一个截器行完个方法后,拦拦拦会返回,拦拦拦如果有多个截器,拦拦拦它序的拦拦拦拦行完所有的截器,拦拦拦才返回.
2:拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦可以在系初始化中截器指定默的参数(拦拦拦拦拦拦也包括了定截器方式)如下:
拦拦拦拦拦拦在截器中把hello当做属性set/get拦拦拦拦拦方式注入到截器中;
拦拦拦
拦拦拦拦拦拦拦拦拦拦拦拦也可以在使用截器的候它置参数:
就是在一个action 的reslut下面配置上如下:
/success.jsp
/register.jsp
welcome
2.拦截器,拦拦拦拦拦拦拦拦拦拦拦拦拦拦截器和默的截器之的系
1:拦拦拦拦拦拦拦拦拦拦拦截器和截器是一个的,拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦也就是一个截器中包括多截器, 拦拦拦拦拦拦拦拦拦一个截器中可以包括多截器拦拦拦拦拦拦,配置如下方式:
拦拦拦
拦截器的使用:1.拦先定;2.在引用使用;
2:struts2拦拦拦拦拦拦拦拦拦 中有一个系默的截器是defaultStack,拦拦拦拦拦拦拦拦拦如果你手引用自己的截器,拦拦拦系默的截器将不起作用拦拦拦拦拦拦拦拦拦;拦拦拦拦拦拦拦拦拦拦拦拦必需手引入系的截器
如果想改系默的截器拦拦拦拦拦拦拦拦拦拦,拦拦拦拦可以配置:
其中myStack拦拦拦拦拦拦拦拦是自己定的截器名字;
如果截器中有多个截器拦拦拦拦拦拦拦拦拦拦拦,拦拦在行action拦拦拦拦拦拦拦拦拦拦拦拦拦之前的序跟配置截器的序一致,而在action拦拦之后行的序是相反的拦拦拦拦拦拦;
3:拦拦拦拦抽象的截器AbstractInterceptor
1:Interceptor拦拦拦拦拦拦拦个超截器接口,拦拦有三方法需要,但是如果不想使用init();
和destroy()方法,拦拦拦拦拦拦拦拦拦拦可以去承个抽象截器;
它的使用跟上的没有什区拦拦拦拦拦拦拦拦;
4:拦拦拦拦拦方法截器MethodFilterInterceptor
1:拦拦拦拦拦拦拦拦拦拦拦拦上的截器都要是整个action的,拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦如果某个方法行截可以去承个;
它的使用跟上的使用方法差不多拦拦拦拦拦拦拦拦拦,拦拦拦拦拦拦拦拦拦只是需要要配置它那个方法行截,拦拦拦拦拦拦拦拦拦方法截器最好不要
配置到自己置默的截器里拦拦拦拦拦拦拦拦拦拦拦,拦拦拦自己手配置.
interceptor-ref name="myInterceptor3">
execute
execute
其中includeMethods ,excludeMethods是固定写法: includeMethods 拦拦拦拦拦拦包含截那些方法,多个方法需要用”,”拦隔; excludeMehtods拦拦拦拦拦拦拦是排除截的那些方法;
5:拦听器PreResultListener接口
1:拦拦拦拦拦拦拦拦拦拦拦拦它的听点在截器行完某个action方法后,拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦拦在渲染之前做一些事情,某个去个接口,
然后向需要它的截器中注册去如下代:拦拦拦拦拦拦拦拦拦拦拦拦拦
publicclass MyInterceptor3 extends MethodFilterInterceptor {
privatestaticfinallongserialVersionUID = 3756655410194005443L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//拦拦拦拦拦拦拦拦拦拦把听器注册到截中去;
invocation.addPreResultListener(new MyListener());
System.out.println("my Interceptor3");
String result=arg0.invoke();
System.out.println("my interceptor3 finshed!");
return result;
}