【问题标题】:JSF Validation Error: Value is not validJSF 验证错误:值无效
【发布时间】:2017-11-10 11:50:20
【问题描述】:

收到错误“验证错误:值无效”时,我无法找到我的错误。提交表单后,我收到此错误,即使在调试时,所选组件是正确的。转换器似乎也工作正常,这就是为什么我在这里过得很艰难。到目前为止,这是我尝试过的:

  • 将 @RequestScoped 更改为 @ManagedBean 和 @ViewScoped - 由于 NullpointerException 导致 SelectOneMenue 每次崩溃之前,@RequestScoped 从未发生过这种情况,所以我不确定那里发生了什么...

  • 我仔细检查了这个 SelectedOneMenue 的转换器,它似乎工作正常,我在那里进行了很多调试,转换为字符串的对象至少对我可见,稍后相同从字符串转换回对象。

  • equals() 方法:equals 方法对我来说似乎没问题,它实际上只是检查 .id 是否相同,在转换器上检查时,它总是正确的。也许我宁愿在这里搜索错误,即使我无法弄清楚这里可能有什么问题。

这是我的代码中我想你会感兴趣的部分,我已经做了一些研究,我发现了很好的东西,虽然我无法让它工作,但这一定没有任何意义,因为我只是开始使用 JavaEE 和 JSF (+ Primefaces) 进行编程。

组件equals()方法

 @Override
 public boolean equals(Object obj) {
    if(obj == null){
        //System.out.println("Component is NULL in equals() method");
    }
    if(this.id == ((Component) obj).id) {
        return true;
    }else {
        return false;
    }
}

组件转换器类

@ManagedBean(name = "componentConverterBean")
@FacesConverter (value = "componentConverter") 
public class ComponentConverter implements Converter{

@PersistenceContext(unitName="PU")
private EntityManager em;

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
    if (modelValue == null) {
        return "";
    }
    return modelValue.toString();
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    if (submittedValue == null || submittedValue.isEmpty()) {
        System.out.println("ComponentConverter: submittedValue is null");
        return null;
    }

    try {
        System.out.println("ComponentConverter: Value to be found: " + submittedValue);
        return em.find(Component.class, Integer.parseInt(submittedValue));
    } catch (NumberFormatException e) {
        throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Component ID", e.getMessage()));
    }
}

}

包含 SelectOneMenues 的 .xhtml 文件的一部分

 <p:outputLabel for="facility" value="Facility: " />
 <p:selectOneMenu id="facility" value="#{visualization.facility}" converter="#{facilityConverterBean}" style="width:150px">
    <p:ajax process="@this" partialSubmit="true" listener="#{visualization.onFacilityChange()}" update="component" />
    <f:selectItem itemLabel="Select Facility" noSelectionOption="true" />
    <f:selectItems value="#{visualization.facilities}" var="facility" itemLabel="#{facility.name}" itemValue="#{facility}" />
 </p:selectOneMenu>
 <p:outputLabel for="component" value="Components: " />
 <p:selectOneMenu id="component" value="#{visualization.component}" converter="#{componentConverterBean}" style="width:150px">
    <f:selectItem itemLabel="Select Component" noSelectionOption="true" />
    <f:selectItems value="#{visualization.components}" var="comp" itemLabel="#{comp.name}" itemValue="#{comp}" />
 </p:selectOneMenu>

“组件:Überprüfungsfehler:Wertist ungültig。” 这是确切的错误,转换为: “组件:验证错误:值无效。”

我非常感谢这里的任何帮助。提前致谢。

【问题讨论】:

  • 所以设施相关的 selectOneMenu 工作?以及如何以及何时提交相关的“组件”,因为只有这样选定的“组件”才会发送到服务器
  • 设施一似乎工作,它也几乎相同的转换器/等于方法,设施不会给我一个错误。选择完所有内容后,用户将按下提交,然后根据选择的设施、组件和从/到日期,我想从数据库中收集其他数据。这能回答你的问题吗?
  • @MaxR。 modelValue.toString() 是否将 modelValue 的 Id 作为 String 返回?
  • 是的,我调试过了,toString() 方法每次都会返回正确的 id。

标签: jsf primefaces jsf-2


【解决方案1】:

对 value="#{visualization.component}" 使用容器对象而不是实体。 例如

class ComponentDto {
     private Component componentEntity;

     private Component getComponentEntity() {
         return this.componentEntity;
     }

     private void setComponentEntity(Component componentEntity) {
         this.componentEntity = componentEntity;
     }
}

另请参阅:https://github.com/primefaces/primefaces/issues/2756

【讨论】:

    猜你喜欢
    • 2011-09-13
    • 2013-05-20
    • 2011-06-01
    • 2017-10-31
    • 2011-02-02
    • 2021-11-22
    • 2012-11-25
    • 2015-11-03
    相关资源
    最近更新 更多