PrimeFaces を使用して JSF でプロジェクトを作成していますが<p:SelectOneMenu>
、特に選択した値をオブジェクトから文字列に変換する際に問題があります。私が思うに、必要な Converter クラス、実装toString()
、およびequals()
メソッドを作成しましたが、かなり正しいと思います。ただし、<h:messages />
コンポーネントでは、無限に次のことがわかります。
j_idt7:j_idt92: Validation Error: Value is not valid
j_idt7:j_idt98: Validation Error: Value is not valid
1 つの大きな形式の一部である値を選択する必要があります。次に、選択した所有者 (właściciel) と会社 (firma) に基づいて、それらをデータベースに追加します。
これは私のものです<p:SelectOneMenu>
(2回-2つのメニュー):
<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 のコンバーターに関する多くのチュートリアルを読み、それを改善するために多くの方法を試しましたが、まだ機能しません。わかりません...おそらく問題は私のコードのどこかにありますか?長いので、ここには貼り付けません (もちろん、必要であれば貼ります)。