作者在 2010-11-02 15:45:44 发布以下内容
配置拦截器:
<interceptors>
<interceptor name="xxx" class="xxxx">
<param name="参数名">参数值</param>
</interceptor>
</interceptors>
定义拦截器栈:<interceptor name="xxx" class="xxxx">
<param name="参数名">参数值</param>
</interceptor>
</interceptors>
<interceptor-stack name="拦截器栈1">
<interceptor-ref name="拦截器1" />
<interceptor-ref name="拦截器2" />
<interceptor-ref name="拦截器栈2" />
</interceptor-stack >
可以使用两种方法为拦截器指定参数:(1)定义拦截器时指定<interceptor-ref name="拦截器1" />
<interceptor-ref name="拦截器2" />
<interceptor-ref name="拦截器栈2" />
</interceptor-stack >
(2)使用拦截器时指定
(2)的实例:
<interceptor-stack name="拦截器栈1">
<interceptor-ref name="拦截器栈2" />
<param name="拦截器1.参数1">参数值1</param>
<param name="拦截器2.参数2">参数值2</param>
</interceptor-stack >
如何在action中使用拦截器:<interceptor-ref name="拦截器栈2" />
<param name="拦截器1.参数1">参数值1</param>
<param name="拦截器2.参数2">参数值2</param>
</interceptor-stack >
<action name="xxx" class="xxxxxx">
<result name="xxx">/xxx.jsp</result>
<interceptor-ref name = "defaultStack" />
<interceptor-ref name = "interceptor1">
<param name="xxx"value1</param>
</interceptor-ref>
</action>
默认拦截器:<result name="xxx">/xxx.jsp</result>
<interceptor-ref name = "defaultStack" />
<interceptor-ref name = "interceptor1">
<param name="xxx"value1</param>
</interceptor-ref>
</action>
<default-interceptor-ref name="xxx" />
如果为某个包指定了默认拦截器,而在包中未显式的指定拦截器,则会使用那个默认拦截器。若显式的指定了拦截器,则默认拦截器不起作用了。如何在拦截器中过滤指定的方法:
<interceptor-ref name="testStack">
<param name="executeTime2.excludeMethods">bb</param>
<param name="executeTime2.includeMethods">aa</param>
</interceptor-ref>
<param name="executeTime2.excludeMethods">bb</param>
<param name="executeTime2.includeMethods">aa</param>
</interceptor-ref>
其中,includeMethods的优先级比较高。
如何在拦截器中取得一些参数:
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
String user = (String) session.get("userName");
String user = ServletActionContext.getRequest().getParameter("userName");
Map session = ctx.getSession();
String user = (String) session.get("userName");
String user = ServletActionContext.getRequest().getParameter("userName");
拦截器结果监听器:
首先需要写一个结果监听器:
public class MyPreResultListener implements PreResultListener {
public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("execute call back: " + resultCode);
long start = (Long) invocation.getStack().findValue("start");
long end = System.currentTimeMillis();
System.out.println("The Time used!!!: " + (end - start));
System.out.println("End@@@@@@@@@@@@@@@@@");
}
}
需要在拦截器中addPreResultListener一个new出来的监听器对象:public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("execute call back: " + resultCode);
long start = (Long) invocation.getStack().findValue("start");
long end = System.currentTimeMillis();
System.out.println("The Time used!!!: " + (end - start));
System.out.println("End@@@@@@@@@@@@@@@@@");
}
}
protected String doIntercept(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(new MyPreResultListener());
System.out.println("Start@@@@@@@@@@@@@@@@@");
long start = System.currentTimeMillis();
invocation.getStack().set("start", start);
String result = invocation.invoke();
return result;
}
invocation.addPreResultListener(new MyPreResultListener());
System.out.println("Start@@@@@@@@@@@@@@@@@");
long start = System.currentTimeMillis();
invocation.getStack().set("start", start);
String result = invocation.invoke();
return result;
}