You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thymeleaf下拉框片段动态设置th:field字段报错求助

问题:Thymeleaf可复用片段中动态设置th:field属性报错

问题描述

需要创建可复用的下拉框Thymeleaf片段,供其他自定义对象调用。尝试通过传入参数动态设置<input>标签的th:field属性,调用时传入*{projectOptions.zMatrixIndex},期望将th:field绑定到该路径,但运行时报错。

报错信息

2022-09-22 15:57:39.818 ERROR 5516 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    
: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed; nested exception is 
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: 
"fragments/dropdown.html" - line 8, col 41)] with root cause

org.springframework.beans.NotReadablePropertyException: Invalid property 'field' of bean class 
[com.tribus.tribusspring.entities.objects.project.main.ProjectData]: Bean property 'field' is 
not readable or has an invalid getter method: Does the return type of the getter match the 
parameter type of the setter?

相关代码

下拉框片段(fragments/dropdown.html)

<body th:fragment="dropdownSearch(field)">
  <div class="select is-normal">
    <input list="browsers" name="browser" th:field="*{field}">
    <datalist id="browsers">
      <option th:each="i : ${#numbers.sequence(0, __${valuesDataIndexSize - 1}__)}" 
       th:value="${i}" th:text="${i} + ' - Kommentar'"></option>
    </datalist>
  </div>
</body>

片段调用示例

<div th:replace="fragments/dropdown.html :: dropdownSearch(field=*{projectOptions.zMatrixIndex})"></div>

解决方案

问题核心:当前写法中,th:field="*{field}"会被Thymeleaf解析为访问上下文Bean的field属性,而非传入的绑定表达式;且调用时传递的是表达式对应的值,不是表达式本身。

以下两种方式可解决问题:

方式1:传递字段路径字符串,动态拼接绑定表达式

  1. 修改片段定义:
<body th:fragment="dropdownSearch(fieldExpr)">
  <div class="select is-normal">
    <input list="browsers" name="browser" th:field="${#strings.format('*{0}', fieldExpr)}">
    <datalist id="browsers">
      <option th:each="i : ${#numbers.sequence(0, __${valuesDataIndexSize - 1}__)}" 
       th:value="${i}" th:text="${i} + ' - Kommentar'"></option>
    </datalist>
  </div>
</body>
  1. 调用时传递不带*的字段路径字符串:
<div th:replace="fragments/dropdown.html :: dropdownSearch(fieldExpr='projectOptions.zMatrixIndex')"></div>

方式2:使用Thymeleaf预处理解析表达式

  1. 修改片段中的th:field:
<body th:fragment="dropdownSearch(field)">
  <div class="select is-normal">
    <input list="browsers" name="browser" th:field="__${field}__">
    <datalist id="browsers">
      <option th:each="i : ${#numbers.sequence(0, __${valuesDataIndexSize - 1}__)}" 
       th:value="${i}" th:text="${i} + ' - Kommentar'"></option>
    </datalist>
  </div>
</body>
  1. 调用时传递完整的绑定表达式字符串:
<div th:replace="fragments/dropdown.html :: dropdownSearch(field='*{projectOptions.zMatrixIndex}')"></div>

注意事项

  • 预处理(__${}__)会在Thymeleaf早期处理阶段解析表达式,确保th:field拿到正确的绑定路径。
  • 需确保传递的字段路径在上下文Bean中存在对应的getter/setter方法,避免再次触发NotReadablePropertyException。

内容的提问来源于stack exchange,提问作者rocketcrafter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 13:41:12