将搜索结果加载到同一页面的百里香

时间:2019-02-24 14:55:29

标签: spring-boot 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 "/";



    }
}

现在这行得通,并且确实加载了页面,而且我知道我支持的作品已经测试了搜索功能,当我只使用后端进行搜索时,我就会得到monter。

编辑

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

1 个答案:

答案 0 :(得分:0)

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