struts2全局结果不会重定向到操作

时间:2013-09-24 22:56:30

标签: struts2 global interceptor

我有一个自定义拦截器(TransferInterceptor),它检查我的应用程序中的函数更改。当它发生更改时,拦截器返回一个后处理字符串(结果),其中包含我要重定向到的操作的全局结果名称。我在struts.xml文件的全局结果中定义了结果,但它们没有重定向到指定的操作。我有一个“登录”全局结果可以正常工作,但它是从JSP调用的:

<s:a action="Login.action">Login</s:a>

struts.xml文件:

<package name="default" extends="struts-default" namespace="/">
    <interceptors>
        <interceptor name="AuthenticationInterceptor" class="com.purchasing.utils.AuthenticationInterceptor" />
        <interceptor name="TransferInterceptor" class="com.purchasing.utils.TransferInterceptor" />
        <interceptor-stack name="secureStack">
            <interceptor-ref name="TransferInterceptor" />
            <interceptor-ref name="AuthenticationInterceptor" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="secureStack" />

    <global-results>
        <result name="Login">login.jsp</result>
        <result name="RESULTA" type="redirect">ACTIONA</result>
        <result name="RESULTB" type="redirect">ACTIONB</result>
    </global-results>

    <action name="Login" class="com.purchasing.actions.Login">
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <result name="success">welcome.jsp</result>
        <result name="error">login.jsp</result>
        <result name="input">login.jsp</result>
    </action>

    <action name="ACTIONA" class="com.purchasing.actions.ACTIONA">
        <result name="success">ACTIONA.jsp</result>
        <result name="error">ACTIONA.jsp</result>
        <result name="input">ACTIONA.jsp</result>
    </action>   

    <action name="ACTIONB" class="com.purchasing.actions.ACTIONB">
        <result name="success">ACTIONB.jsp</result>
        <result name="error">ACTIONB.jsp</result>
        <result name="input">ACTIONB.jsp</result>
    </action>                

</package>

TransferInterceptor:

public String intercept(ActionInvocation ai) throws Exception
{
    String result = ai.invoke();

    session = ai.getInvocationContext().getSession();
    fl = (FunctionLine) session.get(Constants.FUNCTION_LINE);
    function = fl.getFl_function();
    last_function = (Integer) session.get(Constants.LAST_FUNCTION);

    if (function != last_function) {
       session.put(Constants.LAST_FUNCTION, new Integer(function));
       switch (function) {
              case 61: return "RESULTA";
              case 62: return "RESULTB";
       }
       return "error";
    }
    else {
       return result;
    }
 }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

根据@Lukasz评论:

If you want to redirect to an action use redirectAction result type instead of redirect which is used to redirect to static resources

它仍然无效,因为ACTIONB&amp; ACTIONA从不执行,因为它们通过相同的拦截器堆栈,它返回redirectAction结果&amp;因此,它有点陷入无限循环。