Monday, 26 August 2013

retaining values between multiple jsp and action in struts2

retaining values between multiple jsp and action in struts2

My struts project structure is as follows: page1->action1->page2->action2.
What i need is for a value i entered in an input tag in page1 to be
accessed in action2.
here is my code:
page1:
<div class = "container">
<s:form id = "idinput" method = "post" action = "idEntered">
Enter id: <input id = "txtid" name = "txtid" type = "text" />
<input id = "cmdsubmit" name = "cmdsubmit" type = "submit" value =
"enter details" />
</s:form>
</div>
action1:
public class AddId extends ActionSupport {
private int txtid;
//getter and setter
@Override
public String execute() throws Exception {
return "success";
}
}
page2:
<div class = "container">
<s:form id = "formvalues" method = "post" action = "formEntered">
<p>Your id entered is: <s:property value = "txtid" /></p>
First name: <input id = "txtfname" name = "txtfname" type = "text" />
Last name: <input id = "txtlname" name = "txtlname" type = "text" />
Age: <input id = "txtage" name = "txtage" type = "text" />
<input id = "cmdform" name = "cmdform" type = "submit" value =
"submit form" />
</s:form>
</div>
and finally action2:
public class AddForm extends ActionSupport {
private String txtfname;
private String txtlname;
private int txtage;
private int txtid2;
//getters and setters
@Override
public String execute() throws Exception {
return "success";
}
}
a solution i have come up with is to use
<s:hidden value = "%{txtid}" name = "txtid2 />
in my form in page2 which will allow me to use the value of txtid as
txtid2 in action2, this however seems more like a hack than an actual
solution, so any other suggestions are welcome.
Thanks in advance!

No comments:

Post a Comment