您的位置:首页 > 编程语言 > Java开发

struts2拦截器使用

2014-11-24 16:26 169 查看
<package name="testLogin" namespace="/" extends="struts-default">
<!-- 拦截器 -->
<interceptors>
<interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>
<!-- 定义公共的拦截器链,在action标签中只需要引用拦截器链 -->
<interceptor-stack name="defaultstack1">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>

<action name="demo" class="com.action.LoginAction">
<result name="error" type="redirect">/error.jsp</result>
<result name="success">/success.jsp</result>
<result name="checkError">/checkSession.jsp</result>
<interceptor-ref name="defaultstack1"></interceptor-ref>
</action>
</package>

这时定义一个全局的interceptor

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.custom.i18n.resources" value="globalMessages"/>

<constant name="struts.i18n.encoding" value="GBK"/>

<!--<constant name="struts.enable.DynamicMethodInvocation" value="true" />-->

<package name="user" extends="struts-default" >

<interceptors>

<interceptor name="MyInterceptor" class="com.gxk.test.MyInterceptor"/>

</interceptors>

<global-results>

<result name="login">/login.jsp</result>

</global-results>

<action name="login" class="com.gxk.test.Login">

<result>/interceptor/welcome.jsp</result>

<result name="error">/interceptor/error.jsp</result>

</action>

<action name="viewBook">

<result>/interceptor/viewBook.jsp</result>

<interceptor-ref name="defaultStack"/>

<interceptor-ref name="MyInterceptor"/>

</action>

</package>

</struts>

这时定义一个单独的action的interceptor,<global-results>定义了一个全局的login, 采用struts2的全局result配置,定义类中的返回类型是login,则转向login.jsp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: