1

我的Struts项目结构如下:

page1-> action1-> page2-> action2->page3

我需要的是我在输入标签中输入的值,page1以便在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>

行动1:

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>

行动2:

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,从中我推断该值未从传递page2action2

我想出的一个解决方案是使用

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

在我的形式中page2,我可以使用txtidas txtid2in的值action2,但这似乎更像是一种 hack,而不是实际的解决方案,所以欢迎任何其他建议。

4

1 回答 1

3

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

例如:

<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它是拦截器堆栈的骨架,它不包括一些拦截器,包括validation拦截器

如果您需要为您的操作添加其他功能,您应该构建自定义堆栈或在操作配置中引用其他拦截器。

于 2013-08-27T10:16:55.877 回答