【问题标题】:How to get specific user by name using thymeleaf and spring boot如何使用 thymeleaf 和 spring boot 按名称获取特定用户
【发布时间】:2020-11-07 00:55:40
【问题描述】:

嘿嘿。我是春季靴子和百里香的新手。我不知道如何搜索特定用户。例如,我将输入一个 thymeleaf,每当我搜索特定的用户名时,就会从数据库中查询用户信息。并出现在屏幕上。

这是我的百里香输入框。

Input Box

listAll 的 Thymeleaf 模板

<!-- /mosque/update(id=${tempList.id}) -->
    <h2>List of all trade</h2>
    <form th:action="@{/mosque/findPeople}"  method="GET">
        <input type="text" th:value="${thePeople}" placeholder="Search people" />
        <input type="submit" value="Search" />
    </form>

    <table border="1">
        <thead>
            <tr>
                <th>NAME</th>
                <th>ADDRESS</th>
                <th>COLLECTION HEAD</th>
                <th>AMOUNT</th>
                <th>WAY OF PAYMENT</th>
                <th>DATE</th>
                <th>YEAR OF TRADE</th>
                <th>TRADE BY</th>
                <th>ACTION</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="tempList : ${listAll}">
                <td th:text="${tempList.name}"></td>
                <td th:text="${tempList.address}"></td>
                <td th:text="${tempList.trade_head}"></td>
                <td th:text="${tempList.amount}"></td>
                <td th:text="${tempList.wop}"></td>
                <td th:text="${tempList.date}"></td>
                <td th:text="${tempList.yot}"></td>
                <td th:text="${tempList.trade_by}"></td>
                <td><a th:href="@{/mosque/update(id=${tempList.id})}">Update</a>
                    &nbsp; <a th:href="@{/mosque/delete(id=${tempList.id})}"
                    onclick="if(!(confirm('Are you sure you want to update?'))) return false">Delete</a></td>
            </tr>

        </tbody>
    </table>



  <form th:action="@{/mosque/findPeople}" method="GET">
        <input type="text"  th:value="${thePeople}" placeholder="Search people" />
        <input type="submit" value="Search" />
    </form>

这是我的控制器类。

@GetMapping("/findPeople")
    public String findPeople(@RequestParam("people") String thePeople, Model theModel) {
        List<Mosque> theMosque = theMosqueService.findPerson(thePeople);
        theModel.addAttribute("thePeople", theMosque);
        return "listAll";
    }

这是我为特定用户所做的查询

// Search People
    @Query("SELECT person FROM Mosque person WHERE person.name like %?1%")
    List<Mosque> findPerson(String value);

【问题讨论】:

  • 可以给listAll添加thymleaf模板吗
  • 是的已添加。请在上面查看。
  • 你必须在People not listAll上进行迭代
  • 我想你没有得到我的问题。不要关心列出所有成员。我只想通过用户名获取特定用户。我将在输入框中输入一个名称,然后它会给我用户信息。
  • 那么您尝试这样做的错误是什么?

标签: spring spring-boot spring-mvc spring-data-jpa spring-data


【解决方案1】:

你必须循环访问 thePeople 而不是 listAll

改变这个

<tr th:each="tempList : ${listAll}">

到这里

<tr th:each="tempList : ${thePeople}">

【讨论】:

    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2017-10-18
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多