TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
技术要点
本节代码介绍抽象拦截器配置并对缺省拦截器栈做简单介绍。
继承抽象拦截器类的自定义拦截器类编写方式。
配置文件struts.xml中如何定义缺省拦截器。
演示代码
java 代码
<!---------------- 文件名: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;
}
}
<!----------------文件名: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;
}
}[/code] 拦截器映射配置。
Java 代码
<!-------------------------- 文件名: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>
<!--------------------------文件名: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>[/code]
代码解释
(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>中配置代码可以写成 如下代码中黑体所示:
Java 代码
<!-------------------------- 文件名: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>
<!--------------------------文件名: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>[/code] 这样的代码形式也能保证“defaultStack”拦截器栈和“example”的拦截器都执行。
源码下载:http://file.javaxxz.com/2014/10/11/020141968.zip |
|