Spring/MVC Thymeleaf th:选择不工作

kla*_*sch 5 spring-mvc thymeleaf

我正在使用Spring,Hibernate以及Thymeleaf构建一个应用程序来获取视图.该应用程序以Activity域类为中心(实际上是由俱乐部成员组织的室内或室外活动).该类有几个查找字段(类别区域),它们被建模为由Hibernate映射到数据库查找表的域类.

在"编辑"视图页面中,我显示了一个下拉框,可以选择查找值,还必须使用所选属性显示当前值.这里出错了.尽管布尔表达式看起来是正确的,但从不生成select属性.

添加th:attr以显示比较表达式的值,即$ {choice.id}和*{category.id}这些值是正确的.我添加了readonly属性(不适合选择元素,但只是为了讨论是否可能是th:selected属性的问题),我们在输出中看到此属性是在输出中生成的!

<label>Categorie</label>
<select th:field="*{category}">
  <option th:each="choice : ${allCategories}" 
          th:value="${choice.id}" 
         th:attr="choiceid=${choice.id}, categoryid=*{category.id}, showselected=(${choice.id} == *{category.id})" 
         th:selected="(${choice.id} == *{category.id})" 
         th:readonly="(${choice.id} == *{category.id})" 
         th:text="${choice.description}"></option>
</select>
Run Code Online (Sandbox Code Playgroud)

HTML输出:

<label>Categorie</label>
<select id="category" name="category">
  <option choiceid="1" categoryid="2" showselected="false" value="1">Actief en sportief</option>
  <option choiceid="2" categoryid="2" showselected="true" readonly="readonly" value="2">Uitgaan en nachtleven</option>
  <option choiceid="3" categoryid="2" showselected="false" value="3">Kunst en cultuur</option>
  <option choiceid="4" categoryid="2" showselected="false" value="4">Eten en drinken</option>
  <option choiceid="5" categoryid="2" showselected="false" value="5">Ontspanning en gezelligheid</option>
</select>
Run Code Online (Sandbox Code Playgroud)

所以我的任务归结为:为什么被 选中="被选中"

没有为id = 2的选项元素生成?

当然,它可以通过使用th:attr表达式生成来实现,但根据文档,它应该以这种方式工作.

此外,我试图绕过使用特殊的th:selected属性并使用th:attr为所选选项生成所选属性(值'none'是临时用于清晰'):

th:attr ="selected =($ {choice.id} ==*{category.id})?'selected':'none'"

同样,没有显示任何内容,也没有呈现选定的属性.我尝试重复的是使用自定义属性名称(所选单词的荷兰语翻译,当然不为Thymeleaf所知):

th:attr ="geselecteerd =($ {choice.id} ==*{category.id})?'selected':'none'"

现在它被渲染了!见下面的输出:

<select id="category" name="category">
  <option geselecteerd="none" value="1">Actief en sportief</option>
  <option geselecteerd="selected" value="2">Uitgaan en nachtleven</option>
  <option geselecteerd="none" value="3">Kunst en cultuur</option>
  <option geselecteerd="none" value="4">Eten en drinken</option>
  <option geselecteerd="none" value="5">Ontspanning en gezelligheid</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我现在真的迷失了,所选择的属性是完全合法的,这是唯一的选择,但似乎Thymeleaf忽略了它.怎么解决这个?

qua*_*zde 8

根据百里香论坛(帖子)th:field不起作用th:selected

尝试这样的事情(未经测试)

<select name="category">
    <option th:each="choice : ${allCategories}" 
     th:value="${choice.id}" 
     th:attr="choiceid=${choice.id}, categoryid=*{category.id}, showselected=(${choice.id} == *{category.id})" 
     th:selected="(${choice.id} == *{category.id})" 
     th:readonly="(${choice.id} == *{category.id})" 
     th:text="${choice.description}"></option>
</select>
Run Code Online (Sandbox Code Playgroud)

  • 这是如何解决问题的? (2认同)

小智 5

' th: field ' 自动生成 'id' 和 'name' 属性,所以如果你想使用 'th: selected',你应该删除 'th: field' 并手动设置它们工作。

我遇到了同样的问题并检查了论坛,“ th: selected ”和“ th: field ”不能同时工作。 检查论坛

<div class="row">
<div class="input-field col s12">
    <select id="doc" name="doc" th:with="doc=*{doc}">
                    <option value="" th:disabled="disabled" selected="true"
                       th:text="${status==true}? 'Seleccione tipo de documento' : ${doc}">Seleccione
                       tipo de documento</option>
                    <option th:each="tipoDoc : ${tipoDocs}" th:text="${tipoDoc}"
                       th:value="${tipoDoc}" />
              </select>
    <label>Documento</label>
</div>
Run Code Online (Sandbox Code Playgroud)