springboot项目中html怎么取spring boot数据
时间: 2023-12-17 17:03:24 浏览: 29
在Spring Boot项目中,可以使用Thymeleaf模板引擎来将后端的数据渲染到前端页面中。Thymeleaf是一种模板引擎,可以轻松地将后端的数据绑定到前端的HTML页面中。
首先,需要在pom.xml文件中添加Thymeleaf的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
然后,在Controller中将需要传递给前端页面的数据放入Model中:
```java
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello World!");
return "hello";
}
```
最后,在HTML页面中使用Thymeleaf的语法来获取后端数据:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
```
在以上示例中,Thymeleaf的语法`${message}`用来获取后端传递到前端的数据,将其显示在HTML页面的`h1`标签中。
希望能对您有所帮助!