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编写缓存工具类的示例代码

    Java编写缓存工具类的示例代码

    这篇文章主要为大家详细介绍了如何基于Java编写一个缓存工具类,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的小伙伴可以了解一下
    2023-07-07
  • spring boot(四)之thymeleaf使用详解

    spring boot(四)之thymeleaf使用详解

    Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。接下来通过本文给大家介绍spring boot(四)之thymeleaf使用详解,需要的朋友可以参考下
    2017-05-05
  • 基于maven实现私服搭建步骤图解

    基于maven实现私服搭建步骤图解

    这篇文章主要介绍了基于maven实现私服搭建步骤图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Java 正则表达式 解释说明

    Java 正则表达式 解释说明

    java正则知识小结,一些常见的正则都包括在里面,推荐收藏。
    2009-06-06
  • 关于springboot中的自定义配置项

    关于springboot中的自定义配置项

    这篇文章主要介绍了关于springboot中的自定义配置项,在项目开发的过程中,经常需要自定义系统业务方面的配置文件及配置项,Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3种方式自定义配置项,需要的朋友可以参考下
    2023-07-07
  • Java内存管理垃圾回收基础详解

    Java内存管理垃圾回收基础详解

    这篇文章主要为大家介绍了Java内存管理垃圾回收基础详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • 关于SpringCloudStream配置问题

    关于SpringCloudStream配置问题

    这篇文章主要介绍了关于SpringCloudStream配置问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 新手入门学习Spring Freemarker教程解析

    新手入门学习Spring Freemarker教程解析

    这篇文章主要介绍了新手入门学习Freemarker教程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Java如何按16进制发送和接收TCP指令

    Java如何按16进制发送和接收TCP指令

    这篇文章主要介绍了Java如何按16进制发送和接收TCP指令问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Spring中Bean对象的定义、注册和获取流程分析

    Spring中Bean对象的定义、注册和获取流程分析

    这篇文章主要介绍了Spring中Bean对象的定义、注册和获取流程分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06

最新评论