Springboot整合thymeleaf
1、创建maven项目
具体的配置如下
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> </parent> <groupId>com.zhu</groupId> <artifactId>09-spring-boot-rhemyleaf</artifactId> <version>0.0.1-SNAPSHOT</version>
2、在pom文件中添加坐标并修改版本
<!-- 修改jdk版本 --> <properties> <java.version>1.7</java.version> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dailet.version>2.0.4</thymeleaf-layout-dailet.version> </properties> <dependencies> <!-- springboot启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
3、编写Controller类
package com.zhu.controller; @Controller public class FirstController { @RequestMapping("/show") public String showView(Model model) { model.addAttribute("msg", "first work"); return "index"; } }
4、在classpath/目录下,即src/main/resourses目录下创建文件夹templates,并在该文件下创建HTML文件index.html
!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>rhymeleaf入门</title> </head> <body> <span th:text="Hello"></span> <hr/> <span th:text="${msg}"></span> </body> </html>
5、先写启动类即可
6、整个pom配置文件(因打错单词运行出错过)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> </parent> <groupId>com.zhu</groupId> <artifactId>09-spring-boot-rhemyleaf</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 修改jdk版本 --> <properties> <java.version>1.7</java.version> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version> </properties> <dependencies> <!-- springboot启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> </project>