Spring MVC多个ModelAttribute在同一个表单上
Posted
技术标签:
【中文标题】Spring MVC多个ModelAttribute在同一个表单上【英文标题】:Spring MVC Multiple ModelAttribute On the Same Form 【发布时间】:2012-10-25 21:30:17 【问题描述】:我有一个带有两个 ModelAttributes 的表单,一个是公民,另一个是惩罚。这两个对象由 jquery 选项卡分隔。我在让表单上的项目正确显示时遇到问题,有些正在显示,有些则没有。我的意思是html元素。
我不确定当页面上有多个 ModleAttributes 时控制器的外观。下面是代码示例:
页面
<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>
控制器
@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");
这是我的代码,但是当我运行它时,tab2 中没有显示任何内容,也没有显示 tab1 中的所有元素。
【问题讨论】:
【参考方案1】:<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
);
);
【讨论】:
一些额外的解释可能会有所帮助。【参考方案2】:我已经在这个链接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>
【讨论】:
【参考方案3】:如果您可以使用 Spring 表单绑定多个模型,我不这么认为。事实上,你应该看看 spring 绑定表单。 http://static.springsource.org/spring/docs/1.1.5/taglib/tag/BindTag.html 查看示例代码。我没有测试过代码。如有任何问题请告知。
型号
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
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 的新手,我想使用开发和维护效率最高的东西,因为如果我使用组合类并且我想查询表单甚至更新,我会看到问题以上是关于Spring MVC多个ModelAttribute在同一个表单上的主要内容,如果未能解决你的问题,请参考以下文章