Struts2讲义9

[size=xx-large]继承方法拦截器的自定义拦截器配置[/size]
技术要点
本节代码介绍方法拦截器配置并对缺省拦截器栈对整个Web项目的Action影响进行介绍。
 [align=left]继承方法拦截器类的自定义拦截器类编写方式。
 配置文件struts.xml中如何定义方法拦截器和其属性。
 对所有Action配置拦截器和拦截器栈。[/align]演示代码
<!-------------------文件名:ExampleInterceptor.java----------------->
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class ExampleInterceptor extends MethodFilterInterceptor {
//重写方法拦截器拦截方法
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
System.out.println("start invoking3...");
String result = arg0.invoke();
System.out.println("end invoking3...");
return result;
}

LoginAction中增加了method方法
<!------------------------文件名:LoginAction.java------------------->
public String method()throws Exception {
FORWARD = "success";
return FORWARD;
}

拦截器映射配置。
<!--------------------文件名:struts.xml-------------->
<struts>
<!-- Action所在包定义 -->
<package name="C04.3" extends="struts-default">
<!-- 拦截器配置定义 -->
<interceptors>
<interceptor name="example"
class="com.example.struts.interceptor.ExampleInterceptor">
</interceptor>
</interceptors>
<!--
缺省拦截器栈配置定义
<default-interceptor-ref name="example"></default-interceptor-ref>
-->
<!-- Action名字,类以及导航页面定义 -->
<!-- 通过Action类处理才导航的的Action定义 -->
<action name="Login"
class="com.example.struts.action.LoginAction" method="method">
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/success.jsp</result>
<!-- Action方法拦截器配置定义 -->
<interceptor-ref name="example">
<!-- 被拦截方法配置定义 -->
<param name="includeMethods">method</param>
<!-- 不被拦截方法配置定义 -->
<param name="excludeMethods">method,execute</param>
</interceptor-ref>
</action>
</package>
</struts>

“includeMethods”配置后的拦截器执行效果如图4.6所示。

图4.6 执行方法拦截器后效果
“includeMethods”和“excludeMethods”同时配置后的拦截器执行效果如图.7所示。

图4.7 method方法还是被拦截器拦截
代码解释
(1)ExampleInterceptor类中,继承MethodFilterInterceptor抽象类。读者也可以查看struts2的源代码,在MethodFilterInterceptor中也只有一个抽象方法,但该抽象方法名为“doIntercept”。也对这个方法进行重写。重写内容和4.3.1小节类似。
(2)LoginAction.java中又定义了一个名为“method”方法,在struts.xml配置文件中,因为LoginAction中有execute方法,又有method方法,因此在<Action>中,请读者注意struts.xml中黑体部分,该部分代码表示现在LoginAction只执行method方法,而execute方法不被执行。笔者在<Action>中增加了一个“method”属性,该属性中“=”后面的内容是Action中具体方法名,如果不写“method”属性,Action是缺省执行execute方法。如果写了“method”属性,Action就执行“=”后写的具体方法。而不会执行execute方法。“example”拦截器还是如之前在<Action>前定义。在<Action>中配置“example”拦截器,笔者增加了“includeMethods”和“excludeMethods”两个param属性定义。“includeMethods”表示的是被拦截器拦截的方法。方法名写在<param>和</param>之间,如果有多个方法开发人员需要拦截器拦截,则方法名之间以“,”相隔。“excludeMethods”表示的是不被拦截器拦截的方法。如果有多个方法,也是以“,”相隔。
注意:如struts.xml配置文件中代码所示。假设
<param name="excludeMethods">method,execute</param>
这行代码被注释,则运行后在MyEclipse的控制台中看见是如图4.6的运行后效果。这说明method方法被拦截。如果
<param name="includeMethods">method</param>
这行代码被注释,则MyEclipse的控制台中是没有任何拦截器拦截信息显示。说明method没有被拦截器拦截即拦截器没有执行。
但是如struts.xml配置文件中代码显示,上述两行代码都没有被注释,读者有时候会不知道method方法到底是被拦截器拦截还是不被拦截。其实运行后的效果如图4.7所示。这说明method方法在两个属性中都被定义,Struts2认为method方法还是被拦截的。
(3)请读者注意struts.xml配置文件中,<Action>前被注释的<default-interceptor-ref >定义。该标签表示的是所有Action都会执行的拦截器或拦截器栈的定义。之前的代码中对于拦截器的定义是在<Action>前,拦截器的配置都是在<Action>中,比如“example”拦截器只有在LoginAction执行时候才会去拦截。如果是配置<default-interceptor-ref >中,则不管是LoginAction还是其他struts.xml配置文件中定义的Action都会被“example”拦截。在<default-interceptor-ref >中,也可以配置拦截器栈。如4.3.2小节中的“exampleStack”拦截器栈如果在<default-interceptor-ref >中配置,则所有Action执行时候,“exampleStack”拦截器栈都会执行该栈中包含的拦截器。
注意:struts.xml配置文件中要么没有<default-interceptor-ref >定义,如果定义了也只能定义一次。该标签在struts.xml配置文件中只能写在<Action>前,而且只能写一次。不能重复定义它。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值