Retaining values between multiple JSPs and Actions in Struts 2

瘦欲@ 提交于 2019-12-06 14:29:38

问题


My struts project structure is as follows: page1->action1->page2->action2->page3

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>

action2:

public class AddForm extends ActionSupport {    
    private String txtfname;
private String txtlname;
private int txtage;
private int txtid;
      //getters and setters 

@Override
public String execute() throws Exception {

    return "success";
}

}

and displaying everything 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 is where I face a problem as txtid is displayed as null, from which I inferred that the value is not passed from page2 to action2

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.


回答1:


In the situation where you want to keep the field values passed from one action to another you could configure the scope of the field. Just place the same field with getters and setters in each action, in your case it will be action1 and action2. The field name is txtid. As well as scope interceptor doesn't include in the defaultStack you should reference it in the action configuration. For 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 you have the scope with the key mykey and field txtid under it. Providing accessors to the field in each action will make transfer field value from one action to another. In the example above used the basicStack which is a skeleton for the interceptor stack and it does not include some interceptors including a validation interceptor. If you need to have other features to your actions, you should either construct a custom stack or reference other interceptors in the action configuration.



来源:https://stackoverflow.com/questions/18443956/retaining-values-between-multiple-jsps-and-actions-in-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!