This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes pass values between pages using Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "pass values between pages using Struts2" Watch "pass values between pages using Struts2" New topic
Author

pass values between pages using Struts2

shah rah
Ranch Hand

Joined: Jan 04, 2007
Posts: 124
I have a register page with 2 fields and submit button. Once submitted I want to show a success page with paramaters entered in the prev page.

Very Simple but I am not clear how to do this with STRUTS2

ACTION CLASS --




JSP


Register.jsp


Sucess.jsp


I am getting message Thanks for registering: but not the value of the firstname. In old versions of struts
it would be setting a Request attribute and getting value from it on the JSP page. I am not sure how it is done in STRUTS2

appreciate reply.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please put your code inside code tags so it's legible.

Actions are instantiated per-request. If you don't save the user's name anywhere or pass it on to the next action there's no way for the next action to have that value. Not knowing how you've configured your result makes it harder to help.
shah rah
Ranch Hand

Joined: Jan 04, 2007
Posts: 124
How to pass the firstname entered on the form to my success page.

In struts 2 examples. I haven't seen anyone using request.getParameter("firstname") to get form values or use request.setAttribute("fname",firstname) to pass values to another page.

can some one direct me on how to get form field values from my jsp page in STRUTS2?

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

In struts 2 provide better facility than older version.

You can get variable using more than one style.

you can also use request for get it by just implement ServletRequestAware and Add unimplemented method.


Or you can directly use or define one String variable which name is match to your jsp page firstname tag like

<s:textfield cssClass="textfield" theme="simple" name="firsttName" />

then declare

private String firsttName;

and use getter and setter method for firsttName in Action you can directly get the value of firsttName.

so no need to take request object.

because Struts 2 place all the Action variable at OGNL value statck So, you can get it directly.




Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
shah rah
Ranch Hand

Joined: Jan 04, 2007
Posts: 124
If it is a simple form field like this it is working fine.



but If I have a java bean I am unable to pass value to next page. I want to know how to pass complex objects? Appreciate your reply.


shah rah
Ranch Hand

Joined: Jan 04, 2007
Posts: 124
Finally I got it. Looks like there was a problem with names of the field and getter and setter names. I fixed the code on my first message too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass values between pages using Struts2
 
Similar Threads
Retrieving request parameters (from the url) in a jsp page
struts2: what kicks in first- the interceptors or the form's validation?
problem getting list in jsp page in struts2
Populating a list of Objects in Struts2
Interceptor messes up action class in Struts2