Struts 2 - RedirectAction to update Url to the redirected action

2.5k Views Asked by At

I am trying to redirect the action after a POST to the Dologin action to one of two actions. I would the url to reflect the action that i am redirecting to. However, the URL still remains with the action I posted to and not the redirected action. Below is the Struts.xml section -- Thank you

<package name="mobile" namespace="/mobile" extends="struts-default"> 
    <action name="login">
        <result>/mobile/login.jsp</result>
    </action>

    <action name="home">
        <result>/mobile/home.jsp</result>
    </action>

    <action name="Dologin" class="action.LoginAction">
        <result name="success" type="redirectAction">
            <param name="namespace">/mobile</param>
            <param name="actionName">home</param>
        </result>

        <result name="input" type="redirectAction">
            <param name="namespace">/mobile</param>
            <param name="actionName">login</param>
        </result>
    </action>
</package>
2

There are 2 best solutions below

0
On

I have tested what you mentioned here and it do reflect the url on the browser. If you are using ajax request, you would not see the change in the url. You may verify this if you are using html frames also.

It is mentioned in the documentation that this result type redirects the browser url and this does happen. Please refer to the document below:

http://struts.apache.org/2.0.14/docs/redirect-action-result.html

0
On

FWIW, I cannot recreate this behavior.

You may also want to DRY up your package configuration:

<package name="mobile" namespace="/mobile" extends="struts-default">
  <action name="login">
    <result>/mobile/login.jsp</result>
  </action>

  <action name="home">
    <result>/mobile/home.jsp</result>
  </action>

  <action name="Dologin" class="action.LoginAction">
    <result name="success" type="redirectAction">home</result>
    <result name="input" type="redirectAction">login</result>
  </action>
</package>