2014-01-13 45 views
0

我不知道是否應該可以添加屬性到轉換器內的組件?所以在getAsString中,我會使用uiComponent.addAttribute()。這對我來說似乎工作了50%,初始值已設置,但稍後調用轉換器時會設置一個新值,但仍會檢索初始值。將屬性動態添加到轉換器中的組件?

回答

1

你不應該這樣做,因爲它打破職責分離。你應該使用一個bean或一個scope屬性。

但也許這適合:

<h:inputText value="#{bean.someValue}" converter="#{bean}"> 
    <f:attribute name="attrName" value="#{bean.attrValue}"/> 
</h:inputText> 

@ManagedBean 
public class Bean implements Converter 
{ 
    private String someValue; 
    private String attrValue; 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) 
    { 
     attrValue = "uppercase"; 
     return someValue.toUpperCase(); 
    } 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) 
    { 
     attrValue = "lowercase"; 
     return value.toLowerCase(); 
    } 

    public String getSomeValue() 
    { 
     return someValue; 
    } 

    public void setSomeValue(String someValue) 
    { 
     this.someValue = someValue; 
    } 

    public String getAttrValue() 
    { 
     return attrValue; 
    } 

    public void setAttrValue(String attrValue) 
    { 
     this.attrValue = attrValue; 
    } 
} 
+0

OK,這是可行的。惱人的是,不得不在每個地方添加該屬性。沒有辦法讓它工作而無需一直操縱facelet嗎? – KTrum

+0

你假裝從jsf太多;) –