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之间是共享的,必须线程安全。 | |||||||||||||||||||
AbstractInterceptor | |||||||||||||||||||
AbstractInterceptor类提供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
|
struts2的Interceptor实现
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.