保留Struts 2中多个JSP和Actions之间的值 [英] Retaining values between multiple JSPs and Actions in Struts 2

查看:95
本文介绍了保留Struts 2中多个JSP和Actions之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的struts项目结构如下:
page1 - > action1 - > page2 - > action2 - > page3

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

我需要的是我在第1页的输入标签中输入的值,以便在action2中访问。

What i need is for a value i entered in an input tag in page1 to be accessed in action2.

这是我的代码:

第1页:

<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";
}   

}

第2页

<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";
}

}

并显示所有内容

第3页:

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

这是我遇到问题的地方 txtid 显示为 null ,我从中推断出该值未从 page2 传递到 action2

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 /> 


我的表单中的

page2 中,这将允许我使用 txtid 的值作为 txtid2 action2 中,这看起来更像是一个黑客而非实际的解决方案,所以欢迎任何其他建议。

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.

推荐答案

在您希望将字段值从一个操作传递到另一个操作的情况下,您可以配置字段的范围。在每个操作中使用getter和setter放置相同的字段,在您的情况下,它将是 action1 action2 。 name是 txtid 以及范围拦截器不包含在 defaultStack 中,您应该在操作配置中引用它。例如

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> 

现在你的关键字范围是 mykey 和字段 txtid 在它下面。在每个操作中为字段提供访问者将使传输字段值从一个操作到另一个操作。在上面的例子中使用 basicStack 这是拦截器堆栈的骨架,它不包括一些拦截器,包括验证拦截器。如果您需要为操作提供其他功能,则应在操作配置中构建自定义堆栈或引用其他拦截器。

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.

这篇关于保留Struts 2中多个JSP和Actions之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