I used to do Build a WEB system with Spring + Doma + H2DB, but I tried to create a page using Thymeleaf as a template engine. think.
Use the previous project as is.
First, add the following to pom.xml.
pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
First, add the HTML file.
This time add test.html.
The place to add is src / main / resources / templates.
test.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>Test</title>
	</head>
	<body>
		<table>
			<thead>
				<tr>
					<th>ID</th>
					<th>Name</th>
				</tr>
			</thead>
			<tbody>
				<tr th:each="entity : ${entities}" th:object="${entity}">
					<td th:text="*{id}">id</td>
					<td th:text="*{name}">name</td>
				</tr>
			</tbody>
		</table>
	</body>
</html>
Finally, add a method for displaying HTML to Controller.
Add the following method to TestController.java.
TestController.java
@RequestMapping(value = "test_th", method = RequestMethod.GET)
public String getEntitiesHtml(Model model) {
	List<TestEntity> list = service.getAllEntities();
	model.addAttribute("entities", list);
	return "test";
}
Change the annotation attached to the class from @ RestController to @ Controller.
TestController.java
//@RestController
@Controller
public class TestController {
	...
}
When I accessed [http: // localhost: 8080 / test_th](http: // localhost: 8080 / test_th), the data was safely displayed in table format.

Recommended Posts