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

Spring MVC中@SessionAttributes和@ModelAttribute的实战应用 在Spring MVC框架中,和是两个非常有用的注解,它们可以帮助我们在控制器(Controller)中管理会话级别的数据。本文将通过一个实际的例子来详细解释这两个注解的使用方法和它们在实际开发中的作用。 阅读详情
在教程中,我们将创建一个简单的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.   
  9.     private Integer id;  
  10.     private String firstName;  
  11.     private String lastName;  
  12.     private String currency;  
  13.     private Double money;  
  14.   
  15.   ......getter/setter  
  16.   
  17. }  


然后实现2个简单的service层用于填充和处理业务逻辑. 

AddressService.java  
Java代码   收藏代码
  1. package org.liukai.tutorial.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.log4j.Logger;  
  7. import org.liukai.tutorial.domain.Address;  
  8. import org.springframework.stereotype.Service;  
  9. import org.springframework.transaction.annotation.Transactional;  
  10.   
  11. /** 
  12.  * 同样用于显示. 
  13.  *  
  14.  * @author liukai 
  15.  *  
  16.  */  
  17. @Service("addressService")  
  18. @Transactional  
  19. public class AddressService {  
  20.   
  21.     protected static Logger logger = Logger.getLogger("service");  
  22.   
  23.     /** 
  24.      * 获得所有的地址. 
  25.      *  
  26.      * @return 
  27.      */  
  28.     public List<Address> getAll() {  
  29.         logger.debug("Retrieving all addresses");  
  30.   
  31.         List<Address> addresses = new ArrayList<Address>();  
  32.   
  33.         // New address  
  34.         Address address = new Address();  
  35.         address.setId(1);  
  36.         address.setStreet("1 Street");  
  37.         address.setCity("City 1");  
  38.         address.setZipCode("11111");  
  39.   
  40.         // Add to list  
  41.         addresses.add(address);  
  42.   
  43.         // New address  
  44.         address = new Address();  
  45.         address.setId(2);  
  46.         address.setStreet("2 Street");  
  47.         address.setCity("City 2");  
  48.         address.setZipCode("22222");  
  49.   
  50.         // Add to list  
  51.         addresses.add(address);  
  52.   
  53.         // New address  
  54.         address = new Address();  
  55.         address.setId(3);  
  56.         address.setStreet("3 Street");  
  57.         address.setCity("City 3");  
  58.         address.setZipCode("33333");  
  59.   
  60.         // Add to list  
  61.         addresses.add(address);  
  62.   
  63.         // Return all addresses  
  64.         return addresses;  
  65.     }  
  66.   
  67. }  


PersonService.java  


