关于spring @ModelAttribute获取任意字符串的问题

mar*_*n72 2 spring-mvc

在spring3上课时,我编写了一个教程中的示例.我创建了一个控制器如下

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());
 }
}
Run Code Online (Sandbox Code Playgroud)

然后我决定玩它并修改方法参数中的@ModelAttribute

public String addContact(@ModelAttribute("contact") Contact ct)
Run Code Online (Sandbox Code Playgroud)

public String addContact(@ModelAttribute("somevalue") Contact ct)
Run Code Online (Sandbox Code Playgroud)

我仍然无法找到应用程序行为的任何变化.这对我来说有点意外.据我所知,表单中的数据是在Contact对象中收集的,并使用@ModelAttribute将该对象绑定到参数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>
Run Code Online (Sandbox Code Playgroud)

和弹簧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>
Run Code Online (Sandbox Code Playgroud)

最后,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>
Run Code Online (Sandbox Code Playgroud)

Jav*_*avi 6

它将用于将模型属性映射到表单:

<form:form modelAttribute="myObject">

</form>
Run Code Online (Sandbox Code Playgroud)

你将不得不使用

public String controllerMethod(@ModelAttribute("myObject") Object obj){
    ....
}
Run Code Online (Sandbox Code Playgroud)

如果在两个模型属性值中都不使用相同的值,则无法看到验证的错误消息(在控制器result.rejectValue和jsp <form:errors/>标记中执行时).

填充对象中表单的值没有区别,因为您可以使用html标记而不是spring标记,而在html标记中,您只有name属性来映射值.

如果您在下面的方法中使用@ModelAttribute,您将在模型中插入一个名为"myObject"的属性,其值为返回的对象.这是此注释的另一个特性,它将在控制器的任何方法之前调用.

@ModelAttribute("myObject") Object obj
public Object method(){
    ...
    return obj;
}
Run Code Online (Sandbox Code Playgroud)