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

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

问题描述

我有两个ModelAttributes的表单,一个是公民,另一个是惩罚。这两个对象由jquery选项卡分隔。我在获取表单上的项目时出现问题,显示正确,有些正在显示,有些则没有。我的意思是html元素。



我不确定Controller在页面上有多个ModleAttributes时的外观。 Under是代码示例:

Page

 < title>公民注册< / title> 

< / head>

< body>
< div id =tabs>

< ul>
< li>< a href =#tab1>公民注册< / a>< / li>
< li>< a href =#tab2>惩罚< / a>< / li>
< / ul>


< div id =tab1>
< form:form id =citizenRegistrationname =citizenRegistrationmethod =postmodelAttribute =citizensaction =citizen_registration.htm>
< div id =divRightclass =mainDiv>
< div class =divGroupid =divCharInfo>
< fieldset>
< legend>特征信息< / legend>
< ol>
< li>< form:label for =photopath =photo>选择照片< / form:label>

< li>
< label>选择性别< / label>
< form:options items =$ {gender.genderList}itemValue =genderIditemLabel =genderDesc/>
< / form:select>
< form:errors path =genderIdclass =errors/>
< / li>
$ b $ lt; li>< form:label for =weightpath =weight> Enter Weight< i>(lbs)< / i>< / form:label>
< form:input path =weightid =weighttitle =Enter Weight/>< form:errors path =weightid =errors/>
< / li>

< li>< form:label for =heightpath =height> Enter Height< i>(feet)< / i>< / form:label>
< form:input path =heightid =heighttitle =Enter Height/>< form:errors path =heightid =errors/>
< / li>
.......................


< div id =tab2>
< form:form id =punishmentRegistrationname =punishmentRegistrationmethod =postmodelAttribute =punishmentaction =punishment_registration.htm>


    < li>
    < form:label for =punishmentIdpath =punishmentId>处罚编号< / form:label>
    < form:input path =punishmentIdid =punishmentId/>< form:errors path =punishmentIdid =errors/>
    < / li>

    < li>
    < form:label for =crimeRecNopath =crimeRecNo>选择犯罪< / form:label>
    < / form:select>
    < form:errors path =crimeRecNoid =errors/>
    < / li>

    < li>
    < form:label for =monitoringStDatepath =monitoringStDate>开始日期< / form:label>
    < form:input path =monitoringStDateid =monitoringStDate/>< form:errors path =monitoringStDateid =errors/>
    < / li>


    < li>
    < form:label for =monitoringEnDatepath =monitoringEnDate>结束日期< / form:label>
    < form:input path =monitoringEnDateid =monitoringEnDate/>< form:errors path =monitoringEnDateid =errors/>
    < / li>
    < / ol>

    < / form:form>
    < / div>

    < / div>

    < / body>
    < / html>

控制器

  @RequestMapping(value =citizen_registration.htm,method = RequestMethod.GET)
public ModelAndView loadPage(HttpServletRequest请求,
HttpServletResponse响应,
@ModelAttribute公民公民,@ModelAttribute处罚惩罚,
BindingResult结果,
ModelMap m,模型模型)抛出异常{

//这里的代码

返回新的ModelAndView(citizen_registration);

这是我的代码,但是当我运行它时,tab2中的任何内容都不显示,并且不显示tab1中的所有元素。

解决方案

我不这么认为,如果您可以使用Spring表单绑定多个模型。事实上,你应该看看春季装订表格。
http://static.springsource.org /spring/docs/1.1.5/taglib/tag/BindTag.html
查看示例代码。我没有测试过这些代码。让我们知道有任何问题。



模型



  public class用户{

私人字符串用户名;
私人字符串密码;

..设置者和获取者
}

public class UserProfile {
private String firstName;
private String lastName;

setter and getter
}



控制器



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ModelMap map){
User user = new User(); //会推荐使用spring容器来创建对象
UserProfile profile = new UserProfile();

map.addAttribute('user',user);
map.addAttribute('profile',profile);

returnform
}




@RequestMapping(....)
public String newAccountForm(@ModelAttrbite('User')用户用户,BindingResult resultUser,@ModelAttribute('UserProfile')UserProfile userProfile,BindingResult resultProfile){

//检查每个模型的绑定结果。如果无效,请再次返回表单页面
//根据需要进一步处理。


$ b $ / code $ / pre
$ b

JSP



 <%@ taglib uri =http://www.springframework.org/tagsprefix =spring> 

< form action =method =post>

< spring:bind path =user.username>
< input type =textname =$ {status.expression}value =$ {status.value}>< br />
< / spring:bind>

< spring:bind path =user.password>
< input type =passwordname =$ {status.expression}value =$ {status.value}>< br />
< / spring:bind>

< spring:bind path =profile.firstName>
< input type =textname =$ {status.expression}value =$ {status.value}>< br />
< / spring:bind>
< spring:bind path =profile.lastName>
< input type =textname =$ {status.expression}value =$ {status.value}>< br />
< / spring:bind>

< input type =submitvalue =Create/>
< / form>


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