struts2的Interceptor实现

来源:互联网 发布:js如何设置光标大小 编辑:程序博客网 时间:2024/05/18 00:25
Interceptor interface
publicinterface Interceptor extends Serializable {
    void destroy();
    void init();
    String intercept(ActionInvocation invocation) throws Exception;
}
init方法是在interceptor被实例化之后,intercept方法调用之前被调用的。主要用来分配资源。

interceptor方法是拦截的主要逻辑,返回一个结果,这个结果用于向另一个web资源提出请求时的参数。

调用ActionInvoaction类型的参数invoke方法将会执行action(如果这个拦截器是栈的最后一个拦截器)

或者另外一个拦截器。注意:记住result被呼出之后,invoke会返回。如果想在结果被呼出之前做一些其他事情就需要实现PreResultListener。一个struts2的action实例被每一个request创建,没有必要线程安全。Interceptors在各requests之间是共享的,必须线程安全。AbstractInterceptorAbstractInterceptor类提供init和destroy的空实现,可以被用于这两个方法没有需要实现的时候可以继承一个拦截器实现的例子
importcom.opensymphony.xwork2.ActionInvocation;
importcom.opensymphony.xwork2.interceptor.AbstractInterceptor;
publicclassSimpleInterceptor extendsAbstractInterceptor {
    publicString intercept(ActionInvocation invocation) throwsException {
       MyAction action = (MyAction)invocation.getAction();
       action.setDate(newDate());
       returninvocation.invoke();
    }
}
翻译自http://struts.apache.org/docs/writing-interceptors.html
0 0
原创粉丝点击