使用「Spring Tool Suite」在「Spring boot + Thymeleaf」下创建一个简单的Web界面
我使用「Spring Tool Suite」在「Spring boot + Thymeleaf」上创建了一个简单的Web界面,我将记录下当时的步骤。
环境
-
- Pleiades All in One Eclipse(リリース 2021-06)
- Spring Tool Suite (STS) 4.11.0
请参阅上一篇文章 “如何在Eclipse中安装Spring Tool Suite插件”,了解关于 “Pleiades All in One Eclipse” 和 “Spring Tool Suite (STS)” 的安装方法。
我试过的步骤 (Wǒ de
1. 创建Eclipse项目





2. 创建「欢迎访问!Spring Boot + Thymeleaf」页面。
当访问 http://localhost:8080/ 时,将创建一个显示 “欢迎来到Spring Boot + Thymeleaf!” 页面的示例。

package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("/")
public String index(Model model) {
model.addAttribute("message", "ようこそ!Spring boot + Thymeleafへ!");
return "index";
}
}

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>トップページ</title>
</head>
<body>
<p th:text="${message}"></p>
</body>
</html>
3. 运行Spring Boot应用程序


. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v2.5.2)[0;39m
[2m2021-07-06 23:45:47.817[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.demo.DemoApplication [0;39m [2m:[0;39m Starting DemoApplication using Java 1.8.0_202 on yasushi-PC with PID 7592 (C:\DK\pleiades-2021-06-java\pleiades\workspace\demo\target\classes started by yasushi in C:\DK\pleiades-2021-06-java\pleiades\workspace\demo)
[2m2021-07-06 23:45:47.820[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.demo.DemoApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-07-06 23:45:48.471[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2021-07-06 23:45:48.479[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-07-06 23:45:48.479[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.48]
[2m2021-07-06 23:45:48.543[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-07-06 23:45:48.543[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 682 ms
[2m2021-07-06 23:45:48.758[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.w.s.WelcomePageHandlerMapping [0;39m [2m:[0;39m Adding welcome page template: index
[2m2021-07-06 23:45:48.852[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2021-07-06 23:45:48.859[0;39m [32m INFO[0;39m [35m7592[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.demo.DemoApplication [0;39m [2m:[0;39m Started DemoApplication in 1.337 seconds (JVM running for 1.833)



以上
下次尝试使用页面导航和传递参数来创建Spring boot + Thymeleaf的Web界面,并进行页面跳转。