要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口
package com.yjdgis.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { @Override public void destroy() { } @Override public void init() { } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("进入了拦截器"); //invocation为当前拦截到的方法 String result = invocation.invoke(); return result; } }
配置拦截器:
<package name="text4" namespace="/control/employee4" extends="base"> <interceptors> <interceptor name="myInterceptor" class="com.yjdgis.interceptor.MyInterceptor" /> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="myInterceptor" /> </interceptor-stack> </interceptors> <action name="list_*" class="com.yjdgis.control.ConvertValueTest" method="{1}"> <interceptor-ref name="myInterceptorStack" /> <result name="success">/index.jsp</r<interceptor-ref name="myInterceptorStack"/><result name="success">/indexesult> </action> </package>
在使用了自己定义好了拦截器,一定要应用上系统默认的拦截器,否则struts2的许多核心功能都不能应用上
上面是一种方法还可以同时注入defaultStack