处理faces自定义转换器中属性的默认值

时间:2014-02-24 16:33:06

标签: jsf

我无法找到有关如何在自定义Converter上管理标记的任何文档。我读了许多指出这个标签的例子/ tutos,但是当我按照我在示例中看到的方式应用它时,默认值永远不会设置为我的absent-property,而是留下“deafault”null值..或任何值我在构造函数中设置它(这显然是重复的,因此我无法考虑它是一个正确的解决方案)。

如果有人对此有任何提示......

要显示,转换器-whose属性的默认值永远不会'设置' - 它的标签看起来像这样。

faces-config:

<converter>
    <converter-id>com.converter.currency</converter-id>
    <converter-class>com.somedomain.converters.CurrencyConverter</converter-lass>
    <property>
        <property-name>nbDecimal</property-name>
        <property-class>java.lang.Integer</property-class>
        <default-value>2</default-value>
    </property>
</converter>

标签库:

<tag>
    <tag-name>convertCurrency</tag-name>
    <converter>
        <converter-id>com.converter.currency</converter-id>
        <handler-class>com.somedomain.converters.CurrencyHandler</handler-class>
    </converter>
</tag>

CurrencyHandler:

private final TagAttribute nbDecimal;

public CurrencyHandler(ConverterConfig config) {
    super(config);
    this.nbDecimal= this.getAttribute("nbDecimal");
}
// ...

CurrencyConverter:

private Integer nbDecimal; // + getter / setter

public CurrencyConverter() {
    super();
}

public void init() {
    Integer nbDecimal = getNbDecimal();
    if (nbDecimal != null) {
        setMinFractionDigits(nbDecimal);
        setMaxFractionDigits(nbDecimal);
    } else {
        // this is where I could set them to default, which I don't want since it would not use the <default-value> in converter description
        // unless that is the point where my mistake comes from...
    }
}

public Object getAsObject(FacesContext context, UIComponent component, String value) {//...
}

public String getAsString(FacesContext context, UIComponent component, Object value) {//...
}
/**
 * 
 */
public Integer getNbDecimal() {
    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    if (getExpression() != null) {
        Integer nbDecimal = (Integer) getExpression().getValue(elContext);
        return nbDecimal;
    } else {
        return null;
    }
}

}

0 个答案:

没有答案