Java 如果请求映射方法返回列表,SpringBoot如何解析视图名称?
我正在学习spring boot应用程序中的视图分辨率。为了进行实验,我在spring引导应用程序中创建了一个控制器,如下所示Java 如果请求映射方法返回列表,SpringBoot如何解析视图名称?,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我正在学习spring boot应用程序中的视图分辨率。为了进行实验,我在spring引导应用程序中创建了一个控制器,如下所示 @Controller @RequestMapping("/rooms") public class RoomController { private static List<Room> roomList = new ArrayList<>(); static { for (int i = 1; i <=
@Controller
@RequestMapping("/rooms")
public class RoomController {
private static List<Room> roomList = new ArrayList<>();
static {
for (int i = 1; i <= 10; i++) {
roomList.add(new Room("Room " + i, "Name " + i, "Q"));
}
}
@GetMapping
public List<Room> getRooms(Model model) {
model.addAttribute("rooms", roomList);
// View name is rooms.html
// Returning a room list object with a different name
// Also, no other custom view resolvers are registered
return roomList;
}
}
@控制器
@请求映射(“/rooms”)
公共教室管理员{
私有静态列表roomList=newArrayList();
静止的{
对于(inti=1;iSpring非常聪明,可以从URI中找出视图的名称
有一个类负责这项工作。它知道如何构造视图名称,使用什么前缀和后缀
RequestToViewNameTranslator
,它只是将传入请求的URI转换为视图名称。[…]
默认转换只是去除URI的前导斜杠和尾随斜杠以及文件扩展名,并将结果作为视图名称返回,并根据需要添加配置的前缀和后缀
文件
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Hotel | Rooms</title>
<link th:href="@{/css/style.css}" rel="stylesheet" />
</head>
<body>
<table border="1">
<tr>
<th>Room Number</th>
<th>Name</th>
<th>Bed Info</th>
</tr>
<tr th:each="room:${rooms}">
<td th:text="${room.number}"></td>
<td th:text="${room.name}"></td>
<td th:text="${room.bedInfo}"></td>
</tr>
</table>
</body>
</html>