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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • mybatis新手快速入门以及一些错误汇总

    mybatis新手快速入门以及一些错误汇总

    这篇文章主要给大家介绍了关于mybatis新手快速入门以及一些错误的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • java通过cglib动态生成实体bean的操作

    java通过cglib动态生成实体bean的操作

    这篇文章主要介绍了java通过cglib动态生成实体bean的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • 深入了解Java核心类库--Math类

    深入了解Java核心类库--Math类

    本文是小编最新给大家整理的关于Java中Math类常用方法的知识,通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧,
    2021-07-07
  • Window搭建部署RocketMQ步骤详解

    Window搭建部署RocketMQ步骤详解

    这篇文章主要介绍了Window搭建部署RocketMQ步骤详解,RocketMq是一个由阿里巴巴开源的消息中间件,脱胎去阿里每部使用的MetaQ,在设计上借鉴了Kafka。,需要的朋友可以参考下
    2019-06-06
  • @RequestBody 部分属性没有转化成功的处理

    @RequestBody 部分属性没有转化成功的处理

    这篇文章主要介绍了@RequestBody 部分属性没有转化成功的处理方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • @Accessors(chain = true)注解报错的解决方案

    @Accessors(chain = true)注解报错的解决方案

    这篇文章主要介绍了@Accessors(chain = true)注解报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java实现简易GUI贪吃蛇小游戏

    Java实现简易GUI贪吃蛇小游戏

    这篇文章主要为大家详细介绍了Java实现简易GUI贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 集合框架(Collections Framework)详解及代码示例

    集合框架(Collections Framework)详解及代码示例

    这篇文章主要介绍了集合框架(Collections Framework)详解及代码示例,文章涉及集合数组的区别,collection接口,iterator迭代器,list接口及其用法,LinkedHashSet集合等有关内容,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • mybatis项目CRUD步骤实例详解

    mybatis项目CRUD步骤实例详解

    这篇文章主要介绍了mybatis项目CRUD步骤,包括pom.xml引入相应的依赖,在resources目录下写配置文件,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • MyBatis-Plus中更新操作的两种实现

    MyBatis-Plus中更新操作的两种实现

    本文主要介绍了MyBatis-Plus中更新操作的两种实现,主要是通过id更新和条件更新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04

最新评论