Struts2重定向到具有未知数量参数的另一个动作 [英] Struts2 redirecting to another action with unknown amount of parameters

查看:137
本文介绍了Struts2重定向到具有未知数量参数的另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录操作,在成功执行后重定向到上一页(我将上一页存储在我的会话中,以便稍后可以获取)。在Struts2中,我可以找到两种方法来执行此重定向:

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>

在这个例子中,getPreviousAction()。getRequestURL()方法(这是一个自制方法,它的不会调用struts2)这将返回上一页的url,例如:

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>

我想使用这种redirectaction结果类型,因为它更干净。但是,当查询参数是url的一部分时,我遇到了问题。例如:

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

我知道我可以在我的struts.xml中添加这些硬编码的params,但问题是我的登录操作应该重定向到以前调用的任何动作,而我事先不知道先前的操作有哪些查询参数。这与典型的用例不同,在那里你确切地知道你要重定向到哪个动作

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

我发现一个非常糟糕的解决方案是添加每个param(所有的集合)我在struts.xml中的所有操作的参数,然后使用选项:

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>


推荐答案

您可以保存操作名称,命名空间和参数 ActionMapping

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();

要动态添加参数 redirectAction 结果你应该在动态参数中使用OGNL。

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.

这篇关于Struts2重定向到具有未知数量参数的另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