项目整体结构
maven配置:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.burns.capital</groupId>
<artifactId>springboot_thymeleaf_2022_5_30</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE </version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
启动类以及控制器类写到一起了:ThyApp.java
package com.burns.capital.test.thytest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
@Controller
public class ThyApp {
public static void main(String[] args) {
SpringApplication.run(ThyApp.class, args);
}
@RequestMapping("/users")
public String listUsers(Model model) {
model.addAttribute("users", queryUsers());
return "users";
}
private List<User> queryUsers() {
List<User> users = new ArrayList<>();
users.add(new User(1, "userA"));
users.add(new User(2, "userB"));
return users;
}
}
实体类:User.java
package com.burns.capital.test.thytest;
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
模版文件:users.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<!--<link rel="stylesheet" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css}"/>-->
<link rel="stylesheet" href="/bootstrap-3.4.1-dist/css/bootstrap.min.css"/>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<!--<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table class="table table-hover table-bordered">
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}">0</td>
<td th:text="${user.name}">Angus</td>
</tr>
</tbody>
</table>
</body>
</html>
运行ThyApp.java
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.12.RELEASE)
2022-06-01 17:14:33.484 INFO 5744 --- [ restartedMain] com.burns.capital.test.thytest.ThyApp : Starting ThyApp on DESKTOP-S27LEGK with PID 5744 (E:\idea_workspace\study\springboot_thymeleaf\2022.5.30\target\classes started by 35725 in E:\idea_workspace\study\springboot_thymeleaf\2022.5.30)
2022-06-01 17:14:33.486 INFO 5744 --- [ restartedMain] com.burns.capital.test.thytest.ThyApp : No active profile set, falling back to default profiles: default
2022-06-01 17:14:33.519 INFO 5744 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-06-01 17:14:33.519 INFO 5744 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-06-01 17:14:34.136 INFO 5744 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-06-01 17:14:34.143 INFO 5744 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-06-01 17:14:34.144 INFO 5744 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2022-06-01 17:14:34.192 INFO 5744 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-06-01 17:14:34.192 INFO 5744 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 672 ms
2022-06-01 17:14:34.302 INFO 5744 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-06-01 17:14:34.406 INFO 5744 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-06-01 17:14:34.428 INFO 5744 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-06-01 17:14:34.436 INFO 5744 --- [ restartedMain] com.burns.capital.test.thytest.ThyApp : Started ThyApp in 1.191 seconds (JVM running for 1.788)
2022-06-01 17:14:46.949 INFO 5744 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-06-01 17:14:46.949 INFO 5744 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-06-01 17:14:46.952 INFO 5744 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
访问浏览器地址: