Spring MVC Multiple ModelAttribute On the Same Form

前端 未结 3 1984
余生分开走
余生分开走 2020-11-29 01:44

I have a form with two ModelAttributes one is citizens and the other is punishment. The two objects are separated by jquery tabs. I am having problems in getting the items o

相关标签:
3条回答
  • 2020-11-29 02:09

    I don't think so if you can bind multiple models using the Spring form. In fact you should take a look in the spring binding form. http://static.springsource.org/spring/docs/1.1.5/taglib/tag/BindTag.html Take a look in the sample code. I have not tested the code. Let know in case of any issues.

    Model

    public class User{
    
    private String username;
    private String password;
    
    ..Setter and Getters
    }
    
    public class UserProfile{
    private String firstName;
    private String lastName;
    
    setter and getter
    }
    

    Controller

    @Controller
    public class MyController{
        @RequestMapping(....)
        public String newAccountForm(ModelMap map){
            User user = new User(); //Would recommend using spring container to create objects
            UserProfile profile = new UserProfile();
    
            map.addAttribute('user', user);
            map.addAttribute('profile', profile);
    
            return "form"
        }
    
    
    
    
         @RequestMapping(....)
            public String newAccountForm(@ModelAttrbite('User')User user, BindingResult resultUser, @ModelAttribute('UserProfile')UserProfile userProfile, BindingResult resultProfile){
    
    //Check the binding result for each model. If not valid return the form page again
    //Further processing as required.
    
            }
        }
    

    JSP

    <%@taglib  uri="http://www.springframework.org/tags" prefix="spring">
    
    <form action="" method="post">
    
    <spring:bind path="user.username">
       <input type="text" name="${status.expression}" value="${status.value}"><br />
            </spring:bind>
    
    <spring:bind path="user.password">
       <input type="password" name="${status.expression}" value="${status.value}"><br />
            </spring:bind>
    
    <spring:bind path="profile.firstName">
       <input type="text" name="${status.expression}" value="${status.value}"><br />
            </spring:bind>
    <spring:bind path="profile.lastName">
       <input type="text" name="${status.expression}" value="${status.value}"><br />
            </spring:bind>
    
    <input type="submit" value="Create"/>
    </form>
    
    0 讨论(0)
  • 2020-11-29 02:11

    I already gave alternate approach in this link here

    <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>
    
    0 讨论(0)
  • 2020-11-29 02:27
    <script type="text/javascript">
    $(document).ready(
            function() {
                $("#userAttendance").submit(
                        function(e) {
                            e.preventDefault();
                            jQuery.ajaxSetup({
                                async : false
                            });
                            var a = "${pageContext.request.contextPath}";
                            alert(a);
                            $.post($(this).attr("action"), $(this).serialize(),
                                    function(response) {
                                        $("#showTableByDate").html(response);
                                        jQuery.ajaxSetup({
                                            async : true
                                        });
                                    });
                        });
                //Date picker
                $('#datepicker').datepicker({
                    autoclose : true
                });
            });
    

    0 讨论(0)
提交回复
热议问题