zoukankan      html  css  js  c++  java
  • SpringBoot @ControllerAdvice的使用详解2(添加全局数据 @ModelAttribute)

    二、添加全局数据(搭配 @ModelAttribute)

    1,设置全局数据

    (1)@ControllerAdvice 是一个全局数据处理组件,因此也可以在 @ControllerAdvice 中配置全局数据,使用 @ModelAttribute 注释进行配置。
    (1)这里我们在全局配置中添加了两个方法:

    • message 方法:返回一个 String。
    • userInfo 方法:返回一个 map。

    (2)这两个方法有一个注解 @ModelAttribute,其中 value 属性表示这条返回数据的 key,而方法的返回值是返回数据的 value。

    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import java.util.HashMap;
    import java.util.Map;
     
    @ControllerAdvice
    public class GlobalConfig {
     
        @ModelAttribute(value = "msg")
        public String message() {
            return "hello index";
        }
     
        @ModelAttribute(value = "info")
        public Map<String, String> userinfo() {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", "小明");
            map.put("age", "25");
            return map;
        }
    }

    (2)当然 @ModelAttribute 也可以不写 value 参数,直接在方法中对全局 Model 设置 key 和 value。下面代码的效果同上面是一样的:

    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import java.util.HashMap;
     
    @ControllerAdvice
    public class GlobalConfig {
     
        @ModelAttribute
        public void addAttributes(Model model) {
            model.addAttribute("msg", "hello index");
     
            HashMap<String, String> map = new HashMap<>();
            map.put("name", "小明");
            map.put("age", "25");
            model.addAttribute("info", map);
        }
    }

    2,获取全局数据

    (1)在任意请求的 Controller 中,方法参数中通过 @ModelAttribute 可以获取指定的全局数据,样例代码如下:
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.GetMapping;
    import java.util.Map;
     
    @RestController
    public class HelloController {
        @GetMapping("/hello1")
        public String hello1(@ModelAttribute("msg") String msg,
                          @ModelAttribute("info") Map<String, String> info) {
            String result = "msg:" + msg + "<br>" + "info:" + info;
            return result;
        }
    }

     

    (2)我们也可以在请求的 Controller 方法参数中的 Model 获取所有的全局数据,下面代码的结果同上面的一样:
     @GetMapping("/hello2")
        public String hello2(Model model) {
            Map<String, Object> map = model.asMap();
            String msg = map.get("msg").toString();
            Map<String, String> info = (Map<String, String>)map.get("info");
            String result = "msg:" + msg + "<br>" + "info:" + info;
            return result;
        }
    早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。
  • 相关阅读:
    软件开发与定制报价
    C# HttpHelper 1.0正式版发布
    C#仿QQ皮肤-TextBox 控件实现
    HTML5学习笔记第一节(智能提示和视频音频标签)
    C#多线程|匿名委托传参数|测试您的网站能承受的压力|附源代码升级版
    JavascriptHelp
    我的个人博客论坛版建立啦!
    Win7 + VirtualBox安装Mac OS X雪豹操作系统图文详解
    Sql2005性能工具(SQL Server Profiler和数据库引擎优化顾问)使用方法详解
    Webcast 系列课程 NET最全,最权威的学习资源
  • 原文地址:https://www.cnblogs.com/shanheyongmu/p/15688866.html
Copyright © 2011-2022 走看看