Spring3MVC 在JSP中使用@ModelAttribute--源自技术

本文详细介绍了如何在Spring MVC中使用@ModelAttribute注解,包括在JSP中展示数据。通过示例展示了如何从控制器到视图传递模型数据,以及如何在编辑页面中接收和更新数据。
摘要由CSDN通过智能技术生成
在教程中,我们将创建一个简单的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  

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   
  8.     <servlet>  
  9.         <servlet-name>spring</servlet-name>  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <load-on-startup>1</load-on-startup>  
  12.     </servlet>  
  13.   
  14.     <servlet-mapping>  
  15.         <servlet-name>spring</servlet-name>  
  16.         <url-pattern>/</url-pattern>  
  17.     </servlet-mapping>  
  18.   
  19.     <listener>  
  20.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  21.     </listener>  
  22.   
  23. </web-app>  


在web.xml中我们定义servlet:spring. 
按照惯例,我们必须声明一个spring-servle.xml 
用springIDE插件创建一个配置xml. 
内容包含: 

spring-servle.xml  

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.   
  7.     <!-- 定义一个视图解析器 -->  
  8.     <bean id="viewResolver"  
  9.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  10.         p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />  
  11.   
  12. </beans>  



这个XML配置声明一个视图解析器.在控制器中会根据JSP名映射到/ WEB-INF/jsp中相应的位置. 

然后创建一个applicationContext.xml. 

applicationContext.xml  

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.             http://www.springframework.org/schema/context  
  9.             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.             http://www.springframework.org/schema/mvc   
  11.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  12.   
  13.     <!-- 激活spring的注解. -->  
  14.     <context:annotation-config />  
  15.   
  16.     <!-- 扫描注解组件并且自动的注入spring beans中.   
  17.     例如,他会扫描org.liukai.tutorial下@Controller 和@Service下的文件.所以确保此base-package设置正确. -->  
  18.     <context:component-scan base-package="org.liukai.tutorial" />  
  19.   
  20.     <!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:此标签只在 Servlet MVC工作! -->  
  21.     <mvc:annotation-driven />  
  22.   
  23. </beans>  



首先让我们定义两个简单的POJO 

Address.java 

Java代码   收藏代码
  1. package org.liukai.tutorial.domain;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Address implements Serializable {  
  6.   
  7.     private static final long serialVersionUID = -8889854671283221397L;  
  8.   
  9.     private Integer id;  
  10.     private String street;  
  11.     private String zipCode;  
  12.     private String city;  
  13.    
  14.          ......getter/setter  
  15. }  


Person.java  
Java代码   收藏代码
  1. package org.liukai.tutorial.domain;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable {  
  6.   
  7.     private static final long serialVersionUID = -8333984959652704635L;  
  8.   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值