在教程中,我们将创建一个简单的Spring3MVC simple CRUD应用程序.
什么是@ModelAttribute
Spring3关于@ModelAttribute的文档 (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib)
@ModelAttribute has two usage scenarios in controllers. When you place it on a method parameter, @ModelAttribute maps a model attribute to the specific, annotated method parameter (see the processSubmit() method below). This is how the controller gets a reference to the object holding the data entered in the form.
You can also use @ModelAttribute at the method level to provide reference data for the model (see the populatePetTypes() method in the following example). For this usage the method signature can contain the same types as documented previously for the @RequestMapping annotation.
Note
@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.
大意就是:
当你放置在方法参数,@ModelAttribute模型映射到具体属性;
你也可以使用@ModelAttribute在方法级别上提供参考模型的数据.
英语不好.见谅
eclipse创建一个标准的maven web项目
我们创建一个名为 spring-jsp 的web项目.并添加图下所示
为了开启SpringMVC,我们需要在web.xml添加以下内容
web.xml
在web.xml中我们定义servlet:spring.
按照惯例,我们必须声明一个spring-servle.xml
用springIDE插件创建一个配置xml.
内容包含:
spring-servle.xml
这个XML配置声明一个视图解析器.在控制器中会根据JSP名映射到/ WEB-INF/jsp中相应的位置.
然后创建一个applicationContext.xml.
applicationContext.xml
首先让我们定义两个简单的POJO
Address.java
Person.java
什么是@ModelAttribute
Spring3关于@ModelAttribute的文档 (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib)
引用
@ModelAttribute has two usage scenarios in controllers. When you place it on a method parameter, @ModelAttribute maps a model attribute to the specific, annotated method parameter (see the processSubmit() method below). This is how the controller gets a reference to the object holding the data entered in the form.
You can also use @ModelAttribute at the method level to provide reference data for the model (see the populatePetTypes() method in the following example). For this usage the method signature can contain the same types as documented previously for the @RequestMapping annotation.
Note
@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.
大意就是:
当你放置在方法参数,@ModelAttribute模型映射到具体属性;
你也可以使用@ModelAttribute在方法级别上提供参考模型的数据.
英语不好.见谅

eclipse创建一个标准的maven web项目
我们创建一个名为 spring-jsp 的web项目.并添加图下所示

为了开启SpringMVC,我们需要在web.xml添加以下内容
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
在web.xml中我们定义servlet:spring.
按照惯例,我们必须声明一个spring-servle.xml
用springIDE插件创建一个配置xml.
内容包含:
spring-servle.xml
- <?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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 定义一个视图解析器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
- </beans>
这个XML配置声明一个视图解析器.在控制器中会根据JSP名映射到/ WEB-INF/jsp中相应的位置.
然后创建一个applicationContext.xml.
applicationContext.xml
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 激活spring的注解. -->
- <context:annotation-config />
- <!-- 扫描注解组件并且自动的注入spring beans中.
- 例如,他会扫描org.liukai.tutorial下@Controller 和@Service下的文件.所以确保此base-package设置正确. -->
- <context:component-scan base-package="org.liukai.tutorial" />
- <!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:此标签只在 Servlet MVC工作! -->
- <mvc:annotation-driven />
- </beans>
首先让我们定义两个简单的POJO
Address.java
- package org.liukai.tutorial.domain;
- import java.io.Serializable;
- public class Address implements Serializable {
- private static final long serialVersionUID = -8889854671283221397L;
- private Integer id;
- private String street;
- private String zipCode;
- private String city;
- ......getter/setter
- }
Person.java
- package org.liukai.tutorial.domain;
- import java.io.Serializable;
- public class Person implements Serializable {
- private static final long serialVersionUID = -8333984959652704635L;