Java代码   收藏代码
  1. package org.liukai.tutorial.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.log4j.Logger;  
  9. import org.liukai.tutorial.domain.Person;  
  10. import org.springframework.stereotype.Service;  
  11. import org.springframework.transaction.annotation.Transactional;  
  12.   
  13. /** 
  14.  * 注意这个只是一个临时数据用于显示. 
  15.  *  
  16.  * @author liukai 
  17.  *  
  18.  */  
  19. @Service("personService")  
  20. @Transactional  
  21. public class PersonService {  
  22.   
  23.     protected static Logger logger = Logger.getLogger("service");  
  24.   
  25.     private HashMap<Integer, Person> database = new HashMap<Integer, Person>();  
  26.   
  27.     /** 
  28.      * Initialize a list of persons 
  29.      */  
  30.     private void init() {  
  31.         // New person  
  32.         Person person = new Person();  
  33.         person.setId(1);  
  34.         person.setFirstName("John");  
  35.         person.setLastName("Smith");  
  36.         person.setCurrency("Dollar");  
  37.         person.setMoney(1500.00);  
  38.   
  39.         // Add to list  
  40.         database.put(1, person);  
  41.   
  42.         // New person  
  43.         person = new Person();  
  44.         person.setId(2);  
  45.         person.setFirstName("Jane");  
  46.         person.setLastName("Adams");  
  47.         person.setCurrency("Yen");  
  48.         person.setMoney(1000.00);  
  49.   
  50.         // Add to list  
  51.         database.put(2, person);  
  52.   
  53.         // New person  
  54.         person = new Person();  
  55.         person.setId(3);  
  56.         person.setFirstName("Mike");  
  57.         person.setLastName("Polaski");  
  58.         person.setCurrency("Euro");  
  59.         person.setMoney(2000.00);  
  60.   
  61.         // Add to list  
  62.         database.put(3, person);  
  63.     }  
  64.   
  65.     public PersonService() {  
  66.         // Initialize dummy database  
  67.         init();  
  68.     }  
  69.   
  70.     /** 
  71.      * 检索所有的 persons 
  72.      */  
  73.     public List<Person> getAll() {  
  74.         logger.debug("Retrieving all persons");  
  75.   
  76.         // Initialize our array  
  77.         List<Person> persons = new ArrayList<Person>();  
  78.   
  79.         // Iterate the database  
  80.         for (Map.Entry<Integer, Person> entry : database.entrySet()) {  
  81.             persons.add(entry.getValue());  
  82.         }  
  83.   
  84.         // Return all persons  
  85.         return persons;  
  86.     }  
  87.   
  88.     /** 
  89.      * 根据ID获得对应的Perosn 
  90.      */  
  91.     public Person get(Integer id) {  
  92.         logger.debug("Retrieving person based on his id");  
  93.         return database.get(id);  
  94.     }  
  95.   
  96.     /** 
  97.      * 修改Person 
  98.      */  
  99.     public void edit(Person person) {  
  100.         logger.debug("Editing existing person");  
  101.   
  102.         // Note this is not the best way to update a data!  
  103.   
  104.         // Delete existing user  
  105.         database.remove(person.getId());  
  106.         // Add updated user  
  107.         database.put(person.getId(), person);  
  108.     }  
  109.   
  110. }  


接下来就是@ModelAttribute的两种使用方法. 

模式1:method级(作用于方法上)  


引用
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. 

来源: spring3文档  (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib)

我们在controller里用注解@ModelAttribute 定义一个method. 

AddressController.java 

Java代码   收藏代码
  1. package org.liukai.tutorial.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.apache.log4j.Logger;  
  8. import org.liukai.tutorial.domain.Address;  
  9. import org.liukai.tutorial.service.AddressService;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.ui.Model;  
  12. import org.springframework.web.bind.annotation.ModelAttribute;  
  13. import org.springframework.web.bind.annotation.RequestMapping;  
  14. import org.springframework.web.bind.annotation.RequestMethod;  
  15.   
  16. @Controller  
  17. @RequestMapping("/address")  
  18. public class AddressController {  
  19.   
  20.     protected static Logger logger = Logger.getLogger("controller");  
  21.   
  22.     @Resource(name = "addressService")  
  23.     private AddressService addressService;  
  24.   
  25.     /** 
  26.      * 获得所有Addresses,并使他们成为一个model.  
  27.      * 注意@ModelAttribute作用于method级别上时.会在@Controller执行时加载 
  28.      * method里的方法.即在@Controller执行时@ModelAttribute添加参数. 
  29.      * 返回于该@Controller返回的所有JSP页面. 
  30.      */  
  31.     @ModelAttribute("addresses")  
  32.     public List<Address> getAllAddresses() {  
  33.         return addressService.getAll();  
  34.     }  
  35.   
  36.     /** 
  37.      * 处理和检索一个包含addresses 的JSP Page . 
  38.      */  
  39.     @RequestMapping(value = "list1", method = RequestMethod.GET)  
  40.     public String getAllUsingModelAttribute() {  
  41.         logger.debug("Received request to show all addresses page");  
  42.   
  43.         // 他会解析 /WEB-INF/jsp/addressespage.jsp  
  44.         return "addressespage";  
  45.     }  
  46.   
  47.     /** 
  48.      * 处理和检索一个包含addresses 的JSP Page . 
  49.      *  
  50.      * @return the name of the JSP page 
  51.      */  
  52.     @RequestMapping(value = "list2", method = RequestMethod.GET)  
  53.     public String getAllUsingModel(Model model) {  
  54.         logger.debug("Received request to show all addresses page");  
  55.   
  56.         // 检索所有的address并装入model返回addressespage.jsp  
  57.         model.addAttribute("addresses", addressService.getAll());  
  58.         model.addAttribute("greetings", "I came from Model not ModelAttribute");  
  59.   
  60.         // This will resolve to /WEB-INF/jsp/addressespage.jsp  
  61.         return "addressespage";  
  62.     }  
  63.   
  64. }  


Controller中有一个注解为@ModelAttribute("addresses")的getAllAddresses的method 
表示在JSP Page中的参数名称为addresses. 


创建一个JSP页面:addressespage.jsp 

addressespage.jsp  
Jsp代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.   
  12.     <h1>Addresses</h1>  
  13.   
  14.     <table>  
  15.         <tr>  
  16.             <td width="100">Id</td>  
  17.             <td width="150">Street</td>  
  18.             <td width="150">City</td>  
  19.             <td width="150">Zip Code</td>  
  20.         </tr>  
  21.         <c:forEach items="${addresses}" var="address">  
  22.             <tr>  
  23.                 <td><c:out value="${address.id}" />  
  24.                 </td>  
  25.                 <td><c:out value="${address.street}" />  
  26.                 </td>  
  27.                 <td><c:out value="${address.city}" />  
  28.                 </td>  
  29.                 <td><c:out value="${address.zipCode}" />  
  30.                 </td>  
  31.             </tr>  
  32.         </c:forEach>  
  33.     </table>  
  34.   
  35.     <p>${greetings}</p>  
  36. </body>  
  37. </html>  


根据controller里的@RequestMapping.我们的访问路径为: 
http://localhost:8080/spring-jsp/address/list1  
http://localhost:8080/spring-jsp/address/list2  
下面是根据address/list1得到的截图. 

 

而根据address/list2得到的截图是: 

 


比较一下他们有什么区别? 
后者是不是多了一行文字? 
然后回过头看看代码. 

引用
@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. 

来源: spring3文档  

大意是:@ModelAttribute是在所选择的@RequestMapping 处理方法之前执行的. 
他们有效的填充数据,经常用于从database加载数据. 

所以你可以用@ModelAttribute遍历你的List. 
如果你在controller中做一些update操作.你可以先获得旧的List一直到你获得新数据后覆盖以前的数据. 


模式2:method parameter级(即方法里所带的参数)  
引用
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.rameters in the chosen handler method, potentially with binding and validation applied to it.

来源: spring3文档  

大意是:如果你的方法参数里带有@ModelAttribute的参数.表示从JSP Page传回的参数并自动的转化为java对象 


