Redirect default action in Struts 2

I have an action with an empty string for a name defined in the root namespace, and I want to redirect to this action from another action if a certain result is found, but it does not work.

Here's the default action

<action name="" class="com.example.actions.HomeAction"> <result name="success" type="freemarker">freemarker/home.ftl</result> </action> 

And I define the redirection in the global results for the package:

 <global-results> <result name="sendToRouting" type="redirectAction"> <param name="actionName"></param> <param name="namespace">/</param> </result> </global-results> 

I tried to take out the actionName parameter, but this does not work. If I put a name for HomeAction and refer to it by name in the global results, this works, so I assume that the problem is that there is no action name for the redirect.

Any thoughts?

+4
source share
2 answers

I think you want to use <default-action-ref /> :

 <package name="home" namespace="/" extends="struts-default"> <default-action-ref name="home" /> <action name="home" class="com.example.actions.HomeAction"> <result name="success" type="freemarker">freemarker/home.ftl</result> </action> </package> 

Sorry ... wrong question:

Try changing type="redirectAction" to type="redirect" , I'm sure that redirectAction is now being redirected.

+11
source

Did you get a NullPointerException because the actionName parameter is empty? I hacked this by providing a string that evaluates to an empty string:

 <param name="actionName">${}</param> 

Still looking for a more β€œright” solution, though ...

+3
source

Source: https://habr.com/ru/post/1300619/


All Articles