Skip to content
Advertisement

How to use the entire wildcard value as a redirect result in Struts 2

I am wondering how I can use the Struts2 wildcard feature to redirect users to another action based on the return value:

struts.xml:

<action name="menu" class="a.b.c.d.e.f.actions.SecureMenuAction" method="prompt" >
    <result name="success" type="tiles">.clf.sm</result>
    <result name="input" type="tiles">.clf.smLevel3</result>
    <result name="*" type="redirectAction">{1}</result>
</action>

Action:

String redirectString;
// --- code --- \
return redirectString;

In some cases, return result SUCCESS or INPUT is valid, but in all other cases I want the exact string that I return be used as the redirect location.

If I replace my struts.xml to:

<result name="test*" type="redirectAction">{1}</result>

Then the {1} will be replaced with test, followed by the correct action I want the user directed to.

However, if I simply use * as my result name, then it will not replace the {1} at all (as if the wildcard feature cannot be used like this).


Does anyone know a way I can get this to work using what’s available in Struts2 (2.3.16.2)?

Advertisement

Answer

Result name "*" is not a wildcard. It’s a special meaning for other result name which is taken if no matching result name if given.

If you want to redirect action then you should provide a getter for action name and use dynamic parameter in the result config.

<action name="menu" class="a.b.c.d.e.f.actions.SecureMenuAction" method="prompt" >
    <result name="success" type="tiles">.clf.sm</result>
    <result name="input" type="tiles">.clf.smLevel3</result>
    <result name="*" type="redirectAction">${redirectString}</result>
</action>

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement