SpringMVC @ModelAttribute 详解
[@Controller]4 详解@ModelAttribute
http://blog.sina.com.cn/s/blog_6d3c1ec601017q4p.html
A、@ModelAttribute
Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for RequestMapping annotated handler classes.
在被@RequestMapping注释的处理器类中,这个注释可以绑定一个方法参数或绑定一个方法的返回值到一个命名的模型属性,提供给一个视图。
Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of a RequestMapping annotated handler method).
可以用于把一个command对象提供给web视图,使用指定的属性名称,在被@RequestMapping注释的处理器方法中注释相关参数。
Can also be used to expose reference data to a web view through annotating accessor methods in a controller class which is based on RequestMapping annotated handler methods, with suchaccessor methods allowed to have any arguments that RequestMapping supports for handler methods, returning the model attribute value to expose.
可以用于提供数据给一个web视图,通过注释处理器方法,这个方法允许有任何参数,返回的模型属性值被提供。
A.1、@ ModelAttribute的属性
value
The name of the model attribute to bind to.
绑定的模型属性的名称。
The default model attribute name is inferred from the declared attribute type (i.e. the method parameter type or method return type), based on the non-qualified class name: e.g. "orderAddress" for class "mypackage.OrderAddress", or "orderAddressList" for "List<mypackage.OrderAddress>".
默认的模型属性名称自动判断声明的属性类型(如,方法参数类型或方法返回类型)。如这个值是orderAddress,就对于当前包. OrderAddress。
B、@ModelAttribute注释一个方法
An @ModelAttribute on a method indicates the purpose of that method is to add one or more model attributes. Such methods support the same argument types as @RequestMapping methods but cannot be mapped directly to requests. Instead @ModelAttribute methods in a controller are invoked before @RequestMapping methods, within the same controller.
被@ModelAttribute注释的方法表示这个方法的目的是增加一个或多个模型(model)属性。这个方法和被@RequestMapping注释的方法一样也支持@RequestParam参数,但是它不能直接被请求映射。实际上,控制器中的@ModelAttribute方法是在同一控制器中的@RequestMapping方法被调用之前调用的。
@ModelAttribute methods are used to populate the model with commonly needed attributes for example to fill a drop-down with states or with pet types, or to retrieve a command object like Account in order to use it to represent the data on an HTML form.
被@ModelAttribute注释的方法用于填充model属性,例如,为下拉菜单填充内容,或检索一个command对象(如,Account),用它来表示一个HTML表单中的数据。
A controller can have any number of @ModelAttribute methods. All such methods are invoked before @RequestMapping methods of the same controller.
一个控制器可以有任意数量的@ModelAttribute方法。所有这些方法都在@RequestMapping方法被调用之前调用。
Note the two styles of @ModelAttribute methods. In the first, the method adds an attribute implicitly by returning it. In the second, the method accepts a Model and adds any number of model attributes to it.
有两种类型的@ModelAttribute方法。一种是:加入只一个属性,用方法的返回类型隐含表示。另一种是:方法接受一个Model类型的参数,这个model可以加入任意多个model属性。
B.1、@ModelAttribute注释void返回值的方法
举例说明
@Controller
public class HelloWorldController {
@ModelAttribute
public void populateModel(@RequestParam String abc, Model model) {
model.addAttribute("attributeName", abc);
}
@RequestMapping(value = "/helloWorld")
public String helloWorld() {
return "helloWorld";
}
}
这个例子,在获得请求/helloWorld 后,populateModel方法在helloWorld方法之前先被调用,它把请求参数(/helloWorld?abc=text)加入到一个名为attributeName的model属性中,在它执行后helloWorld被调用,返回视图名helloWorld和model已由@ModelAttribute方法生产好了。
这个例子中model属性名称和model属性对象有model.addAttribute()实现,不过前提是要在方法中加入一个Model类型的参数。
B.2、@ModelAttribute注释返回具体类的方法
举例说明
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
这种情况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。
这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。
B.3、@ModelAttribute(value="")注释返回具体类的方法
举例说明
@Controller
public class HelloWorldController {
@ModelAttribute("attributeName")
public String addAccount(@RequestParam String abc) {
return abc;
}
@RequestMapping(value = "/helloWorld")
public String helloWorld() {
return "helloWorld";
}
}
这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无须要特定的参数。
B.4、@ModelAttribute和@RequestMapping同时注释一个方法
举例说明
@Controller
public class HelloWorldController {
@RequestMapping(value = "/helloWorld.do")
@ModelAttribute("attributeName")
public String helloWorld() {
return "hi";
}
}
这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称由RequestToViewNameTranslator根据请求"/helloWorld.do"转换为helloWorld。Model属性名称有@ModelAttribute(value=””)指定。
C、@ModelAttribute注释一个方法的参数
An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model.Once present in the model, the argument's fields should be populated from all request parameters that have matching names. This is known as data binding in Spring MVC, a very useful mechanism that saves you from having to parse each form field individually.
@ModelAttribute注释方法的一个参数表示应从模型model中取得。若在model中未找到,那么这个参数将先被实例化后加入到model中。若在model中找到,则请求参数名称和model属性字段若相匹配就会自动填充。这个机制对于表单提交数据绑定到对象属性上很有效。
B.1、从model中获取
It may already be in the model due to an @ModelAttribute method in the same controller
参数的值从当前控制器的@ModelAttribute方法提供的model属性中获取。
举例说明
@Controller
public class HelloWorldController {
@ModelAttribute("user")
public User addAccount() {
return new User("jz","123");
}
@RequestMapping(value = "/helloWorld")
public String helloWorld(@ModelAttribute("user") User user) {
user.setUserName("jizhou");
return "helloWorld";
}
}
在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。
B.2、从URI template变量中获取
B.3、从Form表单或URL参数中获取
举例说明
@Controller
public class HelloWorldController {
@RequestMapping(value = "/helloWorld")
public String helloWorld(@ModelAttribute User user) {
return "helloWorld";
}
}
注意这时候这个User类一定要有没有参数的构造函数。
SpringMVC @ModelAttribute 详解的更多相关文章
- SpringMVC @ModelAttribute详解
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...
- SpringMVC RequestMapping 详解
SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- 简单搭建SpringMVC框架详解
在公司待了两年,用的一直是Spring+SpringMVC+Hibernate框架,都是公司自己搭建好的,自己从来没有主动搭建过,闲来无聊,自己搭建试试.一下即我搭建的过程以及搭建所遇到的问题,有部分 ...
- SpringMVC配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
原文地址:https://www.cnblogs.com/lcngu/p/5080702.html Spring配置文件详解:<context:annotation-config/>和&l ...
- SpringMVC Controller详解
SpringMVC Controller 介绍 一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理 ...
- springmvc入门详解
首先,我们先写一个入门小案例,先熟悉一下springmvc是什么,了解一下springmvc的运行流程,对加强springmvc的深层理解有很大帮助 .第一步,创建一个maven项目: <?xm ...
- SpringMVC FistMVC详解
实现一个简单的SpringMVC框架的配置 1.依赖 这是mybatis+spring+现在需要的依赖 <dependency> <groupId>junit</grou ...
- SpringMVC 注解详解
SpringMVC常用注解说明 @Bean, @Configuration表示基于Java配置的类@Bean除了配置在@Configuration,也可以在@Component定义,此时没有特殊意义, ...
随机推荐
- JavaScript 设计模式之代理模式
一.代理模式概念解读 1.代理模式概念文字解读 代理,顾名思义就是帮助别人做事,GOF对代理模式的定义如下: 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.代理模式使得代理对象 ...
- xp看系统位数
运行cmd,看打开的cmd窗口标题一般c:\windows\system32代表32位操作系统,64位就是system64,详细系统信息运行命令“systeminfo”
- 重写lucene.net的分词器支持3.0.3.0版本
lucene.net中每个分词器都是一个类,同时有一个辅助类,这个辅助类完成分词的大部分逻辑.分词类以Analyzer结尾,辅助类通常以Tokenizer结尾.分类词全部继承自Analyzer类,辅助 ...
- PopupWindow的简单使用(结合RecyclerView)
Android弹窗: 在Android中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利.关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindo ...
- Singleton - 单例模式和Double-Checked Locking - 双重检查锁定模式
问题描述 现在,不管开发一个多大的系统(至少我现在的部门是这样的),都会带一个日志功能:在实际开发过程中,会专门有一个日志模块,负责写日志,由于在系统的任何地方,我们都有可能要调用日志模块中的函数,进 ...
- java实现简单webserver(分析+源码)
在日常的开发中,我们用过非常多开源的webserver,比如tomcat.apache等等.如今我们自己实现一个简单的webserver,主要的功能就是用户点击要訪问的资源,server将资源发送到c ...
- stm32 usart的几种通信模式
一 USART 通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换. USART支持同步单向通信和半双工单线通信,也支持LIN(局 ...
- mysql创建数据库和删除数据库
1.创建数据库 启动MySQL 服务之后,输入以下命令连接到MySQL 服务器: [mysql@db3 ~]$ mysql -uroot -p Enter password: Welcome to t ...
- 使用matlab处理图像的基础知识
MATLAB基本函数一 矩阵运算 1.基本算数运算(加减乘除) + -运算要求矩阵维数相同,例m*n * /运算,例A=B*C,B矩阵是m*n矩阵,B是n*p矩阵,则A是m*p矩阵 A/B相当于A*i ...
- Spring的三种注入方式(Setter、构造函数和自动注入)
一.Setter注入 这里我是希望在Student.java中调用Course.java中的内容. public class Course { public String name = "数 ...