Spring/MVC Thymeleaf th:选择不工作 [英] Spring/MVC Thymeleaf th:selected not working

查看:82
本文介绍了Spring/MVC Thymeleaf th:选择不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an application using Spring, Hibernate and of course Thymeleaf for the views. The application is centered around an Activity domain class (which is literally an in- or outdoor activity organized by a club member). The class has several lookup fields (category region) which are modelled as domain classes mapped by Hibernate to database lookup tables.

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

In the Edit view page I show a dropdown box to be able to choose a lookup value, also the current value must be shown using the selected attribute. Here it goes wrong. The select attribute is never generated although the boolean expression appears to be correct.

添加 th:attr 来显示比较表达式的值,即 ${choice.id} 和 *{category.id} 那些值是正确的.我添加了 readonly 属性(不适合 select 元素,只是为了检查它是否可能是 th:selected 属性的问题),我们在输出中看到该属性是在输出中生成的!

The th:attr is added to show the values of the compared expressions, namely ${choice.id} and *{category.id} Those values are correct. I added the readonly attribute (not suitable for a select element but just to chech whether it could be a problem of the th:selected attribute) and we see in the output that this attribute IS generated in the output!

<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>

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>

所以我的问题归结为:为什么是selected="selected"

So my questing boils down to: why is selected="selected"

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

not generated for option element with id=2?

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

Of course it can be achieved by generating is with an th:attr expression but according to the documentation it should just work in this way.

我还尝试绕过特殊 th:selected 属性的使用,并使用 th:attr 为所选选项生成 selected 属性(值 'none' 是临时使用的,为了清楚起见):

Also I tried to bypass the use of the special th:selected attribute and use th:attr to generate the selected attribute for the selected option (the value 'none' is temporary used for clarity'):

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

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

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

Again, nothing is shown and no selected attribute is rendered. I tried to repeat is with a custom attribute name (the Dutch translation of the word selected, of course unknown to Thymeleaf):

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

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

现在它被渲染了!请参阅下面的输出:

And now it is rendered! See the output below:

<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>

我现在真的很迷茫,选择的属性是完全合法的,也是这里唯一的选择,但似乎 Thymeleaf 忽略了它.如何解决这个问题?

I am really lost now, the selected attribute is perfectly legal and the only choice here but it appears that Thymeleaf is ignoring it. How to solve this?

推荐答案

根据 thymeleaf 论坛 (post) th:field 不适用于 th:selected

according to the thymeleaf forum (post) th:field doesn't work with th:selected

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

try something like this (not tested)

<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>

这篇关于Spring/MVC Thymeleaf th:选择不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