2011-08-14 3 views
2

私はPrimefacesのPickListを使用しています。私はそれを動作させることはできません。私の問題はコンバータです。私は別の投稿の指示に従いましたが、無駄でした。ここで PrimefacesでのコンバーターPickList

は私にfacelet

<p:pickList value="#{customerBean.preferredCategories}" var="category" 
    itemLabel="#{category.description}" itemValue="#{category}" converter="#{categoryConverter}"> 
</p:pickList> 

とカテゴリをID(int型)によって構成されて

@FacesConverter(forClass=CategoryLevelView.class,value="categoryLevelConverter") 
public class CategoryConverter implements Converter { 

    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     return String.valueOf(((Category) value).getId()); 
    } 

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) { 
     Category category = new Category(); 
     category.setId(Integer.parseInt(value)); 
     return category; 
    } 
} 

ここに私のカスタムコンバータと説明(文字列) 私が欲しいですソースとターゲットの両方のリスト説明Stringを表示し、選択したカテゴリをBeanのカテゴリのリストとして設定します。どちらのリストもBeanに正しくロードされ、DualListModelはpreferredCategoriesに取り込まれます。問題はPickListがレンダリングされていないことです。何も起こらず、エラーが表示されず、ページがPickListに到着したときにページがレンダリングを停止するだけで、コンバータの間違った使用が原因と考えられます。私のこのケースを実装する正しい方法はどれですか?

ありがとうございました。この行で

+0

任意の回答の下にコメントを追加できます。 'コメントを追加する 'というリンクをクリックするだけです。 –

答えて

0

:あなたはcategoryLevelConverterに、コンバータのIDを設定しようとしているよう

@FacesConverter(forClass=CategoryLevelView.class,value="categoryLevelConverter") 

に見えます。あなたにfaceletのこの行で

converter="#{categoryConverter}" 

コンバータIDが一致していません。

2

私は

@FacesConverter(forClass=CategoryLevelView.class,value="categoryConverter") 
public class CategoryConverter implements Converter { 

forClassCategory.classへの

@FacesConverter(forClass=Category.class,value="categoryConverter") 
public class CategoryConverter implements Converter { 

変更値であるべきだと思います。

converterの値を<p:picklistに記載する必要はありません。

0

あなたの問題を解決したかどうかは分かりませんが、問題が解決していない場合は、これを試すことができます。 getAsObjectメソッドでは、新しいカテゴリオブジェクトを作成し、そのIDを設定して返します。私はあなたがここでやるべきことは、そのIDを持つデータベースからカテゴリを取得し、それを返すことだと思います。

0

私は1つの単純なコンバータを作り、それがPrimefaces選択リスト内のすべての値でうまく動作:にArrayIndexOutOfBounds例外なく

@FacesConverter("PickListConverter") 
public class PickListConverter implements Converter{ 

    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) { 
     PickList p = (PickList) component; 
     DualListModel dl = (DualListModel) p.getValue(); 
     return dl.getSource().get(Integer.valueOf(submittedValue)); 
    } 

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) { 
     PickList p = (PickList) component; 
     DualListModel dl = (DualListModel) p.getValue(); 
     return String.valueOf(dl.getSource().indexOf(value)); 
    } 
} 
+0

このコードでは、どのピックリストのエントリがソースからターゲットに移動されたかを識別する方法として、ピックリストのインデックス(0,1,2など)を使用します。これはピックリストとコンバーターを処理する最もシンプルでエレガントな方法ですが、 'getSource.indexOf(value)'は 'value'が既にTargetに入っているときには-1を返すことがあるので、' ArrayIndexOutOfBounds' 'getAsObject'の例外です。私は作者がどのようにこの機能を働かせるのだろうかと思います。 –

2

この作業を。

@FacesConverter("PickListConverter") 

public class PickListConverter implements Converter { 

public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) { 
    PickList p = (PickList) component; 
    DualListModel dl = (DualListModel) p.getValue(); 
    for (int i = 0; i < dl.getSource().size(); i++) { 
     if (dl.getSource().get(i).toString().contentEquals(submittedValue)) { 
      return dl.getSource().get(i); 
     } 
    } 
    for (int i = 0; i < dl.getTarget().size(); i++) { 
     if (dl.getTarget().get(i).toString().contentEquals(submittedValue)) { 
      return dl.getTarget().get(i); 
     } 
    } 
    return null; 
} 

public String getAsString(FacesContext facesContext, UIComponent component, Object value) { 
    PickList p = (PickList) component; 
    DualListModel dl = (DualListModel) p.getValue(); 
    // return String.valueOf(dl.getSource().indexOf(value)); 
    return value.toString(); 
} 
} 
関連する問題