Struts2讲义八(转载)
Struts2讲义8(转载)继承抽象拦截器的自定义拦截器配置 技术要点 本节代码介绍抽象拦截器配置并对缺省拦截
Struts2讲义8(转载)
继承抽象拦截器的自定义拦截器配置
技术要点
本节代码介绍抽象拦截器配置并对缺省拦截器栈做简单介绍。
?
继承抽象拦截器类的自定义拦截器类编写方式。
? 配置文件struts.xml中如何定义缺省拦截器。
演示代码
<!----------------文件名:ExampleInterceptor.java---------------->import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class ExampleInterceptor extends AbstractInterceptor {//重写抽象拦截器的拦截方法@Overridepublic 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"src="/img/2012/10/18/1216521606.gif"><!--------------------------文件名: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”的拦截器都已经执行。