spring mvc之@ModelAttribute注解
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注解的更多相关文章
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- Spring MVC SessionAttributes ModelAttribute注解
说明 本文主要针对 @SessionAttributes注解 和 @ModelAttribute注解的基础用法进行解析.至于为什么会将这两个注解放在一起,是因为它们之间还是有点影响的. @Sessio ...
- 0001 - Spring MVC中的注解
1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- Spring MVC中@RequestMapping注解使用技巧(转)
@RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...
- Spring MVC常用的注解类
一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring MVC常用的注解
@Controller @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为 类名称开头字母小写,你也可以自己指定,如下 方法一: @Controller ...
随机推荐
- UVa 727 - Equation
题目大意:给一个中缀表达式,转换成后缀表达式. 这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了.今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现 ...
- shell 整理,更新,记录
菜鸟级awk,读取android manifest,把key->value 保存到path.rc. awk -F "name=" '{if(/project path/) p ...
- sqlite3命令行
1.查看版本信息 sqlite3 -verion 2.创建/打开数据库 sqlite3 数据库名 例:sqlite3 test.db 如果test.db不存在就创建 如果存在,则打开3.退出 .q/. ...
- robotium从入门到放弃 三 基于apk的自动化测试
1.apk重签名 在做基于APK的自动化测试的过程中,需要确保的一点是,被测试的APK必须跟测试项目具有相同的签名,那怎么做才能确保两者拥有相同的签名呢?下面将给出具体的实现方法. 首先将被测 ...
- Delphi制作图像特殊显示效果
Delphi制作实现图像的各种显示效果,比如百叶窗.渐变.淡入淡出.水平交错.雨滴效果等,用鼠标点击“打开图像”按钮,可以选择图像文件导入到窗体中:点击其它各个按钮,可以实现图像显示特效,例如:点击“ ...
- js原生设计模式——8单例模式之简约版属性样式方法库
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- js原生之函数
1.函数作为参数传给其他函数: data.sort(function(a,b){return a-b}) //关于数组的sort函数,其回调函数返回负值,a在b之前 //正值,b在a ...
- 建立、配置和使用Activity——使用Bundle在Activity之间交换数据
当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...
- HDU-1995-汉诺塔V
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1995 这题不知道该说水还是不水,对于这题我看到题目数据,就有了想法,因为题目数据给的好 所以我直接 假 ...
- 在ubuntu下增加root用户并登录
1.打开终端. 2.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 3.在弹出的编辑框里输入:greeter-show-ma ...