【问题标题】:ThymeLeaf: Cannot display view after calling endpointThymLeaf:调用端点后无法显示视图
【发布时间】:2019-12-17 08:25:12
【问题描述】:

我正在尝试调用 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 我会显示该模板。这对我来说表明这不是安全或配置问题。

有什么想法吗?

这一切都应该按照这个tutorial.工作

【问题讨论】:

  • 您可能拥有@RestController 而不仅仅是@Controller。如果要渲染模板,则需要使用@Controller@RestController 表示您所有的 @Mappings 简单地序列化返回值并将其输出为 json 或 xml(这就是为什么您看到的是字符串 deviceList 而不是模板)。

标签: spring rest web-services web thymeleaf


【解决方案1】:

您可能拥有@RestController 而不仅仅是@Controller

如果你想渲染模板,你需要使用@Controller@RestController 表示您所有的 @Mappings 只是简单地序列化返回值并将其输出为 json 或 xml(这就是您看到字符串 deviceList 而不是模板的原因)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多