提交<h:selectmanylistbox>会导致“验证错误:值无效”</h:selectmanylistbox>

时间:2009-11-20 06:29:14

标签: validation jsf selectmanylistbox

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

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

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

  

sourceId = maintainForm:associatedAS [severity =(ERROR 2),summary =(maintainForm:associatedAS:Validation Error:Value无效),detail =(maintainForm:associatedAS:验证错误:值无效)]

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

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

谢谢。

2 个答案:

答案 0 :(得分:3)

我和Ravi Nikam有同样的问题。实现了等于方法和转换器,并且它与selectOneMenu一起正常工作,但它提供了一个带Validation Error: Value is not valid的好selectManyListBox。 搜索了几个小时后,我找到了解决方案。 selectManyListbox基于javax.faces.component.UISelectManyUISelectMany的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代码。

答案 1 :(得分:2)

当您向页面发送一些值,然后发送的一些或所有原始值被修改,或者在客户端上添加了一些新值时,会发生此问题。如您所知,JSF在服务器或客户端上保持其视图状态,取决于您如何配置它,因此它在提交时使用该状态验证组件。在您的情况下,它发现发送给客户端的值不再相同。因此,您最终会收到此错误。

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

另一个similar suggestion