首页 > 解决方案 > 在同一页面 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 "/";



    }
}

现在这确实有效,我确实得到了一个加载页面,我知道我支持的作品我测试了搜索功能,当我只用后端搜索时我得到了蒙特。

编辑

我清理了我的代码,我认为我更接近解决方案,但是我没有收到任何错误或任何东西,我不知道我做错了什么

标签: spring-bootthymeleaf

解决方案


我正在返回不存在的页面,我很傻,更新了问题


推荐阅读