1.@ModelAttribute注释void返回值的方法

@Controller
public class HelloModelController { @ModelAttribute
public void populateModel(@RequestParam String abc, Model model) {
model.addAttribute("attributeName", abc);
} @RequestMapping(value = "/helloWorld")
public String helloWorld() {
return "helloWorld.jsp";
} }

  在这个代码中,访问控制器方法helloWorld时,会首先调用populateModel方法,将页面参数abc(/helloWorld.ht?abc=text)放到model的attributeName属性中,在视图中可以直接访问。

  jsp页面页面如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
</head>
<body>
<c:out value="${attributeName}"></c:out>
</body>
</html>

2.@ModelAttribute注释返回具体类的方法

@Controller
public class Hello2ModelController { @ModelAttribute
public User populateModel() {
User user=new User();
user.setAccount("ray");
return user;
}
@RequestMapping(value = "/helloWorld2")
public String helloWorld() {
return "helloWorld.jsp";
}
}

  当用户请求 http://localhost:8080/test/helloWorld2.html时,首先访问populateModel方法,返回User对象,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回User类型,那么这个model属性的名称是user。 
  这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。

  jsp 中如下访问:

<c:out value="${user.account}"></c:out> 

  也可以指定属性名称

@Controller
public class Hello2ModelController { @ModelAttribute(value="myUser")
public User populateModel() {
User user=new User();
user.setAccount("ray");
return user;
}
@RequestMapping(value = "/helloWorld2")
public String helloWorld(Model map) {
return "helloWorld.jsp";
}
}

  jsp中如下访问:

<c:out value="${myUser.account}"></c:out>  

  对象合并:

@Controller
public class Hello2ModelController { @ModelAttribute
public User populateModel() {
User user=new User();
user.setAccount("ray");
return user;
} @RequestMapping(value = "/helloWorld2")
public String helloWorld(User user) {
user.setName("老王");
return "helloWorld.jsp";
}
}

  对象合并指定对象名称:

@Controller
public class Hello2ModelController { @ModelAttribute("myUser")
public User populateModel() {
User user=new User();
user.setAccount("ray");
return user;
} @RequestMapping(value = "/helloWorld2")
public String helloWorld(@ModelAttribute("myUser") User user) {
user.setName("老王");
return "helloWorld.jsp";
}
}

  这样在jsp中可以使用如下方式访问

<c:out value="${myUser.name}"></c:out>
<c:out value="${myUser.account}"></c:out>

3.通过此特性控制权限

  我们可以在基类方法中控制写此注解,需要控制权限的控制器,继承控制器就可以了。

public class BaseController {  

    @ModelAttribute
public void populateModel() throws Exception {
SysUser user=ContextUtil.getCurrentUser();
if(user.getAccount().equals("admin")){
throw new Exception("没有权限");
}
}
}

  需要控制权限的类继承BaseController

@Controller
public class Hello2ModelController extends BaseController { @RequestMapping(value = "/helloWorld2")
public String helloWorld(@ModelAttribute("myUser") User user) {
user.setName("老王");
return "helloWorld.jsp";
}
}

  这样就可以控制权限了,当然控制权限的方法有很多,比如通过过滤器等。这里只是提供一种思路。

4.总结

  @ModelAttribute具有如下三个作用:

  ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时使用。其实@ModelAttribute此处对于供视图页面展示来说与model.addAttribute("attributeName", abc);功能类似。

public String test(@ModelAttribute("user") UserModel user)  

  此处多了一个注解@ModelAttribute("user"),它的作用是将该绑定的命令对象以“user”为名称添加到模型对象中供视图页面展示使用。我们此时可以在视图页面使用${user.username}来获取绑定的命令对象的属性。

  ②暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。

public @ModelAttribute("user2") UserModel test3(@ModelAttribute("user2") UserModel user) 

  大家可以看到返回值类型是命令对象类型,而且通过@ModelAttribute("user2")注解,此时会暴露返回值到模型数据( 名字为user2 ) 中供视图展示使用

  @ModelAttribute 注解的返回值会覆盖@RequestMapping 注解方法中的@ModelAttribute 注解的同名命令对象

  ③暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用对象,如注册时需要选择的所在城市等,而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加到模型对象中,用于视图页面展示时使用。

Spring MVC @ModelAttribute的更多相关文章

  1. [Spring MVC] - @ModelAttribute使用

    在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里. 如果把@ModelAttrib ...

  2. Spring MVC @ModelAttribute详解

    被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...

  3. Spring MVC @ModelAttribute注解

    在一个Controller内,被@ModelAttribute标注的方法会在此controller的每个handler方法执行前被执行. 被@ModelAttribute标注的方法的参数绑定规则和普通 ...

  4. spring mvc @ModelAttribute 每次执行requestmapping前自动执行

    在不少应用场景中,我们希望在每次执行requestmapping前自动执行一些操作,比如把某些数据(比如数据字典.系统配置.标准错误号,这在企业应用系统中极为常见)塞到model中供view访问,因为 ...

  5. Spring MVC @ModelAttribute 详解

    1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...

  6. Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程

    初学 Spring MVC , 感觉对于 @ModelAttribute 和 @SessionAttributes 是如何被Spring MVC处理的,这一流程不是很清楚, 经过Google资料,有了 ...

  7. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  8. spring mvc:@RequestParam与@ModelAttribute异同

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

  9. spring mvc:注解@ModelAttribute妙用

    在Spring mvc中,注解@ModelAttribute是一个非常常用的注解,其功能主要在两方面: 运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入Model ...

随机推荐

  1. MySQL查询本周、上周、本月、上个月份数据的sql代码(转)

    感谢:http://www.jb51.net/article/32277.htm ----------------------------------------------------------- ...

  2. 【分块】bzoj3343: 教主的魔法

    3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 631  Solved: 272[Submit][Status][Discuss ...

  3. 【线段树/数学/扩展欧几里得】 Bzoj 3913:奇数国

    Description 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜 ...

  4. jQuery的优点——(一)

    稍微学点js然后就用起jQuery,用久了,就好像自己都成高手了,想得少了,都觉得自己就是js高手了.哗啦啦的惭愧啊=.= 面试GY的时候,因为在简历上写着大大的会用jQuery快速开发原型页面,然后 ...

  5. 【转载】test和cmp比较

    标 题:test和cmp一个很菜很基础的话题! 作 者: FTBirthday 时 间:2003/05/19 01:14am 链 接:http://bbs.pediy.com 看过破解教程,都知道te ...

  6. Jetty实践-Hello World

    该程序摘自官网教程: 1.首先去Jetty官网,下载jetty-distribution-9.2.6.v20141205.zip,Jetty Jar包,解压到任意目录: 2.使用Eclipse新建一个 ...

  7. 谈 DevOps 自动化时,也应该考虑到 SOX 等法案

    [编者按]作者 Aaron Volkmann 是 CERT Division 高级研究员,在本文中,他对 DevOps 自动化违反 SOX 法案进行了阐述.同时,也简单的提出了如何通过 CI 来避免这 ...

  8. Python SyntaxError: Non-ASCII character '\xe5'

    error: SyntaxError: Non-ASCII character '\xe5' in file D:\worklife\workshop\myCrawler\src\mainDriver ...

  9. 不定长内存池之apr_pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  10. hdu 4678 Mine 博弈论

    这是一题简单的博弈论!! 所有的空白+边界的数字(个数为n)为一堆,容易推出其SG函数值为n%2+1: 其他所有的数字(个数为m)的SG值为m%2. 再就是用dfs将空白部分搜一下即可!(注意细节) ...