Struts 2: parameters between Actions
Asked Answered
C

3

8

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

for example:

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

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

Please I will appreciate your help

Thanks in advance

Compass answered 24/4, 2009 at 4:29 Comment(0)
C
29

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>

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.

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.

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

Cyclops answered 24/4, 2009 at 19:26 Comment(5)
Barett, I test your solution how I search in books and the struts specifications and I don't have success... in the form ..when I press SUBMIT ..the user is saved by the method ("save" of action: UserAction), and I put this.id= user.getId(); before the "SUCCESS" and the same sentences like you put above and in the next action the param id appear blank or when I put request.getParameter("id") it comes null.... :( do you have some approach to my problem ? ...I will appreciate too much....Compass
...other thought to my problem is: I m getting the id value later of save the object USER ...this id is generated by hibernate to the object and later this object will have the id generated.... but I need this ID generated to pass to the other action ...but it seems like the ID isnt generated while the method SAVE is running.... :S ... any helps I will be happy!Compass
I have the same problem.. it appears blank or null when I try to pass the variable this way.. if, instead, I try <param name="userId">1111</param> then it worksAlbeit
Hi guys, thanks for the upvotes. There's not enough information here to help you guys out more specifically... :( Most likely scenario, make sure your UserAction class has a "String getUserId()" method, with no overrides.Cyclops
as a matter fact, @Barett, there are more information about my problem here, and also, the solution that worked for me: #14214467Albeit
L
3

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

request.setAttribute("ID", iDValueObject);

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

request.getAttribute("ID");

Leaflet answered 24/4, 2009 at 5:52 Comment(1)
If you're using struts2, you should try to implement the ParameterAware and use it :DSociety
J
0

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>
Jodyjoe answered 19/7, 2011 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.