Java代码   收藏代码
  1. package org.liukai.tutorial.controller;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.apache.log4j.Logger;  
  9. import org.liukai.tutorial.domain.Person;  
  10. import org.liukai.tutorial.service.PersonService;  
  11. import org.springframework.stereotype.Controller;  
  12. import org.springframework.ui.Model;  
  13. import org.springframework.web.bind.annotation.ModelAttribute;  
  14. import org.springframework.web.bind.annotation.PathVariable;  
  15. import org.springframework.web.bind.annotation.RequestMapping;  
  16. import org.springframework.web.bind.annotation.RequestMethod;  
  17.   
  18. @Controller  
  19. @RequestMapping("/main")  
  20. public class MainController {  
  21.   
  22.     protected static Logger logger = Logger.getLogger("controller");  
  23.   
  24.     @Resource(name = "personService")  
  25.     private PersonService personService;  
  26.   
  27.     /** 
  28.      * 获得所有Person,并使他们成为一个model. 
  29.      */  
  30.     @ModelAttribute("persons")  
  31.     public List<Person> getAllPersons() {  
  32.         logger.debug("Retrieving all persons and adding it to ModelAttribute");  
  33.         // Delegate to PersonService  
  34.         return personService.getAll();  
  35.     }  
  36.   
  37.     /** 
  38.      * 获得所有的货币类型,并使他们成为一个model. 
  39.      */  
  40.     @ModelAttribute("currencies")  
  41.     public List<String> getAllCurrencies() {  
  42.         logger.debug("Retrieving all currencies and adding it to ModelAttribute");  
  43.   
  44.         // Prepare data  
  45.         List<String> currencies = new ArrayList<String>();  
  46.         currencies.add("Dollar");  
  47.         currencies.add("Yen");  
  48.         currencies.add("Pound");  
  49.         currencies.add("Euro");  
  50.         currencies.add("Dinar");  
  51.   
  52.         return currencies;  
  53.     }  
  54.   
  55.     /** 
  56.      * 处理和检索一个包含Perosn 的JSP Page 
  57.      */  
  58.     @RequestMapping(method = RequestMethod.GET)  
  59.     public String getAllPage(Model model) {  
  60.   
  61.         logger.debug("Received request to show all persons page");  
  62.   
  63.         // personsage.jsp会引用一个名叫persons的model attribute  
  64.         // 我们不需要手动添加这个model  
  65.         // 前面他已经自动的通过@ModelAttribute("persons")进行了添加.  
  66.   
  67.         // 他会解析 /WEB-INF/jsp/personspage.jsp  
  68.         return "personspage";  
  69.     }  
  70.   
  71.     /** 
  72.      * 检索修改页面 
  73.      */  
  74.     @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)  
  75.     public String getEdit(@PathVariable Integer id, Model model) {  
  76.         /* 
  77.          * @PathVariable表示指定@RequestMapping的URL模板中{}里的值  
  78.          * 相当于以前我们URL后面传的参数如XX?id=XXXX . 
  79.          * 但是现在我们可以用 XX/id/XX来代替.  
  80.          * 这个就是REST风格的URL,我们可以实现非常复杂的URL模板 
  81.          */  
  82.   
  83.         logger.debug("Received request to show edit page");  
  84.   
  85.         /* 
  86.          * 根据ID检索出对应的Person,然后把检索出来的Person放入一个叫 
  87.          * "personAttribute"的model中. 
  88.          * 这样editpage.jsp就会接收一个 名为personAttribute的参数. 
  89.          * 相当于request.setAttribute("XX",XX) 
  90.          */  
  91.         model.addAttribute("personAttribute", personService.get(id));  
  92.   
  93.         // This will resolve to /WEB-INF/jsp/editpage.jsp  
  94.         return "editpage";  
  95.     }  
  96.   
  97.     /** 
  98.      * 保存修改结果 
  99.      */  
  100.     @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)  
  101.     public String saveEdit(@ModelAttribute("personAttribute") Person person,  
  102.             @PathVariable Integer id, Model model) {  
  103.         logger.debug("Received request to update person");  
  104.   
  105.         // 我们从页面接收到了一个名为"personAttribute"的model并命名为person  
  106.         // 同样我们也获得了指定的id.  
  107.         person.setId(id);  
  108.   
  109.         // 更新person  
  110.         personService.edit(person);  
  111.   
  112.         // 在更新后我们重新显示所有Person 的页面  
  113.         model.addAttribute("persons", personService.getAll());  
  114.   
  115.         // This will resolve to /WEB-INF/jsp/personspage.jsp  
  116.         return "personspage";  
  117.     }  
  118.   
  119. }  


这个controller里定义了两个method级别的@ModelAttribute方法:getAllPersons和getAllCurrencies 
我们已经了解了他们的用法和意义. 
然后在saveEdit方法中,有个一个参数是用@ModelAttribute注解的. 

Java代码   收藏代码
  1. @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)  
  2.     public String saveEdit(@ModelAttribute("personAttribute") Person person,   
  3.       @PathVariable Integer id, Model model) {  
  4. ...  
  5. }  

表示从JSP 页面返回的一个叫"personAttribute"的值.并自动的转化为Person对象. 
这样和以前我们用的request.getParameters("personAttribute")效果一样. 
但是一个是操作参数对象.一个是处理请求.两者的实现思想不同.  


在此controller中我们有3个映射: 
/main  - 检索所有的Person 

/main/edit/{id} - (GET)根据ID进行检索和edit 

/main/edit/{id} - (POST) 根据ID进行更新 

注:后两者的URL虽然一样, 
但一个是GET方法,一般用于检索. 
一个是POST方法,一般用于提交表单. 
如果大家有注意@RequestMapping中的method方法其实有四种. 
GET 
POST 
PUT 
DELETE 

每个方法对应一个逻辑操作.对于REST风格的编程是一个相当好的补充. 
关于这点感兴趣的同学可以看看springsource一篇官方BLOG: REST in Spring 3: @MVC  

