Struts 2:Action之间的参数

Struts 2: parameters between Actions(Struts 2:Action之间的参数)

本文介绍了Struts 2:Action之间的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)...

例如:

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

userForm.html ===================> addressForm.html?user_id=X ...(其中 X : Id 传递了 UserAction (方法:save) 到 AddressAction (method:newAddress))

感谢您的帮助

提前致谢

推荐答案

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

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.

<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>

${userId} 语法将在您的 UserAction 上调用 getUserId 并传递您在问题中显示的该参数:addressForm.html?user_id=X.collect-address 可以将成功结果发送到 addressForm.html.Docs here. 如果您想避免使用其他操作,您可以尝试 使用结果 type="redirect" 和通过这种方式传递东西.

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.

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

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

尝试将完成此请求的所有代码放入单个操作中,并使用 User 和 Address 的帮助器或服务类来分离和重用代码,而不是操作链接".

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".

这篇关于Struts 2:Action之间的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Struts 2:Action之间的参数

基础教程推荐