2017-01-04 18 views
0
<p:selectOneMenu id="tag1" value="#{attachmentManagement.tag1}" converter="#{stringFilterConverter}"> 
    <f:selectItems value="#{attachmentManagement.tag1List}" var="tag1" itemLabel="#{not empty tag1.value ? tag1.value : '无'}"/> 
</p:selectOneMenu> 

<p:commandButton value="button" actionListener="#{attachmentManagement.someFuction()}"/> 

を動作しない、私はボタンをクリックすると、この例外がthrowedた:コンバータは、JSFに

javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert [email protected] of type class java.lang.String to class com.bolan.fzsystem.appserver.view.model.StringFilter 
.... 
Caused by: java.lang.IllegalArgumentException: Cannot convert [email protected] of type class java.lang.String to class com.bolan.fzsystem.appserver.view.model.StringFilter 
.... 

stringFilterConverterとsomeFunctionはまったく呼び出されません。 私はコンバータを設定しているので、なぜjsfはそれを使用せず、toString()を使って値を更新します。

My Converterの実装はCDI Beanです。私は解決策を見つけた。コンバータのスコープをデフォルトからアプリケーションスコープに変更します。そして今、コンバータは動作します。しかし、なぜ?

これは私のコンバータの実装です:あなたのコードで

@Named 
@ApplicationScoped //change scope from default to this 
public class StringFilterConverter implements Converter, Serializable { 
    private static final long serialVersionUID = -6691961589607931061L; 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     if (value == null || value.isEmpty()) { 
      return ValueFilter.UNDEFINE; 
     } 
     return JSON.parseObject(value, new TypeReference<String>() {}); 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     ValueFilter<String> filter = (ValueFilter<String>) value; 
     return JSON.toJSONString(filter); 
    } 
} 
+0

してください(もちろん、私はあなたのコンバータの実装クラスは、注釈@FacesConverter("stringFilterConverter")を持っていると仮定)この非常に興味深い記事を読む[CDI Bean](http://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named) –

答えて

0

は、しかし、私はあなたが、コンバータのように適切な使用をEL式の構文を使用して、コンバータの名前をラップするべきではないと思いますがconverter="#{stringFilterConverter}"として、コンバータを定義スニペット次のようになります。

<p:selectOneMenu id="tag1" value="#{attachmentManagement.tag1} converter="stringFilterConverter"/> 

+0

私のコンバータの実装はCDI Beanです。私は解決策を見つけた。コンバータのスコープをデフォルトからアプリケーションスコープに変更します。そして今、コンバータは動作します。しかし、なぜ? – vipcxj

+0

コンバータの実装であなたの答えを更新できますか? –

関連する問題