需求缘起:有人在群里@我:请教群主大神一个问题,spring boot + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 。我就看看之前博客中的
【Spring Boot使用模板freemarker】好像确实没有介绍到在.ftl文件中如何获取contextPath,这就是本文解决要解决的问题。
(1)问题的提出;
(2)spring中是如何定义requestContextAttribute的;
(3)Spring Boot应该如何定义呢?
(4)有更好的解决方案嘛?
(5)总结
接下来我们一起来看下本节的内容:
(1)问题的提出;
我们有时候需要在freemarker模板文件.ftl中获取contextPath,如果没有配置一些参数的话,那么是无法进行获取的。
(2)spring中是如何定义requestContextAttribute的;
在spring 中是使用配置文件的方法进行配置指定的,如下:
<property name="requestContextAttribute" value="request"/>
配置完之后,我们就可以在我们的x.ftl文件中使用如下代码进行引入使用:
${request.contextPath}。
(3)Spring Boot应该如何定义呢?
在spring 中是使用配置文件的方式,但是我们知道spring boot基本上零配置编程的(虽然也支持配置文件的方式),那么我们应该怎么办呢?我们可以之定义一个FreemarkerViewResolver进行指定requestContextPath属性值,具体代码如下:
package com.kfit.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; /** * * @author Angel --守护天使 * @version v.0.1 * @date 2017年1月15日 */ @Configuration @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter{ @Bean public FreeMarkerViewResolver freeMarkerViewResolver() { System.out.println("MvcConfig.freeMarkerViewResolver()"); FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); resolver.setPrefix(""); resolver.setSuffix(".ftl"); resolver.setContentType("text/html; charset=UTF-8"); resolver.setRequestContextAttribute("request"); return resolver; } }
添加以上的代码之后,就可以在x.ftl文件中使用${request.contextPath}了。
(4)有更好的解决方案嘛?
以上方式虽然也能解决问题,但是总觉得绕了一个圈子,我们原本使用freemarker的时候,我们使用的是在配置文件application.properties文件进行使用的,现在又回到了代码的方式去引入了,那么要思考下我们可不可以在application.properties进行直接指定呢,答案是可以的。我们只需要在application.properties中添加如下配置:
spring.freemarker.request-context-attribute=request
那么就可以在ftl文件中进行使用${request.contextPath}了。
(5)总结
本文说了这么说,其实很简单就两个步骤:
1、在application.properties添加如下信息:
spring.freemarker.request-context-attribute=request
2、在x.ftl文件中进行使用:
${request.contextPath}
BTW:有时候我们为了代码的使用简单,request-context-attribute也会设置为ctx,那么使用的时候就是 ${ctx.contextPath}
à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
相关推荐
1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理静态资源(默认资源映射) 1.16 处理静态资源(自定义资源映射) 1.17 Spring Boot定时任务的使用 1.18 Spring Boot使用Druid和监控配置 ...
spring-boot-freemarker 整合源码
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...
spring-boot-starter-freemarker-1.5.4.RELEASE.jar
camel-freemarker-1.6.4.jar, camel-freemarker-2.8.1.jar, ...freemarker-1.4.1.jar, freemarker-2-3-18.jar, freemarker-2.1.5.jar, freemarker-2.2.5.jar, freemarker-2.2.8.jar,...freemarker.jar, freemarker2_03.jar
spring-boot-starter-freemarker-1.3.3.RELEASE.jar
spring-boot-starter-freemarker-2.0.0.RELEASE
代码生成器支持model、Mapper、Dao、Service、ServiceImpl、Controller 一次性的生成,支持同时生成多张表,此代码生成器依赖JAR包比较少,除了必用Freemarker 和JDBC之外基本上完全个人编写
1.什么是 Spring Boot? 2.Spring Boot 有哪些优点? 3.什么是 JavaConfig? 4.如何重新加载 Spring Boot 上的更改,而无需重新启动服务器? 5.Spring Boot 中的监视器是什么? 6.如何在 Spring Boot 中禁用 ...
Spring boot + Mybatis + Freemarker
基于SpringBoot+LayUI+Freemarker+Mybatis的通用后台管理系统源码.zip 完整代码,可运行 。...SpringMVC+Spring+SpringBoot+LayUI+freemarker 运行环境 IDEA【或者Eclipse】 + Tomcat6以上 + Redis + MySQL5
- chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...
Struts+Spring+Hibernate+Freemarker新闻系统
spring整合freemarker实现发送邮件, html中内嵌图片,添加附件, 解决乱码问题. spring整合freemarker实现发送邮件, html中内嵌图片,添加附件, 解决乱码问题.
01、Spring Boot之Hello World_高清.mp4 02、spring boot返回json数据_高清.mp4 03、Spring Boot完美使用FastJson解析JSON数据_高清.mp4 04、Spring Boot热部署(springloader)_高清.mp4 05、springboot + ...
使用Spring boot + FreeMarker 实现代码生成功能,完整案例可以查看 eladmin 项目,教程地址: https://www.ydyno.com/archives/1177.html
spring与freemarker整合 示例源码
Spring配置Freemarker视图解析器,配置多个视图解析器
Spring Boot 系列教程 freemarker导出word下载
com.jagregory.shiro.freemarker.ShiroTags 已经打包过的权限标签,可直接使用