`
liukai
  • 浏览: 702260 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3MVC 在JSP中使用@ModelAttribute

阅读更多
在教程中,我们将创建一个简单的Spring3MVC simple CRUD应用程序.

什么是@ModelAttribute

Spring3关于@ModelAttribute的文档
引用

@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在方法级别上提供参考模型的数据.

英语不好.见谅

根据配合m2eclipse创建一个标准的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;

	private Integer id;
	private String firstName;
	private String lastName;
	private String currency;
	private Double money;

  ......getter/setter

}


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

AddressService.java
package org.liukai.tutorial.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.liukai.tutorial.domain.Address;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 同样用于显示.
 * 
 * @author liukai
 * 
 */
@Service("addressService")
@Transactional
public class AddressService {

	protected static Logger logger = Logger.getLogger("service");

	/**
	 * 获得所有的地址.
	 * 
	 * @return
	 */
	public List<Address> getAll() {
		logger.debug("Retrieving all addresses");

		List<Address> addresses = new ArrayList<Address>();

		// New address
		Address address = new Address();
		address.setId(1);
		address.setStreet("1 Street");
		address.setCity("City 1");
		address.setZipCode("11111");

		// Add to list
		addresses.add(address);

		// New address
		address = new Address();
		address.setId(2);
		address.setStreet("2 Street");
		address.setCity("City 2");
		address.setZipCode("22222");

		// Add to list
		addresses.add(address);

		// New address
		address = new Address();
		address.setId(3);
		address.setStreet("3 Street");
		address.setCity("City 3");
		address.setZipCode("33333");

		// Add to list
		addresses.add(address);

		// Return all addresses
		return addresses;
	}

}


PersonService.java


package org.liukai.tutorial.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.liukai.tutorial.domain.Person;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注意这个只是一个临时数据用于显示.
 * 
 * @author liukai
 * 
 */
@Service("personService")
@Transactional
public class PersonService {

	protected static Logger logger = Logger.getLogger("service");

	private HashMap<Integer, Person> database = new HashMap<Integer, Person>();

	/**
	 * Initialize a list of persons
	 */
	private void init() {
		// New person
		Person person = new Person();
		person.setId(1);
		person.setFirstName("John");
		person.setLastName("Smith");
		person.setCurrency("Dollar");
		person.setMoney(1500.00);

		// Add to list
		database.put(1, person);

		// New person
		person = new Person();
		person.setId(2);
		person.setFirstName("Jane");
		person.setLastName("Adams");
		person.setCurrency("Yen");
		person.setMoney(1000.00);

		// Add to list
		database.put(2, person);

		// New person
		person = new Person();
		person.setId(3);
		person.setFirstName("Mike");
		person.setLastName("Polaski");
		person.setCurrency("Euro");
		person.setMoney(2000.00);

		// Add to list
		database.put(3, person);
	}

	public PersonService() {
		// Initialize dummy database
		init();
	}

	/**
	 * 检索所有的 persons
	 */
	public List<Person> getAll() {
		logger.debug("Retrieving all persons");

		// Initialize our array
		List<Person> persons = new ArrayList<Person>();

		// Iterate the database
		for (Map.Entry<Integer, Person> entry : database.entrySet()) {
			persons.add(entry.getValue());
		}

		// Return all persons
		return persons;
	}

	/**
	 * 根据ID获得对应的Perosn
	 */
	public Person get(Integer id) {
		logger.debug("Retrieving person based on his id");
		return database.get(id);
	}

	/**
	 * 修改Person
	 */
	public void edit(Person person) {
		logger.debug("Editing existing person");

		// Note this is not the best way to update a data!

		// Delete existing user
		database.remove(person.getId());
		// Add updated user
		database.put(person.getId(), person);
	}

}



接下来就是@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文档

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

AddressController.java

package org.liukai.tutorial.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.liukai.tutorial.domain.Address;
import org.liukai.tutorial.service.AddressService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/address")
public class AddressController {

	protected static Logger logger = Logger.getLogger("controller");

	@Resource(name = "addressService")
	private AddressService addressService;

	/**
	 * 获得所有Addresses,并使他们成为一个model. 
	 * 注意@ModelAttribute作用于method级别上时.会在@Controller执行时加载
	 * method里的方法.即在@Controller执行时@ModelAttribute添加参数.
	 * 返回于该@Controller返回的所有JSP页面.
	 */
	@ModelAttribute("addresses")
	public List<Address> getAllAddresses() {
		return addressService.getAll();
	}

	/**
	 * 处理和检索一个包含addresses 的JSP Page .
	 */
	@RequestMapping(value = "list1", method = RequestMethod.GET)
	public String getAllUsingModelAttribute() {
		logger.debug("Received request to show all addresses page");

		// 他会解析 /WEB-INF/jsp/addressespage.jsp
		return "addressespage";
	}

	/**
	 * 处理和检索一个包含addresses 的JSP Page .
	 * 
	 * @return the name of the JSP page
	 */
	@RequestMapping(value = "list2", method = RequestMethod.GET)
	public String getAllUsingModel(Model model) {
		logger.debug("Received request to show all addresses page");

		// 检索所有的address并装入model返回addressespage.jsp
		model.addAttribute("addresses", addressService.getAll());
		model.addAttribute("greetings", "I came from Model not ModelAttribute");

		// This will resolve to /WEB-INF/jsp/addressespage.jsp
		return "addressespage";
	}

}



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


创建一个JSP页面:addressespage.jsp

addressespage.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h1>Addresses</h1>

	<table>
		<tr>
			<td width="100">Id</td>
			<td width="150">Street</td>
			<td width="150">City</td>
			<td width="150">Zip Code</td>
		</tr>
		<c:forEach items="${addresses}" var="address">
			<tr>
				<td><c:out value="${address.id}" />
				</td>
				<td><c:out value="${address.street}" />
				</td>
				<td><c:out value="${address.city}" />
				</td>
				<td><c:out value="${address.zipCode}" />
				</td>
			</tr>
		</c:forEach>
	</table>

	<p>${greetings}</p>
</body>
</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对象


package org.liukai.tutorial.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.liukai.tutorial.domain.Person;
import org.liukai.tutorial.service.PersonService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/main")
public class MainController {

	protected static Logger logger = Logger.getLogger("controller");

	@Resource(name = "personService")
	private PersonService personService;

	/**
	 * 获得所有Person,并使他们成为一个model.
	 */
	@ModelAttribute("persons")
	public List<Person> getAllPersons() {
		logger.debug("Retrieving all persons and adding it to ModelAttribute");
		// Delegate to PersonService
		return personService.getAll();
	}

	/**
	 * 获得所有的货币类型,并使他们成为一个model.
	 */
	@ModelAttribute("currencies")
	public List<String> getAllCurrencies() {
		logger.debug("Retrieving all currencies and adding it to ModelAttribute");

		// Prepare data
		List<String> currencies = new ArrayList<String>();
		currencies.add("Dollar");
		currencies.add("Yen");
		currencies.add("Pound");
		currencies.add("Euro");
		currencies.add("Dinar");

		return currencies;
	}

	/**
	 * 处理和检索一个包含Perosn 的JSP Page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getAllPage(Model model) {

		logger.debug("Received request to show all persons page");

		// personsage.jsp会引用一个名叫persons的model attribute
		// 我们不需要手动添加这个model
		// 前面他已经自动的通过@ModelAttribute("persons")进行了添加.

		// 他会解析 /WEB-INF/jsp/personspage.jsp
		return "personspage";
	}

	/**
	 * 检索修改页面
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getEdit(@PathVariable Integer id, Model model) {
		/*
		 * @PathVariable表示指定@RequestMapping的URL模板中{}里的值 
		 * 相当于以前我们URL后面传的参数如XX?id=XXXX .
		 * 但是现在我们可以用 XX/id/XX来代替. 
		 * 这个就是REST风格的URL,我们可以实现非常复杂的URL模板
		 */

		logger.debug("Received request to show edit page");

		/*
		 * 根据ID检索出对应的Person,然后把检索出来的Person放入一个叫
		 * "personAttribute"的model中.
		 * 这样editpage.jsp就会接收一个 名为personAttribute的参数.
		 * 相当于request.setAttribute("XX",XX)
		 */
		model.addAttribute("personAttribute", personService.get(id));

		// This will resolve to /WEB-INF/jsp/editpage.jsp
		return "editpage";
	}

	/**
	 * 保存修改结果
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String saveEdit(@ModelAttribute("personAttribute") Person person,
			@PathVariable Integer id, Model model) {
		logger.debug("Received request to update person");

		// 我们从页面接收到了一个名为"personAttribute"的model并命名为person
		// 同样我们也获得了指定的id.
		person.setId(id);

		// 更新person
		personService.edit(person);

		// 在更新后我们重新显示所有Person 的页面
		model.addAttribute("persons", personService.getAll());

		// This will resolve to /WEB-INF/jsp/personspage.jsp
		return "personspage";
	}

}



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

@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    public String saveEdit(@ModelAttribute("personAttribute") Person person, 
      @PathVariable Integer id, Model model) {
...
}

表示从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

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>Persons</h1>

<table>
	<tr>
		<td width="50">Id</td>
		<td width="150">First Name</td>
		<td width="150">Last Name</td>
		<td width="100">Money</td>
		<td width="50">Currency</td>
	</tr>
	<c:forEach items="${persons}" var="person">
		<tr>
			<td><c:out value="${person.id}" /></td>
			<td><c:out value="${person.firstName}" /></td>
			<td><c:out value="${person.lastName}" /></td>
			<td><c:out value="${person.money}" /></td>
			<td><c:out value="${person.currency}" /></td>
		</tr>
	</c:forEach>
</table>

</body>
</html>

这个主要是映射 /main.


editpage.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>Edit Person</h1>

<c:url var="saveUrl" value="/main/edit/${personAttribute.id}" />

<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
	<table>
		<tr>
			<td><form:label path="id">Id:</form:label></td>
			<td><form:input path="id" disabled="true"/></td>
		</tr>
	
		<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><form:label path="money">Money</form:label></td>
			<td><form:input path="money"/></td>
		</tr>
		
		<tr>
			<td><form:label path="currency">Currency:</form:label></td>
			<td><form:select path="currency"  items="${currencies}"/></td>
		</tr>
	</table>
	
	<input type="submit" value="Save" />
</form:form>

</body>
</html>

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


@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String getEdit(@PathVariable Integer id, Model model) {
...
}


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



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

  @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    public String saveEdit(@ModelAttribute("personAttribute") Person person, 
      @PathVariable Integer id, Model model) {
...
}


整个项目的目录结构如下




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




BTW:附件为本次教程源码.你可以下载后直接在tomcat或其他web服务器启动.也可以自行添加
maven插件启动.


  • 大小: 14.9 KB
  • 大小: 2.9 KB
  • 大小: 3.6 KB
  • 大小: 3.7 KB
  • 大小: 23 KB
分享到:
评论
14 楼 liukai 2014-04-10  
hkbird 写道
有点疑问啊.
public String saveEdit(@ModelAttribute("personAttribute") Person person,   
      @PathVariable Integer id, Model model) {  


不能写成这样吗?
public String saveEdit(Person person,   
      @PathVariable Integer id, Model model) {  


spring 不是能帮我们转换成我们的对象吗?

应该是可以的,另外这个教程比较老了.目前从我工作的经验来看不推荐使用ModelAttribute
还是一个参数一个参数的接收较好.
13 楼 hkbird 2014-04-09  
有点疑问啊.
public String saveEdit(@ModelAttribute("personAttribute") Person person,   
      @PathVariable Integer id, Model model) {  


不能写成这样吗?
public String saveEdit(Person person,   
      @PathVariable Integer id, Model model) {  


spring 不是能帮我们转换成我们的对象吗?
12 楼 happylouis 2013-11-22  
楼主,你的文章给了我极大地帮助
11 楼 yydriver 2013-09-11  
看了LZ的文章,顿时茅塞顿开,非常感谢。
解释的很清晰。
10 楼 liubey 2013-06-08  
写的真心不错
9 楼 wendellup_account1 2012-12-23  
Merrygrass 写道
public  String save(@ModelAttribute Personnel personnel,@ModelAttribute User user)
楼主请问试过这样吗? 并且两个对象里面都有id,name能正确获取吗?


页面上将两个entity封装成一个VO就可以了吧。。id,name,分别表示。

楼主。你的文章我转了。谢了啊。
8 楼 Merrygrass 2012-09-07  
public  String save(@ModelAttribute Personnel personnel,@ModelAttribute User user)
楼主请问试过这样吗? 并且两个对象里面都有id,name能正确获取吗?
7 楼 Merrygrass 2012-09-07  
public  String save(@ModelAttribute Personnel personnel,@ModelAttribute User user)
6 楼 Merrygrass 2012-09-07  
public  String save(@ModelAttribute Personnel personnel,@ModelAttribute User user){}
5 楼 idi0t 2012-08-03  
  真的不错
4 楼 vip 2012-07-30  
  谢谢分享 终于对spring有些了解了
3 楼 ordinarynan 2012-07-04  
写的真详细啊
2 楼 ubersexual 2012-04-25  
       
1 楼 laijavatoo 2012-04-23  

相关推荐

    Spring MVC 之@ModelAttribute使用.rar

    Spring MVC 之@ModelAttribute使用.rar

    spring mvc中的@ModelAttribute注解示例介绍

    在Spring mvc中,注解@ModelAttribute是一个非常常用的注解,下面这篇文章主要给大家介绍了关于spring mvc中@ModelAttribute注解的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。

    spring mvc中注解@ModelAttribute的妙用分享

    主要给大家介绍了关于spring mvc中注解@ModelAttribute妙用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Android具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

    [Spring MVC] - ModelAttribute使用.docx

    在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里。 如果把@ModelAttribute放在方法的注解上时,代表的是: 该Controller的所有方法在...

    SpringMVCDemo:Spring MVC 框架知识案例

    3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向案例 8.Spring MVC 下载 Excel 文档的需求...

    spring3 MVC实战

    spring3 MVC实战

    Spring MVC--2.@RequestMapping 映射请求

    Spring MVC--2.@RequestMapping 映射请求

    精通Spring MVC 4

    , 在精通Spring MVC4中,我们将会从头开始构建一个有用的Web应用。本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、...

    Servlet JSP和Spring MVC初学指南

    Servlet JSP和Spring MVC初学指南

    spring3mvc真正入门资料

    Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。 Spring3 MVC的优点: 1、...

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....

    Spring3MVC注解教程.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及...

    spring3mvc导入包

    spring3mvc导入包大全。里面拥有spring3MVC框架搭建的必备包。新手学习spring3必备

    spring-mvc-官方中文文档

    spring-mvc-官方中文文档

    spring mvc RequestParam与 ModelAttribute异同.docx

    关于spring mvc中的两个注解:@RequestParam、@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别。

    spring3MVC 框架demo

    spring3MVC框架基本配置案例,里面两个功能案例,上传下载功能和使用xheditor发布消息功能

Global site tag (gtag.js) - Google Analytics