如何用Spring Boot整合模板引擎进行页面渲染?

更新于
2026-10-12 11:53:48
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计730个文字,预计阅读时间需要3分钟。

如何用Spring Boot整合模板引擎进行页面渲染?

Spring Boot中使用Thymeleaf和Freemarker:

1.在Spring Boot项目中添加Spring Boot Starter Thymeleaf依赖。

2.在配置文件中关闭Thymeleaf缓存。

java

org.springframework.boot spring-boot-starter-thymeleaf

如何用Spring Boot整合模板引擎进行页面渲染?

@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }

@Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertyPlaceholderConfigurer configurer=new PropertyPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); return configurer; }}

springboot中使用thymeleaf和freemarker

springboot使用thymeleaf: 1、加入spring-boot-starter-thymeleaf依赖 org.springframework.boot spring-boot-starter-thymeleaf 2、在配置文件中关闭thymeleaf缓存 //开发过程中建议关闭cache spring.thymeleaf.cache=false 3、新建模板html,路径放在src/main/resources/templates/下 4、新建模板controller类,使用@Controller注解,返回String或者ModelAndView值,该值指向templates目录下文件的路径 @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "thymeleaf"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "thymeleaf"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("thymeleaf"); return mv; } } ---------------------------------------------------- 在spring-boot中使用freemarker 1、加入freemarker依赖 org.springframework.boot spring-boot-starter-freemarker 1.5.6.RELEASE 2、在配置文件中关闭freemarker缓存 spring.freemarker.cache=false 3、新建模板ftl,路径放在src/main/resources/templates/下 4、新建模板controller类,使用@Controller注解,返回String或者ModelAndView值,该值指向templates目录下文件的路径 @Controller @RequestMapping("/freemarker") public class FreemarkerController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "freemarker"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "freemarker"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("freemarker"); return mv; } } FreemarkerController.java

package org.com.cay.controller.freemarker; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/freemarker") public class FreemarkerController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "freemarker"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "freemarker"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("freemarker"); return mv; } } ThymeleafController.java

package org.com.cay.controller.thymeleaf; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /* * 在thymeleaf模板文件中,标签是需要闭合的 * 在3.0之前,是需要闭合的; * 在3.0之后,不强制闭合 */ @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "thymeleaf"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "thymeleaf"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("thymeleaf"); return mv; } } application.yml

server: port: 8081 #修改默认的端口号,默认为8080 context-path: /springboot #修改默认的contextPath,默认为/ spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springboot username: root password: admin #springboot支持c3p0,dbcp,如果需要使用其他的数据库连接池,指定type # type: 其他的使用指定的dataSource, 比如druid jpa: database: MYSQL show-sql: true hibernate: ddl-auto: update naming: strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate.dialect: org.hibernate.dialect.MySQL5Dialect thymeleaf: cache: false #开发过程中建议关闭cache #encoding: UTF-8 #thymeleaf编码 #suffix: .html # thymeleaf后缀 #content-type: text/html #格式 freemarker: cache: false #charset: UTF-8 #suffix: .ftl #content-type: text/html freemarker.ftl

Insert title here

Freemarker模板文件

<#if name??> Welcome ${name} <#else> Hello world! thymeleaf.html

Insert title here

Thymeleaf模板文件

Welcome

本文共计730个文字,预计阅读时间需要3分钟。

如何用Spring Boot整合模板引擎进行页面渲染?

Spring Boot中使用Thymeleaf和Freemarker:

1.在Spring Boot项目中添加Spring Boot Starter Thymeleaf依赖。

2.在配置文件中关闭Thymeleaf缓存。

java

org.springframework.boot spring-boot-starter-thymeleaf

如何用Spring Boot整合模板引擎进行页面渲染?

@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }

@Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertyPlaceholderConfigurer configurer=new PropertyPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); return configurer; }}

springboot中使用thymeleaf和freemarker

springboot使用thymeleaf: 1、加入spring-boot-starter-thymeleaf依赖 org.springframework.boot spring-boot-starter-thymeleaf 2、在配置文件中关闭thymeleaf缓存 //开发过程中建议关闭cache spring.thymeleaf.cache=false 3、新建模板html,路径放在src/main/resources/templates/下 4、新建模板controller类,使用@Controller注解,返回String或者ModelAndView值,该值指向templates目录下文件的路径 @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "thymeleaf"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "thymeleaf"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("thymeleaf"); return mv; } } ---------------------------------------------------- 在spring-boot中使用freemarker 1、加入freemarker依赖 org.springframework.boot spring-boot-starter-freemarker 1.5.6.RELEASE 2、在配置文件中关闭freemarker缓存 spring.freemarker.cache=false 3、新建模板ftl,路径放在src/main/resources/templates/下 4、新建模板controller类,使用@Controller注解,返回String或者ModelAndView值,该值指向templates目录下文件的路径 @Controller @RequestMapping("/freemarker") public class FreemarkerController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "freemarker"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "freemarker"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("freemarker"); return mv; } } FreemarkerController.java

package org.com.cay.controller.freemarker; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/freemarker") public class FreemarkerController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "freemarker"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "freemarker"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("freemarker"); return mv; } } ThymeleafController.java

package org.com.cay.controller.thymeleaf; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /* * 在thymeleaf模板文件中,标签是需要闭合的 * 在3.0之前,是需要闭合的; * 在3.0之后,不强制闭合 */ @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello0") public String hello0(){ //ModelAndView return "thymeleaf"; } @RequestMapping("/hello1") public String hello1(Map map){ //ModelAndView map.put("name", "Cay"); return "thymeleaf"; } @RequestMapping("/hello2") public ModelAndView hello2(){ ModelAndView mv = new ModelAndView(); mv.addObject("name", "Amy"); mv.setViewName("thymeleaf"); return mv; } } application.yml

server: port: 8081 #修改默认的端口号,默认为8080 context-path: /springboot #修改默认的contextPath,默认为/ spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springboot username: root password: admin #springboot支持c3p0,dbcp,如果需要使用其他的数据库连接池,指定type # type: 其他的使用指定的dataSource, 比如druid jpa: database: MYSQL show-sql: true hibernate: ddl-auto: update naming: strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate.dialect: org.hibernate.dialect.MySQL5Dialect thymeleaf: cache: false #开发过程中建议关闭cache #encoding: UTF-8 #thymeleaf编码 #suffix: .html # thymeleaf后缀 #content-type: text/html #格式 freemarker: cache: false #charset: UTF-8 #suffix: .ftl #content-type: text/html freemarker.ftl

Insert title here

Freemarker模板文件

<#if name??> Welcome ${name} <#else> Hello world! thymeleaf.html

Insert title here

Thymeleaf模板文件

Welcome