spring boot整合thymeleaf
时间: 2023-05-02 17:04:24 浏览: 78
在Spring Boot中整合Thymeleaf可以通过添加Thymeleaf的依赖和配置Thymeleaf的模板解析器来实现。Thymeleaf是一种模板引擎,可以将HTML模板与数据进行结合,生成最终的HTML页面。在Spring Boot中使用Thymeleaf可以简化开发,提高效率。
相关问题
spring boot 整合thymeleaf项目案例
Spring Boot 是一个快速开发框架,它可以帮助开发者快速搭建一个基于 Spring 的应用程序。而 Thymeleaf 是一种现代化的服务器端 Java 模板引擎,它可以帮助开发者构建动态的 Web 应用程序。下面是一个 Spring Boot 整合 Thymeleaf 的项目案例:
1.首先,在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2.在 application.properties 文件中添加以下配置:
```
spring.thymeleaf.cache=true # 启用模板缓存
```
3.创建一个控制器类,用于处理请求并返回视图:
```
@Controller
public class MyController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("message", "Hello, World!");
return "index";
}
}
```
4.创建一个 Thymeleaf 模板文件,例如 index.html:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot + Thymeleaf</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
```
5.启动应用程序并访问 http://localhost:8080/,应该可以看到 "Hello, World!" 的消息。
实现一个Spring Boot与Thymeleaf整合的有登录界面的webapp
好的,下面是一个简单的实现步骤:
1. 创建一个基于 Spring Boot 的 web 项目,可以使用 Spring Initializr 来快速生成项目模板。
2. 添加 Thymeleaf 依赖,可以在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
3. 在 src/main/resources/templates 目录下创建 login.html 文件,作为登录页面模板。
4. 在 src/main/resources/application.properties 文件中添加以下配置:
```
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
这样 Spring Boot 就能自动找到 login.html 模板文件。
5. 在 src/main/java 目录下创建一个名为 LoginController 的控制器类,用于处理登录请求。代码如下:
```
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String doLogin(@RequestParam String username, @RequestParam String password, Model model) {
if (username.equals("admin") && password.equals("123456")) {
model.addAttribute("username", username);
return "success";
} else {
model.addAttribute("error", "用户名或密码错误");
return "login";
}
}
}
```
这里的 login() 方法用于返回登录页面模板,doLogin() 方法用于处理登录请求,判断用户名和密码是否正确,如果正确则跳转到 success.html 页面,否则返回登录页面并显示错误信息。
6. 在 src/main/resources/templates 目录下创建 success.html 文件,作为登录成功后的页面模板。
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>登录成功</title>
</head>
<body>
<h1>欢迎,[[${username}]]!</h1>
</body>
</html>
```
这里使用了 Thymeleaf 的表达式语法,将登录成功的用户名显示在页面上。
7. 在 src/main/resources/templates 目录下的 login.html 文件中添加以下代码:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form th:action="@{/login}" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div>
<button type="submit">登录</button>
</div>
<div th:if="${error}">
<span style="color: red;">[[${error}]]</span>
</div>
</form>
</body>
</html>
```
这里使用了 Thymeleaf 的表单绑定语法,将表单数据绑定到 doLogin() 方法的参数中。同时,如果登录失败,会显示错误信息。
8. 运行项目,访问 http://localhost:8080/login 即可看到登录页面。输入正确的用户名和密码,会跳转到登录成功页面。
以上就是一个简单的 Spring Boot 与 Thymeleaf 整合的 webapp,希望能对你有所帮助。