- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 spring3 上课时,我从教程中编写了一个示例。我创建了一个 Controller ,如下所示
package my.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import my.spring.form.Contact;
@Controller
public class ContactController {
@RequestMapping(value ="/addContact",method =RequestMethod.POST)
public String addContact(@ModelAttribute("contact") Contact ct){
System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname());
return "redirect:contacts.htm";
}
@RequestMapping("/contacts")
public ModelAndView showContacts() {
System.out.println("showing contacts");
return new ModelAndView("contact", "userEntries", new Contact());
}
}
然后我决定尝试一下并修改方法参数中的@ModelAttribute
public String addContact(@ModelAttribute("contact") Contact ct)
至
public String addContact(@ModelAttribute("somevalue") Contact ct)
我仍然找不到应用程序行为的任何变化。这对我来说有点意外。据我了解,表单中的数据收集在 Contact 对象中,并使用 @ModelAttribute 该对象绑定(bind)到参数 ct。然后使用该参数在方法内部进行处理。@ModelAttribute() 内部使用的实际字符串是否无关紧要?
这是 WEB-INF/jsp/contact.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form action="addContact.htm" commandName="userEntries">
<table>
<tr>
<td>
<form:label path="firstname">First Name</form:label>
</td>
<td>
<form:input path="firstname"/>
</td>
</tr>
<tr>
<td>
<form:label path="lastname">Last Name</form:label>
</td>
<td>
<form:input path="lastname"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
和 spring servlet 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="my.spring.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
最后,index.jsp 转发给联系人
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>my index</title>
</head>
<body>
<jsp:forward page="contacts.htm"></jsp:forward>
</body>
</html>
最佳答案
它将用于将模型属性映射到表单:
<form:form modelAttribute="myObject">
</form>
您必须使用
public String controllerMethod(@ModelAttribute("myObject") Object obj){
....
}
如果您不在两个模型属性值中使用相同的值,您将无法看到验证的错误消息(在 Controller result.rejectValue 和 jsp <form:errors/>
标记中执行操作时)。
在对象中填充表单中的值没有区别,因为您可以使用 html 标签而不是 spring 标签,并且在 html 标签中您只有 name 属性来映射值。
如果您在如下方法中使用@ModelAttribute,您将在模型中插入一个名为“myObject”的属性,其值是对象返回的值。这是此注释的另一个功能,它将在 Controller 的任何方法之前调用。
@ModelAttribute("myObject") Object obj
public Object method(){
...
return obj;
}
关于spring-mvc - 关于 spring @ModelAttribute 接受任意字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4525253/
我想知道是否可以通过在方法签名中使用另一个 ModelAttribute 注释但不请求映射的方法来链接 @ModelAttribute 方法。这将在 Controller 中。 即 @ModelAtt
这就是现在的样子: @SessionAttributes("shoppingCart") public class ItemController { @ModelAttribute p
在我看来,我生成输入“用户名”,使用属性 multiple=multiple 选择,其名称为“rolesss”。 我的问题是,如果我通过邮寄发送此类表单,我的 Controller 应该将角色转换为列
Controller 中的@ModelAttribute方法是 @ModelAttribute("command") public A getA() { ... } @ModelAttribute p
平台:Spring 3.1.2、Tomcat 7.0.30 从 3.0 升级到 Spring 3.1 后出现此问题 我有一个正常的启用 Spring 的表单:
我对 Spring 3 有疑问。任何人都可以告诉我,在 Controller 中使用这样的方法: @ModelAttribute("nameAtributte") public Customer ge
如本节所述 link下节在方法参数上使用@ModelAttribute An @ModelAttribute on a method argument indicates the argument s
我使用以下形式发送值: ${font.nameFont} Controller : @GetMapping public String main(@Authe
我的问题是:如果请求参数和表单字段具有相同的名称,@ModelAttribute 从请求参数填充表单字段而不是表单 DTO。 示例:我有一个带有名为 name 的输入字段的表单: 给定的表单,其值
我有一个带有 lombok 符号的 User 类 @Getter @Setter @AllArgsConstructor @NoArgsConstructor @Builder class User
请帮帮我。我有这样的 Controller 代码: @RequestMapping(method = RequestMethod.GET) public String showOrders(@Requ
想象一下这样的代码: @RequestMapping(value="/users", method=RequestMethod.GET) public String list(Model model)
我在使用 Spring MVC 3.0 中的 ModelAttribute 时遇到了一个奇怪的问题。当我在本地主机上部署应用程序时,它工作正常。但是当我在远程服务器上部署该应用程序时,每次用户访问特定
我有一个具有许多属性的用户实体(此处未显示一些字段): @Entity public class User { @OneToOne(cascade = ALL, orphanRemoval =
我有一个 Spring 2.5 带注释的 Controller ,其中有一个用 @RequestMapping(method=RequestMethod.GET) 注释的方法,它执行一些逻辑来填充模型
我需要帮助来理解下面的示例 @ModelAttribute来自 Spring文档:(方法 populatePetTypes() ) @Controller @RequestMapping("/owne
我有这样的@Controller: @Controller public class CandidateMenuController{ @ModelAttribute(va
我正在使用 Springfox 和 Swagger 生成 swagger 文件。现在,我正在使用 @ModelAttribute 从对象 (NetworkCmd) 中提取变量,以在 swagger 文
我希望在抽象类中有一个注释为 @ModelAttribute 的通用方法,但具有来自子类的值。最终目标是检索 JSP 中变量的值。每个子类 Controller 中的值都不同,但我不想重复 @Mode
我正在重写一个用内部框架编写的旧 REST 服务以使用 Spring。我有一个带有 POST 方法的 Controller ,该方法将参数作为 POST 或 x-www-form-urlencoded
我是一名优秀的程序员,十分优秀!