让我们继续完成其他的JSP 

personspage.jsp
 
Jsp代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.   
  12. <h1>Persons</h1>  
  13.   
  14. <table>  
  15.     <tr>  
  16.         <td width="50">Id</td>  
  17.         <td width="150">First Name</td>  
  18.         <td width="150">Last Name</td>  
  19.         <td width="100">Money</td>  
  20.         <td width="50">Currency</td>  
  21.     </tr>  
  22.     <c:forEach items="${persons}" var="person">  
  23.         <tr>  
  24.             <td><c:out value="${person.id}" /></td>  
  25.             <td><c:out value="${person.firstName}" /></td>  
  26.             <td><c:out value="${person.lastName}" /></td>  
  27.             <td><c:out value="${person.money}" /></td>  
  28.             <td><c:out value="${person.currency}" /></td>  
  29.         </tr>  
  30.     </c:forEach>  
  31. </table>  
  32.   
  33. </body>  
  34. </html>  

这个主要是映射 /main. 


editpage.jsp  

Jsp代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
  3. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  4.     pageEncoding="UTF-8"%>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12.   
  13. <h1>Edit Person</h1>  
  14.   
  15. <c:url var="saveUrl" value="/main/edit/${personAttribute.id}" />  
  16.   
  17. <form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">  
  18.     <table>  
  19.         <tr>  
  20.             <td><form:label path="id">Id:</form:label></td>  
  21.             <td><form:input path="id" disabled="true"/></td>  
  22.         </tr>  
  23.       
  24.         <tr>  
  25.             <td><form:label path="firstName">First Name:</form:label></td>  
  26.             <td><form:input path="firstName"/></td>  
  27.         </tr>  
  28.   
  29.         <tr>  
  30.             <td><form:label path="lastName">Last Name</form:label></td>  
  31.             <td><form:input path="lastName"/></td>  
  32.         </tr>  
  33.           
  34.         <tr>  
  35.             <td><form:label path="money">Money</form:label></td>  
  36.             <td><form:input path="money"/></td>  
  37.         </tr>  
  38.           
  39.         <tr>  
  40.             <td><form:label path="currency">Currency:</form:label></td>  
  41.             <td><form:select path="currency"  items="${currencies}"/></td>  
  42.         </tr>  
  43.     </table>  
  44.       
  45.     <input type="submit" value="Save" />  
  46. </form:form>  
  47.   
  48. </body>  
  49. </html>  

此页面返回以下controller中的方法: 


Java代码   收藏代码
  1. @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)  
  2.     public String getEdit(@PathVariable Integer id, Model model) {  
  3. ...  
  4. }  


我们可以通过类似 
http://localhost:8080/spring-jsp/main/edit/1  
的链接进行编辑. 
下面是编辑页面 

 

当我们编辑完提交表格,执行了下面的方法 

Java代码   收藏代码
  1.   @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)  
  2.     public String saveEdit(@ModelAttribute("personAttribute") Person person,   
  3.       @PathVariable Integer id, Model model) {  
  4. ...  
  5. }  


整个项目的目录结构如下 

 


总结:我们完成一个基于spring3MVC的应用程序. 
并且了解了@ModelAttribute的两种使用模式. 
一个是作用于method级别, 
一个是用作method里的参数. 




BTW:附件为本次教程源码.你可以下载后直接在tomcat或其他web服务器启动.也可以自行添加 
PCIe Crosslink PCIe Crosslink(交叉连接)是一种特殊的PCIe链路连接方式,允许两个PCIe设备(通常是Root Complex或Endpoint)是指两个PCIe设备(如两个CPU、两个FPGA或两个GPU)通过PCIe链路直接连接,形成点对点(P2P)通信通道。:两个Intel/AMD CPU通过PCIe Crosslink直接通信(替代UPI/Infinity Fabric)。:主板需提供PCIe Crosslink连接(如特定插槽支持x16/x8 Crosslink)。 阅读详情

相关推荐

DSPACE仿真平台的使用

