:oops: [size=xx-large]继承抽象拦截器的自定义拦截器配置[/size]
技术要点
本节代码介绍抽象拦截器配置并对缺省拦截器栈做简单介绍。
[align=left]继承抽象拦截器类的自定义拦截器类编写方式。
配置文件struts.xml中如何定义缺省拦截器。[/align]演示代码
图4.4 执行拦截器后效果
拦截器栈执行效果如图4.5所示。
图4.5 缺省拦截器栈中包含的校验拦截器执行
代码解释
(1)ExampleInterceptor类中,继承AbstractInterceptor抽象类。读者可以查看struts2的源代码,在AbstractInterceptor中只有intercept这一个抽象方法。因此自定义的ExampleInterceptor中只需要对这个方法进行重写。重写内容和4.3.1小节类似。
(2)struts.xml配置文件中,在<Action>前还是如4.3.1定义名为“example”的拦截器。在<Action>中,配置“example”拦截器。
注意:在<Action>中还配置了“defaultStack”拦截器栈,这是因为如果在<Action>中不配置该拦截器栈,则Login.action运行时候只会执行配置的“example”拦截器,不会执行“defaultStack”拦截器栈。而且“defaultStack”是Struts2配置的缺省拦截器栈,在4.1小节中的struts-default.xml中定义的拦截器都是由它来调用执行。Struts2规定如果在<Action>中,开发人员配置了自己定义的拦截器或拦截器栈,不显式在struts.xml配置文件中配置“defaultStack”拦截器栈,则所有struts-default.xml中定义的拦截器都不会执行即不执行“defaultStack”拦截器栈。当然如果在<Action>中开发人员没有配置自己定义的拦截器或拦截器栈,就算不显示配置“defaultStack”拦截器栈,则“defaultStack”拦截器栈是会执行的。
(3)为了让Action被执行时候,“defaultStack”拦截器栈和“example”的拦截器都执行,一种办法是如上代码所示。另一种办法也可以如struts.xml配置文件中被注释的那段自定义的拦截器栈配置。在4.1小节中也说过拦截器栈中可以配置拦截器栈,因此在注释中“defaultStack”拦截器栈可以作为配置的“exampleStack”拦截器栈的子元素。在<Action>中配置代码可以写成如下代码中黑体所示:
技术要点
本节代码介绍抽象拦截器配置并对缺省拦截器栈做简单介绍。
[align=left]继承抽象拦截器类的自定义拦截器类编写方式。
配置文件struts.xml中如何定义缺省拦截器。[/align]演示代码
<!----------------文件名:ExampleInterceptor.java---------------->
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ExampleInterceptor extends AbstractInterceptor {
//重写抽象拦截器的拦截方法
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("start invoking2...");
String result =arg0.invoke();
System.out.println("end invoking2...");
return result;
}
}
拦截器映射配置。<!--------------------------文件名:struts.xml----------------->
<struts>
<!-- Action所在包定义 -->
<package name="C04.3.2" extends="struts-default">
<!-- 拦截器配置定义 -->
<interceptors>
<interceptor name="example"
class="com.example.struts.interceptor.ExampleInterceptor">
</interceptor>
<!--
拦截器栈配置定义
<interceptor-stack name="exampleStack">
<interceptor-ref name="example"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
-->
</interceptors>
<action name="Login"
class="com.example.struts.action.LoginAction">
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/success.jsp</result>
<!-- Action拦截器配置定义 -->
<interceptor-ref name="example"></interceptor-ref>
<!-- Action拦截器栈配置定义 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
拦截器执行效果如图4.4所示。图4.4 执行拦截器后效果
拦截器栈执行效果如图4.5所示。
图4.5 缺省拦截器栈中包含的校验拦截器执行
代码解释
(1)ExampleInterceptor类中,继承AbstractInterceptor抽象类。读者可以查看struts2的源代码,在AbstractInterceptor中只有intercept这一个抽象方法。因此自定义的ExampleInterceptor中只需要对这个方法进行重写。重写内容和4.3.1小节类似。
(2)struts.xml配置文件中,在<Action>前还是如4.3.1定义名为“example”的拦截器。在<Action>中,配置“example”拦截器。
注意:在<Action>中还配置了“defaultStack”拦截器栈,这是因为如果在<Action>中不配置该拦截器栈,则Login.action运行时候只会执行配置的“example”拦截器,不会执行“defaultStack”拦截器栈。而且“defaultStack”是Struts2配置的缺省拦截器栈,在4.1小节中的struts-default.xml中定义的拦截器都是由它来调用执行。Struts2规定如果在<Action>中,开发人员配置了自己定义的拦截器或拦截器栈,不显式在struts.xml配置文件中配置“defaultStack”拦截器栈,则所有struts-default.xml中定义的拦截器都不会执行即不执行“defaultStack”拦截器栈。当然如果在<Action>中开发人员没有配置自己定义的拦截器或拦截器栈,就算不显示配置“defaultStack”拦截器栈,则“defaultStack”拦截器栈是会执行的。
(3)为了让Action被执行时候,“defaultStack”拦截器栈和“example”的拦截器都执行,一种办法是如上代码所示。另一种办法也可以如struts.xml配置文件中被注释的那段自定义的拦截器栈配置。在4.1小节中也说过拦截器栈中可以配置拦截器栈,因此在注释中“defaultStack”拦截器栈可以作为配置的“exampleStack”拦截器栈的子元素。在<Action>中配置代码可以写成如下代码中黑体所示:
<!--------------------------文件名:struts.xml---------------------->
<action name="Login"
class="com.example.struts.action.LoginAction">
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/success.jsp</result>
<!-- Action拦截器栈配置定义 -->
<interceptor-ref name="exampleStack"></interceptor-ref>
</action>
这样的代码形式也能保证“defaultStack”拦截器栈和“example”的拦截器都执行。例如在登录页面不输入任何登录信息,单击“登录”按钮。在MyEclipse的控制台下,执行结果如图4.4所示。而在页面中显示如图4.5,“defaultStack”拦截器栈中包含的输入校验拦截器执行,显示拦截后的信息。这两张图就充分证明了“defaultStack”拦截器栈和“example”的拦截器都已经执行。