“FieldError for a collection is not binding in JSP page.”

huangapple go评论73阅读模式
英文:

FieldError for a collection is not binding in JSP page

问题

我正在使用 Java 验证对 `@ElementCollection` 进行验证。

```java
private SortedSet< @Pattern(regexp = "[0-9]{2,3}", message = "只允许输入数字")
           String> dummyList = new TreeSet<>();

在 JSP 页面中,Spring 绑定标签无法绑定错误信息。

 <spring:bind path="obj.dummyList">
               <div>
                    <!--代码--->
                    <form:errors path="dummyList" class="control-label form-error"/>
                </div>
</spring:bind>

但是,当在错误标签中将路径替换为 * 时,错误信息会显示在页面上。

在检查 BindingResult 时,发现字段名称被设置为 dummyList[],而不是 dummyList

请提供一种使绑定在标签中生效的方法。


<details>
<summary>英文:</summary>

I am validating the `@ElementCollection` with java validation.

private SortedSet< @Pattern(regexp = "[0-9]{2,3}", message = "Only numbers allowed")
String> dummyList = new TreeSet<>();


In JSP page spring bind tag is not able to bind the error.

<spring:bind path="obj.dummyList">
<div>
<!--code--->
<form:errors path="dummyList" class="control-label form-error"/>
</div>
</spring:bind>


But when the path is replaced with `*` in the errors tag the error is shown on page

Upon inspecting the BindingResult, found that field name is set to `dummyList[]` instead of `dummyList`.

Please suggest a way to make binding work in the tag.


</details>


# 答案1
**得分**: 0

将星号`*`添加到字段名字后面生效。

```html
<form:errors path="dummyList*" class="control-label form-error"/>
英文:

Appending * to the field name worked.

&lt;form:errors path=&quot;dummyList*&quot; class=&quot;control-label form-error&quot;/&gt;

huangapple
  • 本文由 发表于 2020年9月20日 23:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63980745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定