Springbootは、モデルのグローバル変数を定義します

import com.web.core.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.HashMap;

/**
 * 定义全局变量,比如网站配置信息等内容
 * @Author: thunder
 * @Date: 2020/5/30 9:46
 */

@ControllerAdvice
public class GlobalConfig {

    @Autowired
    SiteService siteService;

    @ModelAttribute
    public void addAttributes(Model model) {

        //获取网站配置信息,
        HashMap<String,String> siteMap = siteService.getAllFromRedis();

        //将网站配置信息添加到全局变量
        model.addAttribute("site",siteMap);

    }

}

おすすめ

転載: blog.csdn.net/u011582840/article/details/106838779
おすすめ