spring - 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 我会显示该模板。这对我来说表明这不是安全或配置问题。
有任何想法吗?
这一切都应该根据本教程进行。
解决方案
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).
推荐阅读
- python - 如何使用 Tweepy 提取用户关注的主题或主题标签
- swift - 切换控制和 iOS SKLabelNodes,如何禁用扫描某些 SKLabelNodes?
- javascript - 将 Highcharts 图表添加到 draw2d 画布
- python - 如何从 mqtt 主题和有效负载生成 dict 结构
- css - 如何将 css 样式表绑定到仅 1 个反应组件?
- linux - 在 Windows 上连接到另一台机器上的 Docker
- ruby-on-rails - Rails 简单表单不从输入传递参数
- wordpress - 具有多个术语的 Wordpress 分类过滤器
- ansible - Ansible - 部署剧本时出现 ssh 错误消息
- reactjs - 反应,插入/附加/呈现现有