Multiple modelattributes in a JSP with Spring
Asked Answered
G

3

2

What I am trying to accomplish is using one single form, submit multiple modelattributes with Spring and JSP.

With one I know how to do it, mapping the model with the tag form:form.

But, if I want to get two modelAttributes in the controller method with the annotations @ModelAttribute how should I do it? Is it even possible? I am aware this is not so common, but I would like to know if it is possible.

Gazehound answered 23/2, 2012 at 11:59 Comment(0)
O
4

AFAIK, you can only bind a form with a single object. If you have two classes, then you can create a single class that references the other two classes and then bind with that class.

Oid answered 24/2, 2012 at 11:29 Comment(0)
F
0

Yup, im agree with nickdos. Btw, dont forget to put those two classes name in path:

example :

<spring:bind path="user.status">
<appfuse:label styleClass="control-label" key="id.user.maritalStatus"/>
<form:input path="user.status" cssClass="form-control" id="status" />
</spring:bind>
Federalist answered 28/4, 2014 at 4:28 Comment(0)
T
0

Just add a nested Jsp form with a single submit button example see below

<form:form method="POST" modelAttribute="applicationGeneralInformation">
  <div class="section2">
    <h2>General Informaion</h2>

    <form:input type="hidden" path="id" id="id"/>
    <label for="app_version">Version</label>: <form:input type="text" id="app_version" path="version"/><br/>
    <label for="app_func_desc">Description</label>: <form:input type="text" id="app_func_desc"
                                                                           path="functionalDescription"/><br/>
    <label for="app_sec_func">Functions</label>: <form:input type="text" id="app_sec_func"
                                                                      path="securityFunctions"/><br/>

</div>
<div class="section2">
    <h2>Application Content</h2>
    <form:form method="POST" modelAttribute="applicationContent">
        <div>
            <h3>CIA Rating</h3>
            <label for="CIARating">CIA Rating</label>: <form:select type="text" id="CIARating" path="CIARating">
            <form:option value="1">1</form:option>
            <form:option value="2">2</form:option>
            <form:option value="3">3</form:option>
            <form:option value="4">4</form:option>
        </form:select><br/><br/>
        </div>
        <div>
            <h3>Business Continuity and Disaster Recovery</h3>
            <div>
                <h4>RTO</h4>
                <label for="RTO">RTO</label>: <form:select type="text" id="RTO" path="RTO">
                <form:option value="1">< 2<sub>Hrs</sub></form:option>
                <form:option value="2">2<sub>Hrs</sub>-4<sub>Hrs</sub></form:option>
                <form:option value="3">4<sub>Hrs</sub>-48<sub>Hrs</sub></form:option>
                <form:option value="4">> 48<sub>Hrs</sub></form:option>
            </form:select><br/>
            </div>
            <div>
                <h4>RPO</h4>
                <label for="RPO">RPO</label>: <form:input type="text" id="RPO" path="RPO"/><br/>
            </div>
        </div>
    </form:form>
    <input type="submit" value="Submit">
  </div>
</form:form>
Translucid answered 3/2, 2017 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.