@ModelAttributeSpring MVC 的目的和用途是什么?
在Spring 3中,我在jsp中的form标签中看到了两个不同的属性
<form:form method="post" modelAttribute="login">
Run Code Online (Sandbox Code Playgroud)
在此属性modelAttribute是表单对象的名称,其属性用于填充表单.我用它来发布一个表单,在控制器中,我习惯于@ModelAttribute捕获值,调用验证器,应用业务逻辑.这里一切都很好.现在
<form:form method="post" commandName="login">
Run Code Online (Sandbox Code Playgroud)
这个属性的期望是,它还是一个表单对象,我们将填充它们的属性?
我正从控制器重定向到另一个控制器.但我还需要将模型属性传递给第二个控制器.
我不想把模型放在会话中.
请帮忙.
我有一个有两个ModelAttributes的表单,一个是公民,另一个是惩罚.这两个对象由jquery选项卡分隔.我在获取正确显示表单上的项目时遇到问题,有些正在显示,有些则没有.我指的是html元素.
当页面上有多个ModleAttributes时,我不确定Controller的外观.下面是代码的示例:
页
<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" …Run Code Online (Sandbox Code Playgroud) 有关Spring MVC @ModelAttribute方法的问题,在控制器@RequestMapping方法中设置模型属性与使用@ModelAttribute方法单独设置属性,哪一个被认为更好并且更常用?
从设计的角度来看,哪种方法从以下方面考虑更好:
方法1
@ModelAttribute("message")
public String addMessage(@PathVariable("userName") String userName, ModelMap model) {
LOGGER.info("addMessage - " + userName);
return "Spring 3 MVC Hello World - " + userName;
}
@RequestMapping(value="/welcome/{userName}", method = RequestMethod.GET)
public String printWelcome(@PathVariable("userName") String userName, ModelMap model) {
LOGGER.info("printWelcome - " + userName);
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
方法2
@RequestMapping(value="/welcome/{userName}", method = RequestMethod.GET)
public String printWelcome(@PathVariable("userName") String userName, ModelMap model) {
LOGGER.info("printWelcome - " + userName);
model.addAttribute("message", "Spring 3 MVC Hello World …Run Code Online (Sandbox Code Playgroud) 我是Spring的新手,并注册了一个用户.我确实喜欢这个.
@RequestMapping("/register")
public String register(@ModelAttribute User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但这里的问题是我user在welcome.jsp页面中不需要这个对象,所以为什么要让模型对象更重.所以我试过没有@ModelAttribute,这对我来说也适用于下面.
@RequestMapping("/register")
public String register(User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
Run Code Online (Sandbox Code Playgroud)
所以我只想知道两者的优点和缺点,如果我真的不需要user对象,那么这是最佳实践jsp.是@ModelAttribute做除了增加对象模型的任何其他事情,这春天隐式绑定不does.Is @ModelAttribute结合或者更安全的方法是什么?
我想在以下4种类型的请求中对我的查询进行分类.@ModelAttribute如果我不需要在视图中发送数据并且我的请求是 -
@RequestBody是必须的.在我的Spring MVC应用程序中,我需要实现一个动态的问卷形式:我有N个问题,每个我都有3个选项.
所以在我的页面中我会有这样的东西:
| Question 1 | 1 | 2 | 3 |
| Question 2 | 1 | 2 | 3 |
| Question 3 | 1 | 2 | 3 |
| ... | 1 | 2 | 3 |
| Question N | 1 | 2 | 3 |
Run Code Online (Sandbox Code Playgroud)
问题存储在数据库中,对于我将使用单选按钮的选项.我将使用forEach标签来修改动态行,但我不知道如何ModelAttribute在这种情况下发布数据和处理绑定...
对于我的模型属性类,这可能是一个很好的结构?是否可以在Spring MVC中使用动态形式的绑定?
我有一个JSP,我试图从我的模型中打印出值,但没有出现在我引用这些值的地方.这是我设置值的控制器方法.
@RequestMapping(value = "/poll1", method = RequestMethod.POST)
public String processPoll1(@RequestParam String vote,
HttpServletResponse response, Model model) {
Map<String, Object> resultMap = poll1DAO.tallyVote(vote);
Cookie poll1 = new Cookie("poll1", "voted");
model.addAttribute("poll1Yes", resultMap.get("yes").toString());
model.addAttribute("poll1No", resultMap.get("no").toString());
poll1.setMaxAge(maxSeconds);
response.addCookie(poll1);
return "redirect:/polls";
}
Run Code Online (Sandbox Code Playgroud)
这是JSP中引用Model属性的部分.
<table>
<tr>
<td><b><i>Poll #1 -- </i></b>Would you like to have a 30-year reunion in 2016?<br></td>
</tr>
<tr>
<td><b>Yes</b></td>
<td> – <c:out value='${model.poll1Yes}' /><br /></td>
</tr>
<tr>
<td><b>No</b></td>
<td> – <c:out value='${model.poll1No}' /><br />
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这是我的输出.不是实际值,而是在属性的位置打印出任何内容.
Poll #1 -- Would you like to have a …Run Code Online (Sandbox Code Playgroud) 如何将百万美元(th:object)值传递给控制器.
HTML:
<form id="searchPersonForm" action="#" th:object="${person}" method="post" >
</form>
Run Code Online (Sandbox Code Playgroud)
SearchPersonController:
@RequestMapping(value = "/modify/{pid}", method = RequestMethod.GET)
public String modifyPersonData(Principal principal, @ModelAttribute("person") Person person, UserRoles userRoles, Model model, @PathVariable("pid") Long pid ) {
//modify data
}
Run Code Online (Sandbox Code Playgroud)
我想尝试传递,@ModelAttribute("person") Person person但这不是从上一页检索表单值.
任何人都可以帮助这个.
谢谢.
我正在尝试制作一个发布CardRequestResource的表单:
public class CardRequestResource extends ResourceSupport{
private Long cardRequestId;
private String deliveryMethod;
private String address;
private boolean isHomeDelivery;
private String requestDate;
private String expectedDate;
private String comments;
private Long statusId;
private PersonResource person;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我首先加载JSP并将一个空的CardRequestResource添加到ModelMap:
@RequestMapping(value = { "", "/approval" }, method = RequestMethod.GET)
public String getApproval(ModelMap map) {
map.put("cardRequestResource", new CardRequestResource());
return "cardoffice/approval";
}
Run Code Online (Sandbox Code Playgroud)
我的JSP使用cardRequestResource模型属性构建表单:
<form:form id="detailForm" name="detailForm" modelAttribute="cardRequestResource">
<form:input path="statusId" type="hidden" id="statusId" name="statusId" />
<form:textarea path="comments" name="comments" id="commentTextarea" rows="7" cols="81" style="font-style: normal;"/>
</form:form>
Run Code Online (Sandbox Code Playgroud)
Javascript函数进行AJAX调用以填充表单值:
function …Run Code Online (Sandbox Code Playgroud) modelattribute ×10
spring-mvc ×7
java ×4
forms ×2
html ×2
spring ×2
controllers ×1
data-binding ×1
jquery ×1
jsp ×1
jstl ×1
null ×1
redirect ×1
spring-3 ×1
spring-boot ×1
thymeleaf ×1