Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass values between pages using Struts2

 
shah rah
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
shah rah
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic