欢迎关注微信公众号: 小哈学Java

文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute

Spring Boot 2.0 中的注解 @ModelAttribute 有什么作用呢?

通常情况下,我们会将 @ModelAttribute 注解放置在 Controller 中的某个方法上,那么,如果您在请求这个 Controller 中定义的 URI 时,会首先调用这个被注解的方法,并将该方法的结果作为 Model 的属性,然后才会调用对应 URI 的处理方法。

一、@ModelAttribute 使用场景

我们通常会通过 @ModelAttribute 来向某个 Controller 中需要的公共模型 Model 中添加数据。如下面的示例代码所示。

二、示例代码

@ModelAttribute
public void findUserById(@PathVariable("userId") Long userId, Model model) {
model.addAttribute("user", userService.findUserById(userId));
} @GetMapping("/user/{userId}")
public String findUser(Model model) {
System.out.println(model.containsAttribute("user"));
return "success !";
}

当我们请求接口 /user/1 时,会先调用 findUserById 方法,方法内,通过 userId 查询到对应的 User 对象放置到 Model 模型中。

需要注意,如果您仅仅只是添加一个对象到 Model 模型中,上面的代码还可以再精炼一点:

@ModelAttribute
public User findUserById(@PathVariable("userId") Long userId) {
return userService.findUserById(userId);
}

通过上述的代码,返回的 User 对象会被自动添加到 Model 模型中,就相当于您手动调用了 model.addAttribute(user) 方法。

欢迎关注公众号: 小哈学Java

Spring Boot 2.0 教程 | @ModelAttribute 注解的更多相关文章

  1. Spring Boot 2.0 教程 | AOP 切面统一打印请求日志

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ...

  2. Spring Boot 2.0 教程 - 深入SpringAplication

    原文连接:https://www.codemore.top/cates/Backend/post/2018-05-20/spring-boot-SpringApplication 可以通过Spring ...

  3. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

  4. Spring Boot 2.0 教程 - 配置详解

    Spring Boot 可以通过properties文件,YAML文件,环境变量和命令行参数进行配置.属性值可以通过,@Value注解,Environment或者ConfigurationProper ...

  5. Spring Boot 2.0 教程 | 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  6. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  7. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

  8. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

  9. Spring Boot 2.0 图文教程 | 集成邮件发送功能

    文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...

随机推荐

  1. linux 端口占用

    进程id为9106,进程名称为java的进程,占用了8080端口(监听了8080端口)

  2. Pytorch

    torch.nn.utils.rnn: pack_padded_sequence() pad_packed_sequence() Notice: The padded embedding metrix ...

  3. 2019-3-26WinForm窗体间如何传值的几种方法

    窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式: 公共静态变量: 使用共有属性: 使用委托与事件: 通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点:传值是双 ...

  4. DW1000 用户手册中文版 第5章 媒体访问控制(帧过滤)

    由于已经在wode中排版无法直接复制到博客中,故本节博客发布使用了图片. PDF下载 http://bphero.com.cn/forum.php?mod=viewthread&tid=68

  5. 雕刻机制作 PCB 指南

    之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB  视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...

  6. company_base_info_staff_job

    "company_base_info_staff_job": { "engine": "python script", # datax or ...

  7. 第k小的元素

    利用快排思想,如果标志位大于k,则第k小的数字在左边,否则在右边.(程序是第k大的元素) #include <iostream> #include <vector> using ...

  8. BZOJ2649 : riddle

    题意同3495,但是内存限制收紧了,不能采用3495的前后缀优化建图的方式. 注意到“每个集合恰好选择一个点”可以放宽成“每个集合最多选择一个点”,对于最后求出的方案里,如果某个集合没选点,任选一个就 ...

  9. 在dcef3当中执行js代码并获得返回值

    1.如何在dcef3当中执行js代码 procedure TForm1.btnWriteZMClick(Sender: TObject);var  js: string;begin  js := 'd ...

  10. vijos搭建踩坑

    nodejs我用的8.x版本,可以工作. 和制作组交谈之后他们说最好榨汁机和主机不要在同一系统下. vj4/vj4/handler/base.py的第343行 从 super(Connection, ...