SpringBoot 配置 freemarker

74 阅读1分钟

1 yml 文件:

spring:
  freemarker:
    suffix: .ftl
    templateEncoding: UTF-8
    templateLoaderPath: classpath:/templates/
    charset: UTF-8
    content-type: text/html;charset=UTF-8
    default_encoding: UTF-8
    cache: false
    expose-session-attributes: true
    # 设置上下文路径
    request-context-attribute: request 

2 maven依赖



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

3 获取上下文路径

页面获取方式: r e q u e s t . c o n t e x t P a t h &lt; l i n k r e l = &quot; s t y l e s h e e t &quot; h r e f = &quot; {request.contextPath} &lt;link rel=&quot;stylesheet&quot; href=&quot; request.contextPath<linkrel="stylesheet"href="{request.contextPath}/css.css"/>

以上无法获取到,试试下面

<#assign ctx=request.contextPath />

这样后来发现也无法获取到 值为null
然后百度 发现这样子可以:

<#assign ctx=springMacroRequestContext.contextPath />

然后
${ctx} 即可