Spring MVC 多个 ModelAttribute 在同一表单上 [英] Spring MVC Multiple ModelAttribute On the Same Form

查看:27
本文介绍了Spring MVC 多个 ModelAttribute 在同一表单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个模型属性的表单,一个是公民,另一个是惩罚.这两个对象由 jquery 选项卡分隔.我在使表单上的项目正确显示时遇到问题,有些正在显示,有些则没有.我的意思是 html 元素.

当页面上有多个 ModelAttributes 时,我不确定控制器的外观.下面是代码示例:

页面

公民注册<身体><div id="标签"><ul><li><a href="#tab1">公民注册</a></li><li><a href="#tab2">惩罚</a></li><div id="tab1"><form:form id="citizenRegistration" name ="citizenRegistration" method="post" modelAttribute="citizens" action="citizen_registration.htm"><div id="divRight" class="mainDiv"><div class="divGroup" id="divCharInfo"><字段集><legend>特性信息</legend><ol><li><form:label for="photo" path="photo">选择照片</form:label><form:input path="photo" type="file" id="photo" title="上传照片"/><form:errors path="photo" id="errors"/></li><li><label>选择性别</label><form:select path="genderId" id="genderId" title="选择您的性别"><form:options items = "${gender.genderList}" itemValue="genderId" itemLabel="genderDesc"/></form:select><form:errors path="genderId" class="errors"/><li><form:label for="weight" path="weight">输入重量<i>(lbs)</i></form:label><form:input path="weight" id="weight" title="输入重量"/><form:errors path="weight" id="errors"/><li><form:label for="height" path="height">输入高度<i>(英尺)</i></form:label><form:input path="height" id="height" title="输入高度"/><form:errors path="height" id="errors"/>……………………<div id="tab2"><form:form id="punishmentRegistration" name ="punishmentRegistration" method="post" modelAttribute="punishment" action="punishment_registration.htm"><ol><li><form:label for ="punishmentId" path="punishmentId">惩罚编号</form:label><form:input path="punishmentId" id="punishmentId"/><form:errors path="punishmentId" id="errors"/><li><form:label for="crimeRecNo" path="crimeRecNo">选择犯罪</form:label><form:select path="crimeRecNo" id="CrimeRecNo" title="选择犯罪"><form:options items = "${crime.crimeList}" itemValue="crimeRecNo" itemLabel="crimeRecNo" title="crimeDesc"/></form:select><form:errors path="crimeRecNo" id="errors"/><li><form:label for ="monitoringStDate" path="monitoringStDate">开始日期</form:label><form:input path="monitoringStDate" id="monitoringStDate"/><form:errors path="monitoringStDate" id="errors"/><li><form:label for ="monitoringEnDate" path="monitoringEnDate">结束日期</form:label><form:input path="monitoringEnDate" id="monitoringEnDate"/><form:errors path="monitoringEnDate" id="errors"/></ol></form:form>

控制器

@RequestMapping(value="citizen_registration.htm", method = RequestMethod.GET)公共 ModelAndView loadPage(HttpServletRequest 请求,HttpServletResponse 响应,@ModelAttribute Citizens 公民,@ModelAttribute 惩罚惩罚,BindingResult 结果,ModelMap m, 模型模型) 抛出异常 {//代码在这里return new ModelAndView("citizen_registration");

这是我的代码,但是当我运行它时,没有显示 tab2 中的任何内容,也没有显示 tab1 中的所有元素.

解决方案

如果您可以使用 Spring 表单绑定多个模型,我不这么认为.事实上,你应该看看弹簧装订的形式.http://static.springsource.org/spring/docs/1.1.5/taglib/tag/BindTag.html查看示例代码.我没有测试代码.如有任何问题,请告知.

型号

公共类用户{私人字符串用户名;私人字符串密码;..Setter 和 Getter}公共类用户配置文件{私人字符串名字;私人字符串姓氏;setter 和 getter}

控制器

@Controller公共类 MyController{@RequestMapping(....)public String newAccountForm(ModelMap map){用户用户 = 新用户();//推荐使用spring容器来创建对象UserProfile profile = new UserProfile();map.addAttribute('用户', 用户);map.addAttribute('profile', profile);返回形式"}@RequestMapping(....)public String newAccountForm(@ModelAttrbite('User')User user, BindingResult resultUser, @ModelAttribute('UserProfile')UserProfile userProfile, BindingResult resultProfile){//检查每个模型的绑定结果.如果无效,请再次返回表单页面//根据需要进一步处理.}}

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><输入类型=提交"值=创建"/></表单>

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 on the form to display properly some are being displayed and some are not. I mean the html elements.

I am not sure how the Controller would look when there is multiple ModleAttributes on the page. Under is a sample of the code:

Page

<title>Citizen Registration</title>

    </head>

    <body>  
        <div id="tabs"> 

        <ul>
            <li><a href="#tab1">Citizen Registration</a></li>
            <li><a href="#tab2">Punishment</a></li>
        </ul>


        <div id="tab1">
            <form:form id="citizenRegistration" name ="citizenRegistration" method="post" modelAttribute="citizens" action="citizen_registration.htm">
                <div id="divRight" class="mainDiv">             
                    <div class="divGroup" id="divCharInfo"> 
                    <fieldset>  
                    <legend>Characteristics Info</legend>
                            <ol>    
                                <li><form:label for="photo" path="photo">Select Photo</form:label>
                                    <form:input path="photo" type="file" id="photo" title="Upload a photo"/><form:errors path="photo" id="errors"/></li>

                                <li>
                                    <label>Select Gender</label>
                                    <form:select path="genderId" id="genderId" title="Select Your Gender">
                                    <form:options items = "${gender.genderList}" itemValue="genderId" itemLabel="genderDesc" />
                                    </form:select>
                                    <form:errors path="genderId" class="errors"/>
                                </li>               

                                <li><form:label for="weight" path="weight">Enter Weight <i>(lbs)</i></form:label>
                                    <form:input path="weight" id="weight" title="Enter Weight"/><form:errors path="weight" id="errors"/>
                                </li> 

                                <li><form:label for="height" path="height">Enter Height <i>(feet)</i></form:label>
                                    <form:input path="height" id="height" title="Enter Height"/><form:errors path="height" id="errors"/>
                                </li> 
                                                .......................


            <div id="tab2">
                <form:form id="punishmentRegistration" name ="punishmentRegistration" method="post" modelAttribute="punishment" action="punishment_registration.htm">

                <ol>
                    <li>
                        <form:label for ="punishmentId" path="punishmentId">Punishment Number</form:label>
                        <form:input path="punishmentId" id="punishmentId"/><form:errors path="punishmentId" id="errors"/>                   
                    </li>

                    <li>
                        <form:label for="crimeRecNo" path="crimeRecNo">Select Crime</form:label>
                        <form:select path="crimeRecNo" id="CrimeRecNo" title="Select Crime">
                        <form:options items = "${crime.crimeList}" itemValue="crimeRecNo" itemLabel="crimeRecNo" title="crimeDesc"/>
                        </form:select>
                        <form:errors path="crimeRecNo" id="errors"/>
                    </li>   

                    <li>
                        <form:label for ="monitoringStDate" path="monitoringStDate"> Start Date </form:label>
                        <form:input path="monitoringStDate" id="monitoringStDate"/><form:errors path="monitoringStDate" id="errors"/>                   
                    </li>


                    <li>
                        <form:label for ="monitoringEnDate" path="monitoringEnDate"> End Date </form:label>
                        <form:input path="monitoringEnDate" id="monitoringEnDate"/><form:errors path="monitoringEnDate" id="errors"/>                   
                    </li>                   
                </ol>               

                </form:form>            
            </div>                      

        </div>

    </body>
</html>

Controller

@RequestMapping(value="citizen_registration.htm", method = RequestMethod.GET)
    public ModelAndView loadPage(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @ModelAttribute Citizens citizens, @ModelAttribute Punishment punishment,
                                 BindingResult result,
                                 ModelMap m, Model model) throws Exception {

//code here

return new ModelAndView("citizen_registration");

This is my code however when i run it nothing in tab2 is displayed andnot all elements in tab1 is shown.

解决方案

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>

这篇关于Spring MVC 多个 ModelAttribute 在同一表单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