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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "pass values between pages using Struts2" Watch "pass values between pages using Struts2" New topic

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





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

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


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:
subject: pass values between pages using Struts2
It's not a secret anymore!