Primefaces 验证错误与转换器和等于工作
Primefaces Validation error with converter and equals working
我在使用 JSF 中的 PrimeFaces p:selectOneMenu 时遇到问题。我读了很多关于同一问题的问题,但没有任何帮助。
当我设置我的组件时,如果我尝试 select 我的 selectOneMenu,出现这个错误:
Validation Error: Value is not valid
很多人通过更正 Converter class 或 equals() 方法解决了这个问题,但在地雷。
转换器
@RequestScoped
public class BaremeConverter implements Converter {
@EJB
private BaremeBean baremeBean;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
try {
return baremeBean.loadById(Integer.parseInt(value));
} catch(NumberFormatException e) {
return null;
}
}
else {
return null;
}
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return String.valueOf(((Bareme) object).getId());
}
else {
return null;
}
}
}
BaremeBean 是这个 class 的 entityBean,它可以很好地加载数据。我的工作区充满了这样的转换器,所以除非我错过了这个中的某些东西,否则它应该可以在这里工作。
class Bareme
的 equals() 方法
@Override
public boolean equals(Object object) {
if (!(object instanceof Bareme)) {
return false;
}
Bareme other = (Bareme) object;
return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}
这是 Netbeans 生成的 equals() 方法,这里看起来也没什么问题。
最后给大家我用的组件的代码,和之前的一样,同样的代码对其他人有效class我得到了。
<h:outputLabel for="forfaitBareme" value="Barème" />
<p:selectOneMenu id="forfaitBareme" class="print-w100p-lab" value="#{transportFacturationBean.forfait.bareme}" converter="#{baremeConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.baremesForfait}" var="b" itemLabel="#{b.id}" itemValue="#{b}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
transportFacturationBean.baremesForfait是一个java.util.List,其中包含很少的Bareme。
您应该知道,使用我项目的另一个自定义对象,下面的代码运行良好。 Camion的实现方式与Bareme相同,转换器相似,equals()方法都是Netbeans生成的。
<h:outputLabel for="forfaitCamion" value="Camion" />
<p:selectOneMenu id="forfaitCamion" class="print-w100p-lab" value="#{transportFacturationBean.forfait.camion}" converter="#{camionConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.camions}" var="c" itemLabel="#{c.type}" itemValue="#{c}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
如有任何帮助,我们将不胜感激!提前致谢!
已解决!我能想到的 最大最小 错误!
return baremeBean.loadById(Integer.parseInt(value));
我的 loadById 方法返回的是一个列表而不是一个简单的对象....对不起大家!
我在使用 JSF 中的 PrimeFaces p:selectOneMenu 时遇到问题。我读了很多关于同一问题的问题,但没有任何帮助。
当我设置我的组件时,如果我尝试 select 我的 selectOneMenu,出现这个错误:
Validation Error: Value is not valid
很多人通过更正 Converter class 或 equals() 方法解决了这个问题,但在地雷。
转换器
@RequestScoped
public class BaremeConverter implements Converter {
@EJB
private BaremeBean baremeBean;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
try {
return baremeBean.loadById(Integer.parseInt(value));
} catch(NumberFormatException e) {
return null;
}
}
else {
return null;
}
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return String.valueOf(((Bareme) object).getId());
}
else {
return null;
}
}
}
BaremeBean 是这个 class 的 entityBean,它可以很好地加载数据。我的工作区充满了这样的转换器,所以除非我错过了这个中的某些东西,否则它应该可以在这里工作。
class Bareme
的 equals() 方法@Override
public boolean equals(Object object) {
if (!(object instanceof Bareme)) {
return false;
}
Bareme other = (Bareme) object;
return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}
这是 Netbeans 生成的 equals() 方法,这里看起来也没什么问题。
最后给大家我用的组件的代码,和之前的一样,同样的代码对其他人有效class我得到了。
<h:outputLabel for="forfaitBareme" value="Barème" />
<p:selectOneMenu id="forfaitBareme" class="print-w100p-lab" value="#{transportFacturationBean.forfait.bareme}" converter="#{baremeConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.baremesForfait}" var="b" itemLabel="#{b.id}" itemValue="#{b}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
transportFacturationBean.baremesForfait是一个java.util.List,其中包含很少的Bareme。
您应该知道,使用我项目的另一个自定义对象,下面的代码运行良好。 Camion的实现方式与Bareme相同,转换器相似,equals()方法都是Netbeans生成的。
<h:outputLabel for="forfaitCamion" value="Camion" />
<p:selectOneMenu id="forfaitCamion" class="print-w100p-lab" value="#{transportFacturationBean.forfait.camion}" converter="#{camionConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.camions}" var="c" itemLabel="#{c.type}" itemValue="#{c}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
如有任何帮助,我们将不胜感激!提前致谢!
已解决!我能想到的 最大最小 错误!
return baremeBean.loadById(Integer.parseInt(value));
我的 loadById 方法返回的是一个列表而不是一个简单的对象....对不起大家!