二、添加全局数据(搭配 @ModelAttribute)
1,设置全局数据
(1)@ControllerAdvice 是一个全局数据处理组件,因此也可以在@ControllerAdvice 中配置全局数据,使用 @ModelAttribute注释进行配置。
(1)这里我们在全局配置中添加了两个方法:
message方法:返回一个 String。
userInfo方法:返回一个 map。
(2)这两个方法有一个注解@ModelAttribute,其中 value属性表示这条返回数据的 key,而方法的返回值是返回数据的 value。
package com.example.demo;
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 "欢迎访问 hangge.com";
}
@ModelAttribute(value = "info")
public Map userinfo() {<