Struts2 redirecting to another action with unknown amount of parameters

跟風遠走 提交于 2019-11-29 15:47:23

问题


I have a login action which after succesful execution redirects to the previous page (I store the previous page in my session so I can fetch it later). In Struts2, I can find two ways to do this redirection:

    <action name="login" class="com.myapp.login.Login">
        <result name="redirect" type="redirect">${previousAction.requestURL}</result>   
    </action>

In this example, the getPreviousAction().getRequestURL() method (this is a selfmade method, its not ntive to struts2) will be invoked and this will return the url of the previous page as intended, for example:

somenamespace/index.action

There is also another type of redirection:

<action name="login" class="com.myapp.login.Login">
     <result type="redirectAction">
        <param name="actionName">${previousAction.name}</param>
        <param name="namespace">/${previousAction.namespace}</param>
    </result>   
</action>

I want to use this redirectaction result type because it is much cleaner. But, I have a problem when query parameters are part of the url. For example:

somenamespace/index.action?name=john&age=50

I know I can add these params hardcoded in my struts.xml, but the problem is my login action should redirect to ANY previously invoked action, and I do not know beforehand which query parameters the previous actions had. This is different from the typical usecase where you know exactly to which action you're redirecting to

A very bad solution I found was adding EVERY param possible (the collection of all params of all my actions in struts.xml) and then use the option:

<param name="suppressEmptyParameters">true</param>

回答1:


You can save action name, namespace, and parameters from the ActionMapping.

ActionMapping mapping = ServletActionContext.getActionMapping();

You can also save query string instead of parameter map.

String params = request.getQueryString();

To add parameters dynamically to redirectAction result you should use OGNL in a dynamic parameter.

<param name="actionName">${previousAction.name +'?'+ parameters}</param>

Supposed you have a getter for parameters and initialized it from session where you saved previous query string, action name, and namespace.



来源:https://stackoverflow.com/questions/24241159/struts2-redirecting-to-another-action-with-unknown-amount-of-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!