提交导致“验证错误:值无效”

发布于 2024-08-12 09:23:34 字数 695 浏览 7 评论 0原文

朋友们,我在网络开发时再次陷入困境,我向用户提供多个选择列表以选择多个选项。 JSF 页面的片段

<h:selectManyListbox id="associatedAS" value="#{maintainForm.selectedAS}">
    <s:selectItems value="#{maintainForm.associatedAS}" var="as" label="#{as.name}" />
    <rmc:asConverter />
</h:selectManyListbox>

,但问题是提交页面时我在控制台上收到错误

sourceId=maintainForm:linkedAS[severity=(错误 2),summary=(maintainForm:linkedAS:验证错误:值无效),detail=(maintainForm:linkedAS:验证错误:值无效)]

我无法弄清楚为什么会发生这种情况,我在列表中显示的项目不是字符串,所以我编写了转换器“asConverter”,用于将其他对象的值转换为字符串,反之亦然。另外,我在标签“#{maintainForm.selectedAS}”中给出的值是列表类型(selectedAS)。

任何形式的帮助表示赞赏。

谢谢。

Pals, I got stuck again while my web development, I am providing multiple selection list to user for selecting many option. A Fragment of JSF Page

<h:selectManyListbox id="associatedAS" value="#{maintainForm.selectedAS}">
    <s:selectItems value="#{maintainForm.associatedAS}" var="as" label="#{as.name}" />
    <rmc:asConverter />
</h:selectManyListbox>

but the problem is that when submit the page I am getting error on console

sourceId=maintainForm:associatedAS[severity=(ERROR 2), summary=(maintainForm:associatedAS: Validation Error: Value is not valid), detail=(maintainForm:associatedAS: Validation Error: Value is not valid)]

I am not able to figure out why this is happening, the item I am displaying in list is not string so I have written converter 'asConverter' for converting values from other objects to string and vice-versa. Also the Value I given above in tag ' #{maintainForm.selectedAS} ' is of type List (selectedAS).

Any kind of help appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-08-19 09:23:34

我和拉维·尼卡姆有同样的问题。实现了 Equals 方法和转换器,并且它与 selectOneMenu 一起工作得很好,但它与 selectManyListBox 一起给出了一个很好的 验证错误:值无效 >。
经过几个小时的搜索,我找到了解决方案。
selectManyListbox 基于javax.faces.component.UISelectMany
UISelectMany 的 javadoc 说:

Obtain the Converter using the following algorithm:
If the component has an attached Converter, use it.
If not, look for a ValueExpression for value  (if any). The ValueExpression must point to something that is:
* An array of primitives (such as int[]). Look up the registered by-class Converter for this primitive type.
* An array of objects (such as Integer[] or String[]). Look up the registered by-class Converter for the underlying element type.
* A java.util.Collection. Do not convert the values.

所以此列表中的最后一点引起了我的问题:“不要转换值”。

我已在 faces-config.xml 中指定,

<converter>
    <converter-for-class>...
    <converter-class>...
</converter>

h:selectManyListbox 中,我没有指定转换器。

我通过添加到 faces-config.xml

<converter-id>myConv</converter-id>

并将属性 converter="myConv" 添加到 h:selectManyListbox 标记解决了该问题。

I had the same problem as Ravi Nikam. Equals method and converter are implemented, and it works fine with a selectOneMenu, but it gives a nice Validation Error: Value is not valid with a selectManyListBox.
After searching for some hours, I found a solution.
The selectManyListbox is based on javax.faces.component.UISelectMany.
The javadoc of the UISelectMany says:

Obtain the Converter using the following algorithm:
If the component has an attached Converter, use it.
If not, look for a ValueExpression for value  (if any). The ValueExpression must point to something that is:
* An array of primitives (such as int[]). Look up the registered by-class Converter for this primitive type.
* An array of objects (such as Integer[] or String[]). Look up the registered by-class Converter for the underlying element type.
* A java.util.Collection. Do not convert the values.

So the last point in this list caused my problem: "Do not convert the values".

I had specified in faces-config.xml

<converter>
    <converter-for-class>...
    <converter-class>...
</converter>

In the h:selectManyListbox I had no converter specified.

I solved the problem by adding to faces-config.xml

<converter-id>myConv</converter-id>

and by adding the attribute converter="myConv" to the h:selectManyListbox tag.

丢了幸福的猪 2024-08-19 09:23:34

当您向页面发送一些值,然后发送的部分或全部原始值被修改,或者在客户端上添加一些新值时,就会出现此问题。正如您已经知道的,JSF 在服务器或客户端上保留其视图状态,取决于您如何配置它,因此它在提交时使用该状态来验证组件。在您的情况下,它发现发送到客户端的值不再相同。因此你最终会得到这个错误。

如果您使用自定义转换器,正如我在转换器页面上所描述的那样,您必须为您尝试转换的对象提供一个有效的 equals 方法。如果您尝试使用默认的 equals 方法或弄乱实现,则对象将无法正确转换,从而导致相当不直观的错误消息:“验证错误:值无效”。 - 参考:crazysquirrel.com

另一个类似的建议

This problem occurs when you send some values to the page, and then some or all of the original values sent got modified, or some new values got added on the client. As you already know, that JSF keep its view state on the server or client, depends how you configured it, so it validates the component using that state on submit. In your case it found out that the values sent to the client are no more the same. Hence you end up getting this error.

If you are using a custom converter, as I describe on the converters page, you have to provide a working equals method for the object that you are trying to convert to and from. If you attempt to use the default equals method or fluff the implementation the object won't convert correctly leading to the rather non-intuitive error message: "Validation Error: Value is not valid". - ref: crazysquirrel.com

Another similar suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文