【问题标题】:How to set an atribute in html with thymeleaf如何使用 thymeleaf 在 html 中设置属性
【发布时间】:2017-05-09 18:37:17
【问题描述】:

我有一个 spring 应用程序,它可以让我获得一个具有模型属性的城市。在 html 代码中,我有很多 .所以,我想知道如果选项值与模型中的属性值相等,我如何设置要选择的选项。

<select class="form-control" id="county" required="required">
    <option>Roma</option>
    <option>Milano</option>
    <option>Paris</option>
    <option>London</option>
    <option selected="selected">Bucharest</option>
</selected

Spring 应用中的模型属性:

model.addAttribute("county", user.getCounty()); //which is "Bucharest" in this case

【问题讨论】:

    标签: java html spring thymeleaf


    【解决方案1】:

    添加 th:value 以选择组件

    <select class="form-control" id="county" required="required" th:value="*{county}">
    

    如果您有一个对象绑定到表单,请改用th:field="*{county}"

    【讨论】:

      猜你喜欢
      • 2016-06-13
      • 1970-01-01
      • 2020-09-27
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多