spring-boot - 在同一页面 thymeleaf 中加载搜索结果
问题描述
我对 thymeleaf 了解甚少,几乎完全不使用它,我想在搜索框中输入一些内容,按搜索,然后在同一页面中获取搜索结果(如果存在)。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en"> <title></title>
</head>
<body>
<div class="container">
<h2>Search for monter</h2>
<!--/*@thymesVar id="monter" type="com.atlas.model.Monter" */-->
<form th:object="${monter}" th:action="@{/}" method="get">
<input type="text" name="search" id="search" />
<input type="submit" value="Search"/>
<div th:if="${not #lists.isEmpty(search)}">
<h2>monter List</h2>
<table class="table table-striped">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<!--/*@thymesVar id="monter" type="com.atlas.model.Monter" */-->
<tr th:each="monter: ${search}">
<!--/*@thymesVar id="monter" type="com.atlas.model.Monter" */-->
<td th:text="${monter.id}"><a href="/monter/${monter.id}">Well</a></td>
<td th:text="${monter.name}">Well</td>
</tr>
</table>
</div>
<div th:if="${#lists.isEmpty(search)}">
<h2>No entry matches that name!</h2>
</div>
</form>
</div>
</body>
</html>
接下来是我的控制器
@Controller
public class MyController {
private final MonterServiceImpl monterService;
public MyController(MonterServiceImpl monterService) {
this.monterService = monterService;
}
@RequestMapping({"", "/", "/index"})
public String getIndexPage() {
System.out.println("booh");
return "index";
}
@RequestMapping(value = "monters", method = RequestMethod.GET)
public String getMonterName(@RequestParam(value = "search",required = false) String name, Model model) {
System.out.println(monterService.findMonterByName(name));
model.addAttribute("search", monterService.findMonterByName(name));
return "/";
}
}
现在这确实有效,我确实得到了一个加载页面,我知道我支持的作品我测试了搜索功能,当我只用后端搜索时我得到了蒙特。
编辑
我清理了我的代码,我认为我更接近解决方案,但是我没有收到任何错误或任何东西,我不知道我做错了什么
解决方案
我正在返回不存在的页面,我很傻,更新了问题
推荐阅读
- javascript - 动态房间和视频的联网 A 帧问题
- javascript - 闪亮的 conditionalPanel() JS 条件不适用于 bslib
- elasticsearch - 带有 grokPattern 的子字符串
- java - 将 JPanel 设置为子 JTextArea 的大小
- serial-port - 如何更改串口的触发电平
- flutter - 如何减少颤振中的倒计时大小
- php - OpenApi Annotations 导致双斜杠
- amazon-s3 - 如何根据 s3 环境分配雪花连接 ID
- api - Visual Studio Code - 调试时如何跳过目录和本地服务器 API 文件
- javascript - 调试 JavaScript 时遇到问题,关于 Booleans 和 IsInteger()