SpringBoot学习笔记6--整合freemarker/thymeleaf模板引擎

1.资源配置

在pom.xml文件中引入依赖

<!-- 引入 freemarker 模板依赖 -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
  </dependency>
  <!-- 引入 thymeleaf 模板依赖 -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
在application.properties文件中添加如下配置:

#加载静态资源文件
spring.mvc.static-path-pattern=/static/**

############################################################
#
# freemarker相关配置
#
############################################################
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#关闭缓存,即时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
#设置文件后缀
spring.freemarker.suffix=.ftl

############################################################
#
# thymeleaf相关配置
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

2.在src/main/resources下的templates文件夹可以分别新建 freemarker 和thymeleaf 两个文件夹,然后分别新建index.ftl和index.html如下图所示:

3.分别创建controller--FreemarkerController&&ThymeleafController

注意:这里请不要使用@RestController,不然返回前端的只是一串字符串。@RestController注解表示返回的内容都是HTTP Content不会被模版引擎处理,它默认为该类中的所有的方法都添加了@ResponseBody

4.进入浏览器访问http://localhost:8088/SpringBoot/ftl/index即可看到如下结果

进入浏览器访问http://localhost:8088/SpringBoot/thy/index即可看到如下结果



猜你喜欢

转载自blog.csdn.net/qq_20788055/article/details/80460535