struts拦截器使用
一 方法调用之后使用的拦截器 实现interceptor 接口
拦截器 一定是线程安全的,就是要同步
public interface Interceptor extends Serializable { void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception; }
实现方法intercept
example
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class SimpleInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { MyAction action = (MyAction)invocation.getAction(); action.setDate(new Date()); return invocation.invoke(); } }
<struts> ... <package name="struts-default"> <interceptors> <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/> <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> ... </interceptors> </package> ... </struts>
二 要实现在之前 调用 就实现 PreResultListener的接口