如何在spring mvc中的另一个表单动作中获取一个表单值

时间:2013-12-06 04:45:07

标签: java spring spring-mvc spring-annotations

使用spring mvc我在单个jsp版本中有两种形式,使用的是spring 3.1.2

<form:form method="get" commandName="command1" action="form1.htm">
</form:form>   

<form:form method="get" commandName="command2" action="form2.htm">
    <form:submit value='Save'>
    </form:form>

@RequestMapping(value = "/form1", method = RequestMethod.GET, params = "Get")
            public String method1(
                    @ModelAttribute("command1") Object1 object1 {


        }

@RequestMapping(value = "/form2", method = RequestMethod.GET, params = "Get")
    public String method2(
            @ModelAttribute("command2") Object2 object2 {
    // Here i want to access object1 how to do this 

}

我尝试过的事情。

在form2中保留一个隐藏字段,保存form1对象,我可以访问它。 或者在会话中设置form1值,我可以访问

但我想知道在春季mvc

中做到这一点的最好方法

1 个答案:

答案 0 :(得分:2)

您可以将这两种形式组合在一起,就像这样

您的JSP

<form:form method="get" commandName="command1" action="form1.htm">
    <!-- field of form 1 -->
    <form:input path="frm1Field1" />
    <form:form method="get" commandName="command2" action="form2.htm">
        <!-- field of form 2 -->    
        <form:input path="frm2Field1" />
        <form:input path="frm2Field2" />
        <input type="submit" value="Save"/>
    </form:form>
    <input type="submit" value="Save"/>
</form:form>

Criteria Clasess

public class Form2 {
private String frm2Field1;
private String frm2Field2;
//setter & getter methods
}


public class Form1 extends Form2 {
private String  frm1Field1;
//setter & getter methods
}

控制器

@RequestMapping(value = "/form1", method = RequestMethod.GET, params = "Get")
public String method1(@ModelAttribute("command1")Form1 form1){
form1.getFrm1Field1();
//access value of for2 
form1.getFrm2Field1();
form1.getFrm2Field2();
}

@RequestMapping(value = "/form2", method = RequestMethod.GET, params = "Get")
public String method2(@ModelAttribute("command2")Form2 form2) {
form2.getFrm2Field1();
form2.getFrm2Field2();
}

希望这能解决您的问题