Java Struts 2:Action 之间的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/784527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:26:26  来源:igfitidea点击:

Struts 2: parameters between Actions

javajakarta-eeparametersstruts2struts-action

提问by

I have the following question: I need to pass a parameter (for example ID ) when I finish a form and the action save the form's values, this will forward to the result = "success" and I need that the action that will be call in the success come with the ID and other parameters to later use in the next form for save this information (info-form2 and info.form1)...

我有以下问题:当我完成表单并且操作保存表单的值时,我需要传递一个参数(例如 ID ),这将转发到 result = "success" 并且我需要将调用的操作成功后附带 ID 和其他参数,以便稍后在下一个表单中使用以保存此信息(info-form2 和 info.form1)...

for example:

例如:

FORM1 ( USER ) ==== "success" ====> FORM2 ( ADDRESS )

FORM1(用户)====“成功”====> FORM2(地址)

userForm.html ===================> addressForm.html?user_id=X ...(where X : Id passed throw of UserAction (method:save) to AddressAction (method:newAddress))

userForm.html ====================> addressForm.html?user_id=X ...(其中 X : Id 将 UserAction (method:save) 的 throw 传递给 AddressAction (方法:新地址))

Please I will appreciate your help

请我将感谢您的帮助

Thanks in advance

提前致谢

采纳答案by Barett

You used the word "forward" but it sounds like you want to go to a new page (address.html) to collect more information about the address. If this is the case, you need to redirect to the address page after the user action completes.

您使用了“转发”一词,但听起来您想转到新页面 (address.html) 以收集有关该地址的更多信息。如果是这种情况,您需要在用户操作完成后重定向到地址页面。

<action name="user" class="UserAction">
  <!-- Redirect to another namespace -->
  <!-- for Struts 2.2 --> <result type="redirectAction">
  <!-- for Struts 2.0 <result type="redirect-action"> -->
    <param name="actionName">collect-address</param>
    <param name="userId">${userId}</param>
  </result>
</action>

The ${userId} syntax will call getUserId on your UserAction and pass that parameter as you showed in your question: addressForm.html?user_id=X. collect-address can have a success result that goes to addressForm.html. Docs here.If you want to avoid using another action, you can try using the result type="redirect"and pass things through that way.

${userId} 语法将在您的 UserAction 上调用 getUserId 并传递您在问题中显示的参数:addressForm.html?user_id=X。collect-address 可以有一个成功的结果,它会转到 addressForm.html。文档在这里。如果您想避免使用其他操作,您可以尝试使用 result type="redirect"并通过这种方式传递内容。

If you really want to forward, you can use action chaining. This is discouraged by Ted Husted on the Struts2 teambut it may work for you.

如果你真的想转发,你可以使用action chainingStruts2 团队的 Ted Husted 不鼓励这样做,但它可能对你有用

Instead of action chaining, try to bring all the code to complete this request into a single action and use helper or service classes for User and Address to separate and reuse the code instead of "action chaining".

尝试将完成此请求的所有代码合并到单个操作中,而不是操作链接,并使用 User 和 Address 的帮助程序或服务类来分离和重用代码,而不是“操作链接”。

回答by Bhushan Bhangale

Not very clear what you want to do.

不是很清楚你想做什么。

Looks like after successfull execution of an action, the request gets forwarded to another action. In the first action you want to pass parameter ID and use that in the 2nd action. Since both the actions are getting used in the same request call you can save the ID parameter in request like this

看起来在成功执行一个动作后,请求被转发到另一个动作。在第一个操作中,您要传递参数 ID 并在第二个操作中使用它。由于这两个操作都在同一个请求调用中使用,因此您可以像这样在请求中保存 ID 参数

request.setAttribute("ID", iDValueObject);

request.setAttribute("ID", iDValueObject);

In the second action you can extract the value of ID like this

在第二个操作中,您可以像这样提取 ID 的值

request.getAttribute("ID");

request.getAttribute("ID");

回答by Rahul Saini

This should work:

这应该有效:

  <!--  Package Default -->
    <package name="**default**" extends="struts-default,json-default" namespace="/">
        <action name="noOp" class="com.web.myapp.action.NoOpAction">
           <result name="success" type="chain">
                    <param name="requiresValidation">true</param>
                    <param name="actionName">userAuthentication</param>
                    <param name="namespace">/user</param>
          </result>
        </action>   
    </package>
    <!--  Package User -->
    <package name="user" extends="struts-default,json-default" namespace="/user">
    <action name="userAuthentication" class="com.web.myapp.action.AuthenticateAction">
    ...
    </action>