在strut2中 个人认为最重要的还是自定义拦截器的使用
首先要定义拦截器 以<interceptors>包括起来 在这之中 可以定义拦截器 也可以定义拦截器栈 进行使用
<interceptor name = "first" class = "com.kevinswift.first"></interceptors>
<interceptor name = "second" class = "com.kevinswift.second"></interceptors>
<interceptor-stack name = "mystack">
<interceptor-ref name = "first"></interceptor-ref>
<interceptor-ref name = "second"></interceptor-ref>
</interceptor-stack>
要记住拦截器栈的使用也是需要先定义拦截器的
随后在action中进行调用
<action name = "firstAction" class = "com.kevinswift.action.FirstAction">
<!-- 指定action结果类型为chain -->
<result type = "chain">secondAction</result>
<interceptor-ref name = "mystack"></interceptor-ref>
</action>
<action name = "secondAction" class = "com.kevinswift.action.SecondAction">
<result>success.jsp</result>
定义为拦截器链
在拦截器指定的类中 常用的是继承自abstractInterceptor接口 只需调用一个intercept方法即可
package com.custom.action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class FirstAction extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
System.out.println("第一个拦截器的使用" );
//调用下一个拦截器
String result = arg0.invoke();
System.out.println("第一个拦截器的调用 2");
return result;
}
}
invoke方法表示下一拦截器的使用 当没有拦截器可以使用的时候 就默认调用action的类了
secondAction中也同样如此
package com.custom.action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SecondAction extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
System.out.println("第二个拦截器的调用1");
String res = arg0.invoke();
System.out.println("第二个拦截器的调用2");
return res;
}
}
大神建议struts看源码 还是大神比较靠谱