java - Retaining values between multiple JSPs and Actions in Struts 2 -


my struts project structure follows: page1->action1->page2->action2->page3

what need value entered in input tag in page1 accessed in action2.

here 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 , 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> 

action2:

public class addform extends actionsupport {         private string txtfname; private string txtlname; private int txtage; private int txtid;       //getters , setters   @override public string execute() throws exception {      return "success"; } 

}

and displaying in

page3:

<div class = "container">     id: <s:property value = "txtid" /><br>     first name: <s:property value = "txtfname" /><br>     last name: <s:property value = "txtlname" /><br>     age: <s:property value = "txtage" /> </div> 

this face problem txtid displayed null, inferred value not passed page2 action2

a solution have come use

<s:hidden value = "%{txtid}" name = "txtid2 />  

in form in page2 allow me use value of txtid txtid2 in action2, seems more hack actual solution, other suggestions welcome.

in situation want keep field values passed 1 action configure scope of field. place same field getters , setters in each action, in case action1 , action2. field name txtid. scope interceptor doesn't include in defaultstack should reference in action configuration. example

<action name="action1" class="com.package.action.addid">     <result>/jsp/page2.jsp</result>     <interceptor-ref name="basicstack"/>     <interceptor-ref name="scope">         <param name="key">mykey</param>         <param name="session">txtid</param>         <param name="autocreatesession">true</param>     </interceptor-ref> </action> <action name="action2" class="com.package.action.addform">     <result>/jsp/page3.jsp</result>     <interceptor-ref name="scope">         <param name="key">mykey</param>         <param name="session">txtid</param>         <param name="autocreatesession">true</param>     </interceptor-ref>     <interceptor-ref name="basicstack"/> </action>  

now have scope key mykey , field txtid under it. providing accessors field in each action make transfer field value 1 action another. in example above used basicstack skeleton interceptor stack , not include interceptors including validation interceptor. if need have other features actions, should either construct custom stack or reference other interceptors in action configuration.


Comments

Popular posts from this blog

c++ - Linked List error when inserting for the last time -

java - activate/deactivate sonar maven plugin by profile? -

java - What is the difference between String. and String.this. ? -