SpringBoot+thymeleaf项目搭建

废话不多数,直接上操作
一. 创建一个maven项目

SpringBoot+thymeleaf项目搭建

SpringBoot+thymeleaf项目搭建

SpringBoot+thymeleaf项目搭建

SpringBoot+thymeleaf项目搭建

SpringBoot+thymeleaf项目搭建

二. 配置pom.xml文件
编辑pom.xml
<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>
<groupId>com.test</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 继承springBoot默认值 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<!-- 配置具体依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
三. 新建application.properties
spring boot允许你自定义一个application.properties文件,来重写spring boot的环境变量或者定义你自己环境变量,使用此配置文件之后,spring boo启动时,spring boot会默认在src下边寻找application.properties,并且自动把配置信息读取到spring容器中,同时覆盖spring boot的默认配置。
添加thymeleaf的配置:
# ---- thymeleaf ---- start
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# ---- thymeleaf ---- end
四. 新建SapmleController.java
代码编辑:
package com.test.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class SampleController {
@RequestMapping("/")
@ResponseBody
public String home(){
return "hello world";
}
@RequestMapping("/hello")
public String helloTest(Model model){
model.addAttribute("name", "HAHA");
return "hello";
}
}

五. 创建hello.html文件
代码编辑:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello:, ' + ${name}" ></p>
</body>
</html>

六.创建MainApplication .java
package com.test.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

七.接下来执行主方法即可启动tomcat服务,进行Mapping的url的访问