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";
}
}

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

总结:

@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 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...

  2. Spring MVC SessionAttributes ModelAttribute注解

    说明 本文主要针对 @SessionAttributes注解 和 @ModelAttribute注解的基础用法进行解析.至于为什么会将这两个注解放在一起,是因为它们之间还是有点影响的. @Sessio ...

  3. 0001 - Spring MVC中的注解

    1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...

  4. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  5. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  6. Spring MVC中@RequestMapping注解使用技巧(转)

    @RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...

  7. Spring MVC常用的注解类

    一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...

  8. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  9. Spring MVC常用的注解

    @Controller @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为 类名称开头字母小写,你也可以自己指定,如下 方法一: @Controller ...

随机推荐

  1. java系列--EL和JSTL

    一.EL表达式语言 构成:${变量} 功能:可以从范围对象直接取值 默认为requestScope 如果访问的属性不存在,EL返回值为null,但在JSP页面中显示空字符串不显示 EL表达式可以出现的 ...

  2. apue- chapter 3 文件IO

    1.函数open和函数openat #include<fcnl.h> int open(const char *path,int oflag,.../*mode_t mode */) in ...

  3. Reactive 手机网络状态

    RAC([UIApplication sharedApplication], networkActivityIndicatorVisible)

  4. dubbo框架揭秘之服务引用

    ApplicationConfig config = new ApplicationConfig("hello-worldp"); RegistryConfig reg = new ...

  5. PHP导出MYSQL数据库并压缩

    PHP可以一键导出MYSQL备份文件,并压缩存放,尽管phpMyAdmin有这功能,不过若你自己开发网站或者是为别人写CMS,你不应该要求别人用你程序的时候再去另外用phpMyAdmin备份MYSQL ...

  6. iOS 之 Quartz2D

    1. Quartz2D 之 简单介绍 2. Quartz2D 之 简单使用 3. Quartz2D 之 绘制文本

  7. MySQL动态添删改列字段

    动态增加列字段: ); 动态删除列字段: alter table TableName drop column field_id; 动态修改列字段: alter table TableName chan ...

  8. 连接linux 服务器

    File > Quick Connect ,Hostname 是ip , Username是用户名

  9. http的Max-Forwards头的作用(转)

    请求头的Max-Forwards用来请求特定代理.当代理收到一个允许URI转发的OPTIONS请求,则检查Max-Forwards.如果Max-Forwards值为0,则不能转发该消息:相反,代理会将 ...

  10. 微信小程序来了,小程序都能做些什么

    2017年的微信大动作就是微信小程序了,到底小程序都能做些什么?这是很多人关注的热点,小程序开发对企业又有什么帮助呢?下面让厦门微信小程序开发公司来为你就分析下.       微信小程序与APP的关系 ...