spring-boot - 如何评估百里香模板中嵌套对象的字段?
问题描述
给定两个对象:
public class MVCmodel {
UUID uuid;
NestedObject nestedObject = new NestedObject ();
}
public class NestedObject {
@Size(min = 2, max = 99)
String name = "";
}
百里香叶模板
<tr>
<td>
<input required="true" type="text" th:name="name"
th:value="${mVCmodel.getNestedObject.getName()}"
>
<span th:if="${#fields.hasErrors('name')}"
th:errors="${mVCmodel.getNestedObject().getName()}"
>Name Error
</span>
</td>
</tr>
我收到此错误:评估 SpringEL 表达式的异常:“fields.hasErrors('name')”
SpringEL 尝试评估 mVCmodel.name 并且必须失败,因为 MVCmodel 中没有字段名称。
如何评估字段 mVCmodel.nestedObject.name?
解决方案
If you're not using in a form using th:object
, you need to use #fields.hasErrors
like this:
${#fields.hasErrors('${mVCmodel.nestedObject.name}')}
If you are using th:object
, something like this should work:
${#fields.hasErrors('nestedObject.name')}
Difficult to tell from the code you posted. Do you have getters and setters for each of your fields?
推荐阅读
- ignite - 为什么 ignite 无法反序列化 GridClosureProcessor 对象?
- mysql - 尝试编写 MySQL 触发器以替换 phpmyadmin 上的 CHECK 约束时出现语法错误
- javascript - 调试 HTML 动态表时遇到问题
- python - 如何用python获取雅虎财务数据的季度和具体日期?
- tizen - 如何在 tizen 原生小部件上设置和显示图像?
- python - 如何在两个单独的列中以 YYYY 和 MM 格式遍历日期?
- json - 是否有标头管理器配置可以使用 Json 主体解决 JMeter 中的 422 错误?
- python - 无法从python中的每一行搜索和编译正则表达式代码
- c# - 如何在后台加载场景/加载级别时隐藏黑屏
- javascript - 使用 HTMLAudioElement 时 useEffect 抛出 DOMException