@ModelAttribute运用详解
@ModelAttribute使用详解
1.@ModelAttribute注释方法
例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。
(1)@ModelAttribute注释void返回值的方法
- <span style="font-size:12px;">@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";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这个例子,在获得请求/helloWorld 后,populateModel方法在helloWorld方法之前先被调用,它把请求参数(/helloWorld?abc=text)加入到一个名为attributeName的model属性中,在它执行后helloWorld被调用,返回视图名helloWorld和model已由@ModelAttribute方法生产好了。
这个例子中model属性名称和model属性对象有model.addAttribute()实现,不过前提是要在方法中加入一个Model类型的参数。
当URL或者post中不包含次参数时,会报错,其实不需要这个方法,完全可以把请求的方法写成,这样缺少此参数也不会出错
- <span style="font-size:12px;"> @RequestMapping(value = "/helloWorld")
- public String helloWorld(String abc) {
- return "helloWorld";
- }</span>
- <span style="font-size:12px;">
- </span>
(2)@ModelAttribute注释返回具体类的方法
<span style="font-size:12px;">@ModelAttribute
- public Account addAccount(@RequestParam String number) {
- return accountManager.findAccount(number);
- }
- </span>
- <span style="font-size:12px;">
- </span>
这种情况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。
这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。
(3)@ModelAttribute(value="")注释返回具体类的方法
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @ModelAttribute("attributeName")
- public String addAccount(@RequestParam String abc) {
- return abc;
- }
- @RequestMapping(value = "/helloWorld")
- public String helloWorld() {
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无须要特定的参数。
(4)@ModelAttribute和@RequestMapping同时注释一个方法
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @RequestMapping(value = "/helloWorld.do")
- @ModelAttribute("attributeName")
- public String helloWorld() {
- return "hi";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称由RequestToViewNameTranslator根据请求"/helloWorld.do"转换为逻辑视图helloWorld。
Model属性名称有@ModelAttribute(value=””)指定,相当于在request中封装了key=attributeName,value=hi。
2.@ModelAttribute注释一个方法的参数
(1)从model中获取
- <span style="font-size:12px;">@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";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。
此时如果方法体没有标注@SessionAttributes("user"),那么scope为request,如果标注了,那么scope为session
(2)从Form表单或URL参数中获取(实际上,不做此注释也能拿到user对象)
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @RequestMapping(value = "/helloWorld")
- public String helloWorld(@ModelAttribute User user) {
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
注意这时候这个User类一定要有没有参数的构造函数。
转自http://blog.csdn.net/li_xiao_ming/article/details/8349115,感谢分享。
@ModelAttribute运用详解的更多相关文章
- (转)spring学习之@ModelAttribute运用详解
@ModelAttribute使用详解 1 @ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被 ...
- [转载]springmvc学习之@ModelAttribute运用详解
spring学习之@ModelAttribute运用详解 链接
- @ModelAttribute注解详解
@ModelAttribute注解详解 1.@ModelAttribute定义: 被该注解定义的方法,会在该方法所在的controller的任何目标方法执行之前执行 2.@ModelAttribute ...
- @ModelAttribute运用详解(二十一)
@ModelAttribute使用详解 1.@ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法 ...
- spring学习之@ModelAttribute运用详解
@ModelAttribute使用详解 1.@ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法 ...
- 08_springmvc数据回显和@ModelAttribute注解详解
一.数据回显 提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面. 二.pojo数据回显方法 1.springmvc默认对pojo数据进行回显. pojo数据传入controller方法后,s ...
- @ModelAttribute使用详解
1.@ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个control ...
- SpringMVC5中,@ModelAttribute注解详解
看这个注解的前提最好熟悉一下SpringMVC的model组件,该注解可以有五种使用方式: ①②③为 @ModelAttribute 跟@RequestMapping 分开修饰方法,被@ModelAt ...
- [转]springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
随机推荐
- Java @Override报错
问题 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下错误:The method *** of type *** must override a superclass ...
- 数字图像处理- 3.4 空间滤波 and 3.5 平滑空间滤波器
3.4 空间滤波基础 • Images are often corrupted by random variations in intensity, illumination, or have poo ...
- MathML + MathJax在网页中插入公式
http://www.mathjax.org/download/ http://www.w3.org/Math/Software/mathml_software_cat_editors.html ht ...
- swiper笔记
1.基本使用 var OrderMenu = new Swiper('#OrderMenu',{ loop: false, // 是否循环 autoplay: 1000, // 时间 slidesPe ...
- python 二分法查找实例(递归、循环)
二分法,主要应用于有序序列中,原理是每次查找都将原序列折半,逐渐缩小查找范围的一种算法. 需求 要求在一个有序序列中,例如[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ...
- Spring与Quartz的整合实现定时任务调度 以及crontab的用法
最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考. 我使用的是Maven来管理项目,需要的Jar包我给大家贴 ...
- SCWS分词扩展在UNIX/LINUX下的安装方法
<?php/** * 中文分词处理方法 *+--------------------------------- * @param stirng $string 要处理的字符串 * @param ...
- SSH+Ext+mysql快速开发
一.需要知识点 1.SSH整合 2.EXT使用 以及深入细节点 二.小功能实现 1.Servlet实现校验码验证 2.首页布局实现 3.struts错误信息显示(struts标签使用) 4.首页整体布 ...
- WKWebView使用及注意点
iOS8之后,苹果推出了WebKit这个框架,用来替换原有的UIWebView,新的控件优点多多,不一一叙述.由于一直在适配iOS7,就没有去替换,现在仍掉了iOS7,以为很简单的就替换过来了,然而在 ...
- oracle 中的round()函数、null值,rownum
round()函数:四舍五入函数 传回一个数值,该数值按照指定精度进行四舍五入运算的结果. 语法:round(number[,decimals]) Number:待处理的函数 Decimals:精度, ...