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 ina controller class which is based on RequestMapping annotated handler methods, with such accessor 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类一定要有没有参数的构造函数。

From:http://blog.sina.com.cn/s/blog_6d3c1ec601017q4p.html

转载于:https://www.cnblogs.com/JavaTechLover/archive/2012/09/03/spring-controller4.html

[@Controller]4 详解@ModelAttribute相关推荐

  1. java spring mvc 上传_Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...

  2. k8s学习笔记(10)--- kubernetes核心组件之controller manager详解

    kubernetes核心组件之controller manager详解 一.Controller Manager简介 1.1 Replication Controller 1.2 Node Contr ...

  3. @Controller深入详解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  4. java文件下载controller_Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...

  5. Spring MVC 之@Controller@RequestMapping详解

    一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml   dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...

  6. UINavigation​Controller 的详解(基于 API )

    闲谈 最近做项目涉及到一个UINavigation​bar 的设置,网上给出的方法大多都很不全面,于是乎决定看 API 来研究一下 UINavigationController.至于我遇到的问题在本篇 ...

  7. ARM架构Generic Interrupt Controller(GIC)详解之术语介绍

    在上一篇博文ARM通用中断控制器GIC(generic Interrupt Controller)简介中,笔者对GIC做了一个整体的简介,接下来笔者将对GIC中的一些细节问题进行研究学习,首先要介绍的 ...

  8. Spring框架中entity层、dao层、service层、mapper层、controller层详解

    1. entity层 entity就是属性类,通常定义在model层里面,其相当于MVC的M层,属于数据模型层: 一般的实体类对应一个数据表,其中的属性定义数据表中的字段,实体类的字段数量 >= ...

  9. 跟我学Kafka之Controller控制器详解(一)

    我们的kafka源码分享已经进行过很多期了,主要的内容也都分享的差不多了,那么那么在今后的分享中,主要集中在kafka性能优化和使用 Kafka集群中的其中一个Broker会被选举为Controlle ...

最新文章

  1. python画二维散点图-基于python 二维数组及画图的实例详解
  2. 活动 | 壁咚小咖,霸道表白!分享我和PMcaff一起成长的故事
  3. Boost:BOOST_ASSERT扩展的用法测试程序
  4. 服务器软RAID和LVM的实现
  5. LiveVideoStackCon音视频技术大会首次来到上海
  6. 通过Jedis API使用排序集
  7. dubbo原理_dubbo的底层原理
  8. idea devtools 配置开关_最新版IDEA常用配置指南,打造你的最酷IDE
  9. Oracle之Union与Union all的区别
  10. crontab的使用
  11. (PHP 4, PHP 5) isset — 检测变量是否设置
  12. 【code vs】 2780 ZZWYYQWZHZ
  13. php 开源留言板,PHP开源多功能留言板(SyGuestBook)
  14. c语言必背数组函数100代码,C语言必背的典型程序设计题目 数组、函数参考答案.pdf...
  15. 智能家居控制系统完整设计方案
  16. node mysql菜鸟教程_Node.js 创建第一个应用
  17. java 中抽象类的继承_java抽象类 继承
  18. java根据word模板导出_Java通过word模板导出word
  19. MyBatisplus字段名与表名的映射
  20. 【深度好文】香港富豪卧底贫民窟:“你没出息,是因为不努力”害了多少程序员

热门文章

  1. MySQL中文全文索引
  2. 智能布线—更好的安全性
  3. $emit传递多个参数_Go语言参数传递方式
  4. 开源库OpenNMT-py使用记录
  5. 【Python-ML】SKlearn库原型聚类KMeans
  6. 我的机器学习入门之路(中)——深度学习(自然语言处理)
  7. 说说牛顿迭代 -- 方法篇
  8. Linux个人用户安装JDK(与root用户不同版本)
  9. es配置中防止脑裂的配置
  10. Python 技术篇-多线程的2种创建方法,多线程的简单用法,快速上手