SpringBoot+freemaker学习(一)简单配置使用

配置freemarker详解

1、SpringBoot freemarker 配置

#设置是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-request-override=false 
#设置是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-session-override=false 
#启用模板缓存。
spring.freemarker.cache=false 
#设置编码格式
spring.freemarker.charset=UTF-8 
#检查模板位置是否存在
spring.freemarker.check-template-location=true 
#内容类型值
spring.freemarker.content-type=text/html
#为这种技术启用MVC视图解决方案。
spring.freemarker.enabled=true 
#设置是否应该在与模板合并之前将所有请求属性添加到模型中。
spring.freemarker.expose-request-attributes=false
#设置是否在与模板合并之前将所有HttpSession属性添加到模型中。
spring.freemarker.expose-session-attributes=false 
#设置是否公开RequestContext供Spring宏库使用,名称为"springMacroRequestContext".
spring.freemarker.expose-spring-macro-helpers=true 
#更喜欢文件系统访问模板加载。文件系统访问允许对模板更改进行热检测。
spring.freemarker.prefer-file-system-access=true 
#前缀,用于在构建URL时查看名称
spring.freemarker.prefix= 
#所有视图的RequestContext属性的名称。
spring.freemarker.request-context-attribute= 
#众所周知的FreeMarker密钥将传递给FreeMarker的配置。
#spring.freemarker.settings.*= 
#后缀,该后缀用于在构建URL时查看名称。
spring.freemarker.suffix= 
#以逗号分隔的模板路径列表。
spring.freemarker.template-loader-path=classpath:/templates/ 
#可以解析的视图名称的白列表。
spring.freemarker.view-names= # White list of view names that can be resolved.

2、一般使用进行一下配置即可

//示例
pring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.request-context-attribute=request

3、简单示例

目录结构:

在这里插入图片描述

配置

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.html
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.request-context-attribute=request

controller

package com.haan.springbootfreemarker.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FreeMarkerController {
    @RequestMapping("/simple")
    public String simple(){
        return "simple";
    }
}

classpath:/static文件夹中的index.html

(这是放在static文件夹中的,根据配置是不使用FreeMarker模板的)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker学习</title>
</head>
<body>
    <a href="simple">简单入门</a>
</body>
</html>

classpath:/templates中的simple.html

(根据配置,在classpath:/templates文件夹下的以.html结尾的文件将使用FreeMarker模板)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>simple</title>
</head>
<body>
    <h2>Freemarker的简单测试页面</h2>
</body>
</html>
posted @ 2022-10-30 23:22  寒小韩  阅读(1241)  评论(0编辑  收藏  举报