首页 > 解决方案 > ThymLeaf:调用端点后无法显示视图

问题描述

我正在尝试调用 REST 端点,然后显示一个 ThymeLeaf 模板:

端点:

@GetMapping("/devices")
public String getDeviceDetailU(Model model) {
  List<FinalDevice> devices = deviceService.getAll();
  model.addAttribute("devices", devices);
  return "deviceList";  
}

对于端点,我尝试返回 /deviceList、/deviceList.html、deviceList.html。

每当我导航到端点时,我都会得到返回的字符串。

这是模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <body>
Hello World!
    </body>
</html>

虽然我理解,此时它不会显示列表,我只想转发到模板。

如果我转到 localhost:8080/deviceType 我会显示该模板。这对我来说表明这不是安全或配置问题。

有任何想法吗?

这一切都应该根据本教程进行。

标签: springrestweb-serviceswebthymeleaf

解决方案


You probably have @RestController instead of just a @Controller.

If you want templates to be rendered you need to use @Controller. @RestController means that all your @Mappings simply serialize the return value and output it as json or xml (which is why you are seeing the string deviceList instead of the template).


推荐阅读