1.springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 简单比较下两者不同,

1.1freemaker 优点

freemarker 不足:thymeleaf由于使用了标签属性做为语法,模版页面直接用浏览器渲染,使得前端和后端可以并行开发。freemarket使用</>这样的语法,就无法直接使浏览器渲染出原本页面的样子。

thymeleaf优点:

  1. 静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。
  2. springboot官方推荐方案。

thymeleaf缺点:

  1. 模板必须符合xml规范

比较下两者

1.从写code的习惯角度可能freemarker更习惯于我们的思维。
2.不过从前后分离开发的角度看thymeleaf更合适,值的绑定都是基于html的dom元素属性的,适合前后联调。

还是回归下主题

开始编码:

1.引入pom依赖

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

2.向yml格式配置文件添加内容

spring:
freemarker:
request-context-attribute: req #req访问request
  
suffix: .html #后缀名
content-type: text/html
enabled: true
cache: false #缓存配置
template-loader-path: classpath:/templates/ #模板加载路径 按需配置
charset: UTF-8 #编码格式
settings:
number_format: '0.##' #数字格式化,无小数点

3.测试接口如图

4.index文件位置如图

index 内容

5.启动项目如图

index页面渲染成功

网上收集 propertity的freemarker 配置 (propertity格式配置文件)

# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.cache=false # Enable template caching.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.freemarker.check-template-location=true # Check that the templates location exists.
spring.freemarker.content-type=text/html # Content-Type value.
spring.freemarker.enabled=true # Enable MVC view resolution for this technology.
spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.
spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.view-names= # White list of view names that can be resolved.

另外需要使用thymeleaf 可以使用如下配置(yml 格式配置文件方式)

##视图模型
spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
    encoding: utf-8
    content-type: text/html
    check-template-location: true

spring boot 配置 freemarker的更多相关文章

  1. Spring Boot 整合 Freemarker,50 多行配置是怎么省略掉的?

    Spring Boot2 系列教程接近完工,最近进入修修补补阶段.Freemarker 整合貌似还没和大家聊过,因此今天把这个补充上. 已经完工的 Spring Boot2 教程,大家可以参考这里: ...

  2. Spring Boot 集成 FreeMarker 详解案例(十五)

    一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...

  3. Spring Boot2 系列教程(十)Spring Boot 整合 Freemarker

    今天来聊聊 Spring Boot 整合 Freemarker. Freemarker 简介 这是一个相当老牌的开源的免费的模版引擎.通过 Freemarker 模版,我们可以将数据渲染成 HTML ...

  4. Spring Boot 整合 Freemarker

    Spring Boot 整合 Freemarker 1.Freemarker 简介 2.Spring Boot 整合 Freemarker 2.1 创建工程 2.2 创建类 2.3 其他配置 原文地址 ...

  5. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  6. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  7. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  8. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  9. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

随机推荐

  1. WPF Demo2

    <Window x:Class="Demo2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/ ...

  2. Zabbix二次开发_01基础

    最近有个想法:想做一个zabbix数据的二次呈现,所以来写一下Zabbix的api的内容. 先说下zabbix api的认证基础. Zabbix API简介 Zabbix API开始扮演着越来越重要的 ...

  3. 小峰mybatis(1) 处理clob,blob等。。

    一.mybatis处理CLOB.BLOB类型数据 CLOB:大文本类型:小说啊等大文本的:对应数据库类型不一致,有long等: BLOB:二进制的,图片:电影.音乐等二进制的: 在mysql中: bl ...

  4. RAII(Resource Acquisition Is Initialization)简介

    RAII(Resource Acquisition Is Initialization),也称为“资源获取就是初始化”,是C++语言的一种管理资源.避免泄漏的惯用法.C++标准保证任何情况下,已构造的 ...

  5. 【Python编程:从入门到实践】chapter9 类

    chapter9 类 9.1 创建和使用类 9.1.1 创建Dog类 class Dog(): """一次模拟小狗的简单尝试""" def ...

  6. CSS Web安全字体组合

    常用的字体组合 font-family属性是多种字体的名称,作为一个"应变"制度,以确保浏览器/操作系统之间的最大兼容性.如果浏览器不支持的第一个字体,它尝试下一个的字体. 你想要 ...

  7. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  8. Glusterfs3.3.1DHT(hash分布)源代码分析

    https://my.oschina.net/uvwxyz/blog/182224 1.DHT简介 GlusterFS使用算法进行数据定位,集群中的任何服务器和客户端只需根据路径和文件名就可以对数据进 ...

  9. C关键字typedef及argc,argv,env参数含义

    C关键字typedef--为C中各种数据类型定义别名. 在此插一点C知识 int main(int argc,const char *argv[],const char *envp[])主函数的红色部 ...

  10. linux case ${variable} in

    脚本实现划分考试等级层次;