我正在与PrimeFaces JSF一个项目,我有问题<p:SelectOneMenu>
,特别是与选定的值的从对象到字符串转换。我已经写了所需的Converter类,实现了toString()
和equals()
方法,我认为是非常正确的。然而,在<h:messages />
组件它无休止地给我:
转换器在JSF(PrimeFaces)与selectOneMenu用于:验证错误
j_idt7:j_idt92: Validation Error: Value is not valid
j_idt7:j_idt98: Validation Error: Value is not valid
我必须选择它们是一个大表格的一部分值。然后,基于选定的所有者(właściciel)和公司(firma),我将它们添加到数据库中。
这是我<p:SelectOneMenu>
(两次 - 两个菜单):
<p:selectOneMenu value="#{wniosek.selectedWl}" var="w">
<f:selectItem itemLabel="Wybierz" itemValue=""/>
<f:selectItems value="#{wniosek.listaWl}" var="wlasciciel"
itemLabel="#{wlasciciel.nazwisko}" itemValue="#{wlasciciel}" />
<p:column>
#{w.nazwisko}
</p:column>
<f:converter converterId="WlascicielConverter" />
</p:selectOneMenu>
<h:outputText value="Nazwa firmy: "/>
<p:selectOneMenu value="#{wniosek.selectedFi}" var="f">
<f:selectItem itemLabel="Wybierz" itemValue=""/>
<f:selectItems value="#{wniosek.listaFi}" var="firma"
itemLabel="#{firma.nazwa}" itemValue="#{firma}" />
<f:converter converterId="FirmaConverter" />
<p:column>
#{f.nazwa}
</p:column>
</p:selectOneMenu>
这是我Converter类业主的<p:SelectOneMenu>
(类似我已经与该公司一个做):
public class WlascicielConverter implements Converter {
int i = 0;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
try {
if (arg2 == null || arg2.isEmpty()) {
return null;
}
String owner = arg2;
return WlascicielBean.findAnOwner(owner);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
if(arg2 == null) return null;
Wlasciciel owner = new Wlasciciel();
if(arg2 instanceof Wlasciciel) {
owner = (Wlasciciel)arg2;
System.out.println(owner.getNazwisko());
String surname = owner.getNazwisko();
return (surname != null) ? String.valueOf(surname) : null;
} else throw new ConverterException("Something wrong!" + arg2.hashCode() + arg2.toString());
}
}
一个equals()
方法:
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof Wlasciciel)) return false;
Wlasciciel wl = (Wlasciciel)obj;
if (this.id_w != wl.getId_w()) return false;
if (!this.nazwisko.equals(wl.getNazwisko())) return false;
if (!this.imie.equals(wl.getImie())) return false;
if (!this.ulica.equals(wl.getUlica())) return false;
if (this.nr != wl.getNr()) return false;
if (this.lokal != wl.getLokal()) return false;
if (this.id_n != wl.getId_n()) return false;
return true;
}
你能不能给我说说解决这个问题的一些提示吗?我已经阅读了很多关于JSF中的转换器的教程,尝试了很多方法来改进它,但它仍然不起作用。我不知道......也许问题在我的代码中的其他地方?由于它的长度,我不会在这里粘贴它(当然,如果有必要,我会这样做)。
另一个可能的原因是''列表在表单提交过程中发生了变化。另请参阅http:// stackoverflow。com/questions/9069379/validation-error-value-is-not-valid/9069660#9069660解密波兰代码的声望无论如何:) –
BalusC
我还在为这个问题而战。我试图检查转换器为什么不正常工作,并发现'getAsString'方法中的'arg2'对象是一个'String',而不是'Wlascicel'对象(但它应该是,不是吗?)。所以我收到一个'ClassCastException' - 它不能将'String'转换为'Wlasciciel'。我编辑了之前的清单并插入了最新的转换器。我真的不知道,什么是错的。 –
找到了解决方案? –