首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring/MVC Thymeleaf Thymeleaf:选定不工作

Spring/MVC Thymeleaf Thymeleaf:选定不工作
EN

Stack Overflow用户
提问于 2015-08-25 14:31:56
回答 6查看 47.8K关注 0票数 6

我正在构建一个应用程序,使用Spring、Hibernate,当然还有Thymeleaf作为视图。应用程序以活动域类为中心(实际上是由俱乐部成员组织的一项活动)。该类有几个查找字段(类别区域),这些字段被建模为Hibernate映射到数据库查找表的域类。

在“编辑视图”页面中,我显示了一个下拉框,以便能够选择一个查找值,并且必须使用所选属性显示当前值。这就出问题了。虽然布尔表达式似乎是正确的,但从不生成select属性。

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

代码语言:javascript
复制
<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输出:

代码语言:javascript
复制
<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"

不是为id=2的选项元素生成的吗?

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

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

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

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

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

现在它被渲染了!见以下产出:

代码语言:javascript
复制
<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似乎忽略了它。如何解决这个问题?

EN

回答 6

Stack Overflow用户

发布于 2015-08-28 16:58:33

根据胸腺论坛(帖子)的说法,th:field不适用于th:selected

尝试像这样的东西(没有测试)

代码语言:javascript
复制
<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>
票数 16
EN

Stack Overflow用户

发布于 2017-08-07 20:22:37

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

我有同样的问题,并检查论坛,'th: selected‘和'th: field’不同时工作。查看论坛

代码语言:javascript
复制
<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>

票数 6
EN

Stack Overflow用户

发布于 2019-05-07 08:53:38

这个解决方案对我有用。

代码语言:javascript
复制
<div class="form-group">
<label>Employee's Status</label>
<select id="statusid" name="statusid" th:field="*{statusid}" class="form-control col-md-7 col-xs-12"  required="true" />
<option th:value="''" th:text="Select"></option>
<option th:each="i : ${allStatus}" th:value="${i.id}" th:text="${i.statusname}" th:selected="${i.id} == ${employee.statusid}"></option>
</select>
</div>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32206849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档