DSPACE仿真平台的使用 文章目录DSPACE仿真平台的使用1.ModelDesk新建项目2.MotionDesk引入项目3.ControlDesk引入项目4.项目整合运行 我们使用DSPACE仿真平台软件来模拟小汽车的自动驾驶。 总共需要用到DSPACE大类下的三个软件:ModelDesk、ControlDesk、MotionDesk。 1.ModelDesk新建项目 在桌面上点击ModelDesk图标,打开DSPACE—ModelDesk 选择 File 标签,侧边栏选择 New,点击 New A

qq_42582489的博客 7550

[springMVC]Spring3MVC 在JSP中使用@ModelAttribute

原文地址: http://liukai.iteye.com/blog/973717 在教程中,我们将创建一个简单的Spring3MVC simple CRUD应用程序.  什么是@ModelAttribute  Spring3关于@ModelAttribute的文档  引用 @ModelAttribute has two usage scenario

wendellup的专栏 9302

Embarcadero Dev-C++ 6.3安装教程【保姆级】

Embarcadero Dev-C++ 6.3安装教程,保姆级教程~

Thebest_jack的博客 1万+

@ModelAttribute运用详解

用法1:用在入参 运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用; 用在方法的入参上依次做如下操作: 从隐含对象中获取隐含的模型数据 将请求参数绑定到隐含对象中 将隐含对象传入到入参 将入参绑定到Model 用法2:用在方法上 被@ModelAttribute注释的方法会在此controller的每个方法执行前被执行 ,如果有返回值,则自动将该返回值加入到ModelMap中。因此对于一个controller映射多个URL的用法

Hai-Yang-code的博客 4万+

安装keil5(MDK)及导入pack包教程

首先说明的是我安装的Keil版本为Keil V5.29.0.0 1、安装软件 右键管理员权限运行安装包 设置安装路径以及pack的存放路径 随意输入First Name和E-mail 安装驱动 完成软件安装,然后可以先关闭弹出的Pack Install(一会再添加) 2、激活一下软件 打开桌面的Keil File → License Management(我之前注册过了) 右...

hrx-@@ 10万+

ARM交叉编译gdbserver并远程在线调试

arm-linux平台。5、生成完整的makefile,新建full_install,执行 ./configure --target=arm-linux-gnueabihf --host=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- CC=arm-linux-gnueabihf-gcc --prefix=/home/geyuxi/Documents/full_install。如果--host和--build不同,是交叉编译;

wangjie36的博客 3178

ModelAttribute用法详解

这是个不错的办法,也比较灵活,但是对于当下的场景,controller类下的每一个方法还是要重复写代码,且如果新增了接口,接口里还要写重复代码,比较麻烦。这样写的好处是我们不需要在每个方法中都放入登录校验的代码,写在controller类下面的每个方法调用之前都会去调用getUserInfo方法验证登录状态,即便再写新的接口,也无需特殊处理。我们写一个基本类,其中包含了一个验证登录状态的接口getUserInfo,接口上有@ModelAttribute注解⬇️。

a20100997的博客 2861

ARMv8/ARMv9的TLB深度学习

千万不要人云亦云。一个TLB entry里有很多很多东西的,有100多个比特位呢,并不是仅仅只有VA/PA/nG/VMID/ASID等。.👉👉👉 个人博客笔记导读目录(全部) 👈👈👈。

baron-周贺贺-代码改变世界ctw 1576

ModelAttribute注解的两种用法

ModelAttribute注解概述 作用: 该注解是SpringMVC4.3版本以后新加入的。它可以用于修饰方法和参数。 出现在方法上,表示当前方法会在控制器的方法执行之前,先执行。它可以修饰没有返回值的方法,也可以修饰有具体返回值的方法。 出现在参数上,获取指定的数据给参数赋值。 属性: value:用于获取数据的key。key可以是POJO的属性名称,也可以是map结构的key。 应用...

清风不灭的博客 2017

SAP GUI 经典事务在 Fiori Launchpad 中的穿越之道:原理、配置与实践指南

在许多企业的 S/4HANA 或 ECC 系统里,经典 SAP GUI 事务依旧承载着关键业务。如何把这些老将自然地嵌入到 Fiori Launchpad,让用户在统一入口里无缝切换新旧 UI,是项目中经常遇到的主题。本文结合社区博文、官方文档与实际项目经验,系统梳理这一过程的技术原理、配置步骤、性能优化以及常见坑点,并给出贴近中国读者的案例解析与改进建议。阅读完毕,你将不仅知道怎么做,还会理解。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 631

