简体   繁体   English

如果请求映射方法返回列表,Spring Boot如何解析视图名称?

[英]How does spring boot resolve view names if the request mapping method returns a list?

I am learning view resolution in spring boot applications. 我正在Spring Boot应用程序中学习视图分辨率。 For experimenting, I have created a controller in a spring boot application as below 为了进行实验,我在Spring Boot应用程序中创建了一个控制器,如下所示

@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;
    }
}

Also, this is my rooms.html file 另外,这是我的rooms.html文件

<!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>

When I run the application, and hit https://localhost:8000/rooms , I still see the correct view rooms.html getting rendered. 当我运行该应用程序并点击https:// localhost:8000 / rooms时 ,我仍然看到呈现正确的view rooms.html的视图。

From my understanding, it should not have been able to resolve to the view "rooms.html", since I am not returning a view name string or a Model or ModelAndView objects. 根据我的理解,它不应该能够解析为“ rooms.html”视图,因为我没有返回视图名称字符串或Model或ModelAndView对象。

Is this an expected behaviour or am I missing something? 这是预期的行为还是我错过了一些东西?

Spring is smart enough to figure out the name of a view from a URI. Spring足够聪明,可以从URI中找出视图的名称。

There is a class DefaultRequestToViewNameTranslator which does the job. 有一个类DefaultRequestToViewNameTranslator可以完成这项工作。 It knows how to construct a view name, what prefix, suffix to use. 它知道如何构造视图名称,要使用的前缀和后缀。

RequestToViewNameTranslator that simply transforms the URI of the incoming request into a view name. RequestToViewNameTranslator只是将传入请求的URI转换为视图名称。 [...] [...]

The default transformation simply strips leading and trailing slashes as well as the file extension of the URI, and returns the result as the view name with the configured prefix and a suffix added as appropriate. 默认转换只是去除URI的前导和尾随斜杠以及文件扩展名,然后将结果作为视图名称返回,并带有配置的前缀和适当添加的后缀。

The documentation of DefaultRequestToViewNameTranslator DefaultRequestToViewNameTranslator的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM