当前位置: 首页 > news >正文

spring boot下thymeleaf全局静态变量配置

我们使用spring boot配合着thymeleaf开发时,有时需要使用全局变量,如:url的路劲,页面某部分的文字等等情况。我不知道是不是spring将thymeleaf的静态变量配置方法漏掉了,以下是org.springframework.boot.autoconfigure.thymeleaf.AbstractThymeleafViewResolverConfiguration的部分代码。

@Bean
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
public ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    configureTemplateEngine(resolver, this.templateEngine);
    resolver.setCharacterEncoding(this.properties.getEncoding().name());
    resolver.setContentType(appendCharset(this.properties.getContentType(),
            resolver.getCharacterEncoding()));
    resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
    resolver.setViewNames(this.properties.getViewNames());
    // This resolver acts as a fallback resolver (e.g. like a
    // InternalResourceViewResolver) so it needs to have low precedence
    resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
    resolver.setCache(this.properties.isCache());
    return resolver;
}

看到@Bean这个注解应该很多人知道怎么处理了,spring将它配置好的ThymeleafViewResolver加入了context中,免去了一些麻烦,我接下来给出简单的例子作为参考,这段代码只要放到spring能扫描到的地方就可以了。

@Resource
private void configureThymeleafStaticVars(ThymeleafViewResolver viewResolver) {
    if(viewResolver != null) {
        Map<String, Object> vars = new HashMap<>();
        vars.put("ctx", "/app/");
        vars.put("var1", "var1");
        vars.put("var2", "var2");
        viewResolver.setStaticVariables(vars);
    }
}

html文件写下这样的代码:

<th:block th:inline="text">
    ctx: [(${ctx})]
    ctx: [(${var1})]
    ctx: [(${var2})]
</th:block>

渲染出来的结果:

clipboard.png

相关文章:

  • ajax提交数据处理总结
  • django.http.response中HttpResponse 子类
  • SpringMVC三种异常处理方式
  • .NET 常见的偏门问题
  • CentOS6.4 安装LVS-RRD监控LVS
  • java8集合--LinkedList纯源码
  • 函数:递归是神马 - 零基础入门学习Python022
  • OSS控制台新增函数计算处理事件入口
  • 抽象类和接口
  • ABP理论学习之SignalR集成
  • Eclipse Java EE IDE 创建 Dynamic Web project问题
  • 集成第三方接口的技巧总结
  • 开始VS 2012 中LightSwitch系列的第2部分:感受关爱——定义数据关系
  • 在左上角倾斜四十五度显示的TextView
  • java IO类简单介绍
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 08.Android之View事件问题
  • 10个确保微服务与容器安全的最佳实践
  • HTTP 简介
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • IDEA常用插件整理
  • JavaScript 基本功--面试宝典
  • Java教程_软件开发基础
  • Linux中的硬链接与软链接
  • Magento 1.x 中文订单打印乱码
  • php ci框架整合银盛支付
  • Ruby 2.x 源代码分析:扩展 概述
  • SpiderData 2019年2月16日 DApp数据排行榜
  • spring + angular 实现导出excel
  • Spring Cloud中负载均衡器概览
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • Terraform入门 - 3. 变更基础设施
  • Unix命令
  • Web Storage相关
  • 对象引论
  • 工作中总结前端开发流程--vue项目
  • 猴子数据域名防封接口降低小说被封的风险
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 入门到放弃node系列之Hello Word篇
  • 算法之不定期更新(一)(2018-04-12)
  • 微信支付JSAPI,实测!终极方案
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • 我们雇佣了一只大猴子...
  • ​第20课 在Android Native开发中加入新的C++类
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (十三)Maven插件解析运行机制
  • (数据结构)顺序表的定义
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)setTimeout 和 setInterval 的区别
  • .Net IE10 _doPostBack 未定义
  • .net web项目 调用webService
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter