全局数据绑定

Exisi 2022-06-28 14:34:55
Categories: Tags:

 

示例

@ControllerAdvice

public class MyGlobalExceptionHandler{

@ModelAttribute(name = "data")

public Map<String,Object> mydata(){
        HashMap<String, Object> map =
new HashMap<>();
        map.put(
"age", 99);
        map.put(
"gender", "");
       
return map;
}

}

 

 

@RestController

public class HelloController{

    @GetMapping("/hello")

    public String hello(Model model){

//定义完成后,在任何一个Controller 的接口中,都可以获取到这里定义的数据
        Map<String, Object> map = model.asMap();

        System.out.println(map);

        return"hello controller advice";

}

}

 

来自 <https://www.cnblogs.com/lenve/p/10748453.html>