Monicka Akilan
Monicka Akilan

Reputation: 1549

how to get one form value inside another form action in spring mvc

Am using spring mvc i have two forms in a single jsp version am using is 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 

}

Things i tried.

Kept one hidden field in form2 holding form1 object and i can access that. Or setting that form1 value in session and i can access that

But i want to know the best way to do this in spring mvc

Upvotes: 1

Views: 4439

Answers (1)

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

You can combine this two form together something like this

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

Controllers

@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();
}

hope this will solve your problem

Upvotes: 2

Related Questions