java Thymeleaf 下拉选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40472243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:14:35  来源:igfitidea点击:

Thymeleaf dropdown selection

javahtmlspring-bootthymeleaf

提问by Alminas Plunge

I am using Thymeleaf for one of my java projects. Now, what I am trying to achieve, is when i choose something else from the drop down, and not a default value which is "ALL", and then press submit button [POST], to get the details about the chosen item. I would like to keep that chose item displaying in the drop down, after the submission. And what it does now, it returns back to default value. If I add to the provided code below attribute selected, then it selects all items from the list, which is not acceptable as well, as it end up always displaying the last item of the list.

我正在将 Thymeleaf 用于我的一个 Java 项目。现在,我想要实现的是,当我从下拉列表中选择其他内容而不是默认值“ALL”,然后按提交按钮 [POST],以获取有关所选项目的详细信息。在提交后,我想保留该选择的项目显示在下拉列表中。它现在所做的,它返回到默认值。如果我添加到下面提供的代码中选择属性,那么它会从列表中选择所有项目,这也是不可接受的,因为它最终总是显示列表的最后一个项目。

<div class="controls">
    <select class="form-control" name="example" id="example">
        <option value="0">ALL</option>
        <option th:each="user : ${allUsers}" th:value="${user.id}" th:text="${user.name}">
        </option>
    </select>
</div>

my controller to get the list, I kept it as simple as possible:

我的控制器获取列表,我保持它尽可能简单:

@ModelAttribute("allUsers")
public List<User> allUsers() {
    List<User> userList= repo.findAll();
    return userList;
}

also I would like to note, that my object is:

我还要指出,我的目标是:

<form class="form-search" action="#" method="post"
      th:action="@{/combinedsearch}"
      th:object="${combinedreport}">

So the idea to use something like this doesn't work

所以使用这样的想法是行不通的

<select th:field="*{user}">, as my object doesn't actually have User as a field.

<select th:field="*{user}">,因为我的对象实际上没有 User 作为字段。

Any ideas? Help?

有任何想法吗?帮助?

回答by Alminas Plunge

Basically what I did is in a combinedReport controller, I have extracted that user_ID, which I receive after HTML form submission, then I used it to whatever I need to do in back end, and then I send that user_id back to front end, simply by adding it to a model as an attribute:

基本上我所做的是在combinedReport控制器中,我提取了在HTML表单提交后收到的user_ID,然后将其用于后端需要做的任何事情,然后将该user_id发送回前端,简单地通过将其作为属性添加到模型中:

model.addAttribute("lastselected", userId);

model.addAttribute("lastselected", userId);

Then I have created custom logic method in a User's object:

然后我在用户的对象中创建了自定义逻辑方法:

public boolean isSelected(Integer userId){
        if (userId != null) {
            return userId.equals(id);
        }
        return false;
    } 

and then I have modified my code in my thymeleaf template to:

然后我将 thymeleaf 模板中的代码修改为:

<select class="form-control" name="example" id="example">
     <option value="0">ALL</option>
     <option th:each="user : ${allUsers}"
             th:value="${user.id}"
             th:selected="${user.isSelected(lastselected)}"
             th:text="${user.name}">
     </option>
</select>

and baaaam! it works like a charm !

和 baaam!它就像一个魅力!