SpringBoot整合thymeleaf

SpringBoot整合thymeleaf


1.thymeleaf是什么?


Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择-尽管它还有很多工作要做。

2.thymeleaf配置

1.首先创建一个SpringBoot项目。

不会创建SpringBoot项目请看       

创建SpringBoot项目

2.在pom.xml文件加入依赖

```<!--thymeleaf依赖-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency> ```

3.在resources文件夹下的templates文件夹里面创建一个html文件

4.在html页面的<html lang="en" >里面加入 xmlns:th="http://www.thymeleaf.org"

这样就会有代码提示

5.测试

创建一个TestController.java



@RequestMapping("/test")

public String test1(HttpServletRequest request) throws UnsupportedEncodingException{

request.setCharacterEncoding("UTF-8");

request.setAttribute("name","林林");

return "test"

;}

6.在配置文件里面配置前缀跟后缀



spring.mvc.view.prefix=/templates/

spring.mvc.view.suffix=.html

最后访问


thymeleaf语法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。