SpringBoot中的模板引擎

    技术2026-10-07  1

    模板引擎思想: 模板引擎 常见的模板引擎有JSP、Velocity、Freemarker、Thymeleaf SpringBoot推荐使用Thymeleaf; 引入thymeleaf

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

    如需切换thymeleaf版本:

    <properties> <thymeleaf.version>X.X.X.RELEASE</thymeleaf.version> <!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 --> <!-- thymeleaf2 layout1--> <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> </properties>

    强调一下:如果你自己改变一些配置,而不是用人家自动给你引入的,一定要处理好适配关系。 也就是说处理好 <thymeleaf.version>与 <thymeleaf-layout-dialect.version>的版本,如果对应不好,可想而知会怎样。

    Thymeleaf使用

    package org.springframework.boot.autoconfigure.thymeleaf; ...... @ConfigurationProperties( prefix = "spring.thymeleaf" ) public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; private boolean checkTemplate = true; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML"; }

    默认只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染; 第一步: 创建模板文件t1.html,并导入thymeleaf的名称空间。 命名空间

    <html lang="en" xmlns:th="http://www.thymeleaf.org">

    t1.html

    <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>1111111111111111111111111111111111</h1> <div th:text="${hello}"></div> <h1>1111111111111111111111111111111111</h1> </body> </html>

    Controller

    @Controller public class HelloController { @RequestMapping("/t1") public String t1(Map<String,Object> map){ map.put("hello","errorPage"); return "t1"; } }

    如图: 语法规则 th:text --> 改变当前元素里面的文本内容; th:任意html属性 --> 来替换原生属性的值

    更多配置参考官方文档:https://www.thymeleaf.org/documentation.html 中文参考书册:https://www.lanzous.com/i7dzr2j SpringMVC自动配置 Spring Boot为Spring MVC提供了自动配置,可与大多数应用程序完美配合。

    以下是SpringBoot对SpringMVC的默认配置

    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

    自动配置在Spring的默认值之上添加了以下功能:

    包含ContentNegotiatingViewResolver和BeanNameViewResolver。–> 视图解析器支持服务静态资源,包括对WebJars的支持。–> 静态资源文件夹路径自动注册Converter,GenericConverter和Formatterbeans。 --> 转换器,格式化器支持HttpMessageConverters。–> SpringMVC用来转换Http请求和响应的;User—Json;自动注册MessageCodesResolver。–> 定义错误代码生成规则静态index.html支持。–> 静态首页访问定制Favicon支持。–> 网站图标自动使用ConfigurableWebBindingInitializerbean。 如果您想保留 Spring Boot MVC 的功能,并且需要添加其他 MVC 配置(拦截器,格式化程序和视图控制器等),可以添加自己的 WebMvcConfigurer 类型的 @Configuration 类,但不能带 @EnableWebMvc 注解。 如果您想自定义 RequestMappingHandlerMapping、RequestMappingHandlerAdapter 或者 ExceptionHandlerExceptionResolver 实例,可以声明一个 WebMvcRegistrationsAdapter 实例来提供这些组件。 如果您想完全掌控 Spring MVC,可以添加自定义注解了 @EnableWebMvc 的 @Configuration 配置类。 扩展SpringMVC 以前的配置文件中的配置 <mvc:view-controller path="/hello" view-name="success"/> Copy to clipboardErrorCopied

    现在,编写一个配置类(@Configuration),是WebMvcConfigurer类型;不能标注@EnableWebMvc

    配置类 将t2请求,转变为t1

    package com.yangzhenxu.firstspringboot.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/t2").setViewName("t1"); } }

    注意请求到了这个页面但是没有数据,这是为什么??? 没数据是因为没走后台解析器,直接找的页面。 既保留了所有的自动配置,也可以通过配置类来扩展自己的配置。

    Processed: 0.013, SQL: 9