我尝试使用 validator 属性验证 p:selectOneMenu
,但在验证方法中对象的值为 null。我不明白为什么它是空的,有人可以帮助我。我正在使用 PrimeFaces 6.2。
我的xhtml是:
<p:selectOneMenu id="somFieldType"
widgetVar="somFieldType"
converter="entityConverter"
required="false"
value="#{paramDetail.article_field_type}"
validator="#{detailArticleBean2.isTypeValid}"
... >
<f:selectItem itemLabel="#{i18n['avocado.general.seleccionar']}" itemValue="0"/>
<f:selectItem itemLabel="#{i18n['avocado.general.texto']}" itemValue="1"/>
...
</p:selectOneMenu>
在我的 bean 中,我只打印值:
public void isTypeValid(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
System.out.println(value.toString());
}
控制台返回错误:
javax.el.ELException: /Ref/Art/artDetail.xhtml @165,67 validator="#{detailArticleBean2.isTypeValid}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
非常感谢您的帮助。
最佳答案
您的值为null
,因此您无法应用.toString()
。使用类似的东西:
public void isTypeValid(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
if (value == null) {
// Value is unset; don't validate
return;
}
// Value is set; do validation
...
}
另请参阅:
关于java - PrimeFaces validator 属性方法中的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66874782/