第一种方法:
直接implements实现com.opensymphony.xwork2.interceptor.Interceptor
- public class MyInterceptor implements Interceptor {
-
- public void destroy() {
- System.out.println("destroy()");
- }
-
- public void init() {
- System.out.println("init()");
- }
-
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("Interceptor");
- String result = invocation.invoke();
- return result;
- }
-
- }
第二种方法
直接extends com.opensymphony.xwork2.interceptor.AbstractInterceptor
这种方法是少了destroy,init方法
- public class MyInterceptor2 extends AbstractInterceptor {
-
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- System.out.println("MyInterceptor2222");
- String result = invocation.invoke();
- return result;
- }
-
- }
第三种方法
直接extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
这种方法能对Action里面的方法级进行控制,是否执行这个方法
- public class MyInterceptor3 extends MethodFilterInterceptor {
-
- @Override
- protected String doIntercept(ActionInvocation invocation) throws Exception {
- System.out.println("use MethodFilterInterceptor");
- String result = invocation.invoke();
- return result;
- }
-
- }
然就是配置
- <interceptors>
- <interceptor name="myInterceptor" class="com.langhua.interceptor.MyInterceptor">
- </interceptor>
-
- <interceptor name="myInterceptor2" class="com.langhua.interceptor.MyInterceptor2">
- </interceptor>
-
- <interceptor name="myInterceptor3" class="com.langhua.interceptor.MyInterceptor3">
- <param name="excludeMethods">execute</param>
- <param name="includeMethods">addmessage</param>
- </interceptor>
-
- <interceptor-stack name="myInterceptorStack">
- <interceptor-ref name="myInterceptor3"></interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
里面分interceptor和interceptor-stack
stack也能引用stack
default-interceptor-ref表示Action如果不指定interceptor就用这个default的
extends="struts-default"表示这个XML文件是extends 另一个xml的,名字叫struts-default
在这个XML中,配置了一个常用的interceptor可以去Core包中找到这个XML
interceptor配置很灵活的。如果要写自己的interceptor就要把<interceptor-ref name="defaultStack"></interceptor-ref>默认的给加上,当然也可以自己配置。
interceptor的参数
- <interceptor-stack name="myInterceptorStack">
- <interceptor-ref name="myInterceptor3"></interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
这个是因为MethodFilterInterceptor 里有这两个成员变量,如果你自己的interceptor要带参数的话就要相应的Action里面写到成员变量,并加上get,set方法
- public abstract class MethodFilterInterceptor extends AbstractInterceptor {
- protected transient Logger log = LoggerFactory.getLogger(getClass());
-
- protected Set<String> excludeMethods = Collections.emptySet();
- protected Set<String> includeMethods = Collections.emptySet();
监听器
首先要实现com.opensymphony.xwork2.interceptor.PreResultListener类
并重写里面的方法beforeResult
- public class MyListener implements PreResultListener {
-
- public void beforeResult(ActionInvocation invocation, String resultCode) {
- System.out.println(resultCode);
- }
-
- }
然后再在拦截器里面调用
- invocation.addPreResultListener(new MyListener());
监听器是在这个拦截器完成别的拦截器之后调用的
struts2 Action获得HttpSession,HttpServletRequest,HttpSevletResponse的方法
非IOC方式
这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类
- ActionContext ctx = ActionContext.getContext();
-
- HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
-
- HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
-
-
-
-
-
- HttpServletRequest request = ServletActionContext.getRequest ();
主要是这两个类com.opensymphony.xwork2.ActionContext和org.apache.struts2.ServletActionContext都对request等进行了大量的封装,直接调用方法就可以获和
更好一点的IOC方式
action类实现ServletRequestAware接口,并新建一个HttpServletRequest request
- public class UserLoginAction extends ActionSupport implements ServletRequestAware{
- public void setServletRequest(HttpServletRequest request) {
- this.request=request;
- }
- 然后可以生成的request得到对象,如request.getRemoteAddr()
- action类实现SessionAware接口,并创建一个MAP对象session
- public class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{
- public void setServletRequest(HttpServletRequest request) {
- this.request=request;
- }
- public void setSession(Map session) {
- this.session=session;
}
这些获得HttpServletRequest等对象需要implments的接口都在
org.apache.struts2.interceptor下面
如Apllication的是ApplicationAware
如HttpSession的是SessionAware(struts2的Session都被封装成Map了)
如HttpServletRequest的是ServletRequestAware
如HttpServletResponse的是ServletResponseAware
|