spring boot 配置freemarker及如何使用freemarker渲染页面

 更新时间:2023年10月30日 15:04:16   作者:寂夜了无痕  
springboot中自带的页面渲染工具为thymeleaf 还有freemarker这两种模板引擎,本文重点给大家介绍spring boot 配置freemarker及如何使用freemarker渲染页面,感兴趣的朋友一起看看吧

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

1.1freemaker 优点

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

thymeleaf优点:

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

thymeleaf缺点:

模板必须符合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及使用freemarker渲染页面的文章就介绍到这了,更多相关spring boot 配置freemarker内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java结构型设计模式之组合模式Composite Pattern详解

    Java结构型设计模式之组合模式Composite Pattern详解

    组合模式,又叫部分整体模式,它创建了对象组的数据结构组合模式使得用户对单个对象和组合对象的访问具有一致性。本文将通过示例为大家详细介绍一下组合模式,需要的可以参考一下
    2022-11-11
  • 一篇文章带你了解Java基础-多态

    一篇文章带你了解Java基础-多态

    这篇文章主要介绍了Java 多态的深入理解的相关资料,子类继承父类的特征和行为,使得子类具有父类的各种属性和方法。或子类从父类继承方法,使得子类具有父类相同的行为,需要的朋友可以参考下
    2021-08-08
  • SpringBoot MongoDB 索引冲突分析及解决方法

    SpringBoot MongoDB 索引冲突分析及解决方法

    这篇文章主要介绍了SpringBoot MongoDB 索引冲突分析及解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • Spring Cloud中使用Eureka的详细过程

    Spring Cloud中使用Eureka的详细过程

    Eureka 是 Netflix 开源的一个服务发现组件,它在微服务架构中扮演着重要的角色,这篇文章主要介绍了Spring Cloud中如何使用Eureka,需要的朋友可以参考下
    2024-07-07
  • Spring Boot和Docker实现微服务部署的方法

    Spring Boot和Docker实现微服务部署的方法

    这篇文章主要介绍了Spring Boot和Docker实现微服务部署的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 详解关于mybatis-plus中Service和Mapper的分析

    详解关于mybatis-plus中Service和Mapper的分析

    这篇文章主要介绍了详解关于mybatis-plus中Service和Mapper的分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java实现角色扮演游戏的示例代码

    Java实现角色扮演游戏的示例代码

    这篇文章主要介绍了通过Java语言实现的自制的角色扮演游戏,选择两个角色,然后进行PK,可用来学习JAVA的接口,继承和多态。需要的可以参考一下
    2022-02-02
  • springboot 3.x 整合 RocketMQ 5.x的详细过程

    springboot 3.x 整合 RocketMQ 5.x的详细过程

    本文介绍了如何在SpringBoot中使用RocketMQ 5.x客户端,包括依赖配置、参数设置、生产者和消费者的消息发送与接收示例以及服务端环境搭建,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • 【面试】Spring事务面试考点吐血整理(建议珍藏)

    【面试】Spring事务面试考点吐血整理(建议珍藏)

    本文是小编给大家收藏整理的Spring事务面试考点,非常不错,值得收藏,感兴趣的朋友参考下吧
    2019-04-04
  • JAVA 十六进制与字符串的转换

    JAVA 十六进制与字符串的转换

    笔者前几日在开服过程中需要将字符串转化成为16进制的字符串,在网上找到了一些方法尝试之后,均发现存在一个问题-->字符串转为16进制后再转回来,英文正常,中文出现乱码
    2009-05-05

最新评论