• springboot thymeleaf 后端 全局变量


    springboot thymeleaf  后端 全局变量

    pom.xml 中:

    <!-- google java lib -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>17.0</version>
    </dependency>

    任意Spring能扫到的有类中:

    import org.springframework.core.env.Environment;
    
    @Resource
        private Environment env;
    
    
    @Resource
        private void configureThymeleafStaticVars(ThymeleafViewResolver viewResolver) {
            if(viewResolver != null) {
                Map<String, Object> vars = Maps.newHashMap();
                vars.put("oauthServerIp", "http://127.0.0.1:9101");
                vars.put("oauth2Serverurl", env.getProperty("security.oauth2.serverurl"));
                viewResolver.setStaticVariables(vars);
            }
        }

    application.properties

    #自定义非官方配置
    security.oauth2.serverurl=http://127.0.0.1:9191

    html文件或任务layout的文件中:

    <li><a th:href="@{''+${oauth2Serverurl}+'/cms'}" class="news">首页</a></li>

    js变量放到header.html中:

    <script th:inline="javascript">
            const oauth2Serverurl= '[(${oauth2Serverurl})]';
            const oauthServerIp= '[(${oauthServerIp})]';
        </script>

    参考:

    Spring Boot 中读取配置属性的几种方式:https://www.jianshu.com/p/2aadd10f655f

    Maps.newHashMap()是个啥 https://blog.csdn.net/Ting1king/article/details/108994207

    Spring Boot 中读取配置属性的几种方式

  • 相关阅读:
    表单
    表格
    浮动与定位实例中的问题
    浮动与定位
    BFC的布局规则以及触发条件
    发布为知笔记到博客
    处理为知笔记样式与博客园样式冲突
    文字处理
    块元素并行的问题
    标签嵌套规则
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/16295104.html
Copyright © 2020-2023  润新知