在 SAP 中修改 Profile 文件中的主机名需要谨慎操作,需需确保所有相关配置保持一致

在SAP系统中修改Profile文件主机名需谨慎操作,主要步骤包括:1)停止相关SAP服务;2)重命名并编辑实例级Profile文件(如ER1_D00_OLDHOST改为ER1_D00_LRPSAP),修改所有含旧主机名的参数;3)可选更新默认Profile文件;4)在Windows系统中调整注册表配置;5)验证一致性后重启服务。关键注意:保持大小写统一、修改前备份文件、集群环境需同步所有节点。通过系统日志确认新主机名生效且无报错。

金牌架构师 1053

Spring MVC @ModelAttribute 详解

1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public void populateModel(@RequestParam String abc, Model model) { model.addAttribu

何静媛 4万+

2024年上半年试题二:论性能测试方法及其应用

2025年1月,我以系统分析师的身份参与了公司OA系统的开发与建设。该系统包含的功能有打卡签到、公文流转、工资查询等。我们吸取了之前由于系统使用人数激增暴露出的短时间卡顿等问题,所以在本次开发中,我们非常重视系统的性能测试。通过自动化的测试工具模拟多种正常、峰值及异常负载条件对系统的各项性能指标进行测试。性能测试在软件的质量保证中起着重要的作用,它包括的测试内容丰富多样,常见的负载测试和压力测试都属于性能测试。

银帅 563

Spring3MVC 在JSP中使用@ModelAttribute

在教程中,我们将创建一个简单的Spring3MVC simple CRUD应用程序.  什么是@ModelAttribute  Spring3关于@ModelAttribute的文档  引用 @ModelAttribute has two usage scenarios in controllers. When you place it on a method para

xiaozaoxi的专栏 1466

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Spring MVC @ModelAttribute注解

本文介绍在 Spring MVC 中非常重要的注解 @ModelAttribute,用来将请求参数绑定到 Model 对象。 在 Controller 中使用 @ModelAttribute 时,有以下几种应用情况。 应用在方法上 应用在方法的参数上 应用在方法上,并且方法也使用了 @RequestMapping 需要注意的是,因为模型对象要先于 controller 方法之前创建,所以被 @ModelAttribute 注解的方法会在 Controller 每个方法执行之前都执行。因此一个 Control

洛卡JAVA架构师每日分享Java架构师知识 937

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

spring mvc之@ModelAttribute注解

1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public void populateModel(@RequestParam String abc, Model model) { ...

diaoluo0692的博客 167

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

springmvc jsp java_(转)Spring3MVC 在JSP中使用@ModelAttribute

packageorg.liukai.tutorial.controller;importjava.util.ArrayList;importjava.util.List;importjavax.annotation.Resource;importorg.apache.log4j.Logger;importorg.liukai.tutorial.domain.Person;importorg.liu...

weixin_39710991的博客 217

Spring MVC @ModelAttribute

1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public void populateModel(@RequestParam String abc, Model model) { ...

96

@ModelAttribute注解详细使用

org.springframework.web.bind.annotation.ModelAttribute注解类型将请求参数绑定到Model对象。 @ModelAttribute注解只支持一个属性value,类型为String,表示绑定的属性名称。 提示:被@ModelAttribute注释的方法会在Controller每个方法执行前被执行,因此在一个Controller映射到多个URL时,要谨...

张睿的博客 1万+

Spring @ModelAttribute注解用法

之前项目中并自己并没有怎么使用到过@ModelAttribute这个注解,接手一个老项目的时候发现项目中大量使用@ModelAttribute这个注解,在这里就整理下这个注解常用的方式,也为自己做个记录,以免久了不用又忘记了 @ModelAttribute使用大致有有两种,一种是是直接标记在方法上,一种是标记在方法的参数中,两种标记方法产生的效果也各不相同,这里就列举下两种标记所产生的效果首先先做...

苏美尔人的天空 3136
上一篇: mysql-游标及存储过程游标的使用--源自技术
下一篇: 常用的SQL-看看、练练--源自技术
MyJava_Dx
博客等级 码龄11年 1粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址