JSF自定义转换器未调用null值

Mar*_*ark 7 java jsf jsf-2

我在输出java.math.BigDecimal时创建了自定义Converter.当BigDecimal为0.00或null我想输出破折号时.

这是我的XHTML

<p:dataTable value="#{bean.data}" var="item">
    <p:column>
        <h:outputText value="#{item.currentValue}">
            <f:converter converterId="my.bigDecimalConverter" />
        </h:outputText>
    </p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是#{item.currentValue} 是未调用Converter中nullgetAsString方法.

@FacesConverter("my.bigDecimalConverter")
public class BigDecimalConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (context == null || component == null) {
            throw new NullPointerException();
        }                

        if (value == null) {
            System.out.println("null=");
            return "--";
        }

        System.out.print("Class=" + value.getClass());

        if (value instanceof String) {
            System.out.println("Str=" + value);
            return (String) value;
        }

        if (value instanceof BigDecimal) {
            BigDecimal bd = (BigDecimal)value;
            if (bd.equals(new BigDecimal("0.00"))) {
                return "--";
            } else {
                return bd.toPlainString();
            }
        }

        return "";
    }
}
Run Code Online (Sandbox Code Playgroud)

我说它没有被调用,因为我没有错误,并且println在BigDecimal时没有语句输出null.当BigDecimal不是null按预期工作时,打印出" Class = class java.math.BigDecimal",当BigDecimal为0.00时,我会--在页面上输出.

我正在使用JSF 2.1,Mojarra 2.1.27

我还使用以下来测试我的转换器.

<h:outputText value="#{null}">
    <f:converter converterId="my.bigDecimalConverter" />
</h:outputText>
Run Code Online (Sandbox Code Playgroud)

阅读这个问题,似乎转换器应该使用一个null值. /sf/answers/1336523821/

fon*_*kap 6

您发布的链接表明转换器应该使用null,但是不要说在每种情况下都会使用空值调用转换器.

具体地说,它没有说转换器在它内部h:outputText时被调用并且值为null.

如果你挖掘Mojarra来源,你会看到:

//Line 355 -- com.sun.faces.renderkit.html_basic.HtmlBasicRenderer
//method getCurrentValue

    Object currentObj = getValue(component);
    if (currentObj != null) {
        currentValue = getFormattedValue(context, component, currentObj);
    }
Run Code Online (Sandbox Code Playgroud)

很明显,永远不会转换空值!我找不到解决方法.

然后,如果你真的需要你的值为null(你可以返回0或其他)我认为你唯一的机会是制作一个自定义渲染器.这很容易:

您编写的渲染器会覆盖重要的方法:

package my;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

import com.sun.faces.renderkit.html_basic.TextRenderer;

public class HtmlCustomRenderer extends TextRenderer {

    @Override
    public String getCurrentValue(FacesContext context, UIComponent component) {

        if (component instanceof UIInput) {
            Object submittedValue = ((UIInput) component).getSubmittedValue();
            if (submittedValue != null) {
                // value may not be a String...
                return submittedValue.toString();
            }
        }

        String currentValue = null;
        Object currentObj = getValue(component);
        //Remove the 'if' to call getFormattedValue even if null
        currentValue = getFormattedValue(context, component, currentObj);
        return currentValue;

    }

}
Run Code Online (Sandbox Code Playgroud)

然后我们在faces-config.xml中声明渲染器:

<render-kit>
    <renderer>
        <component-family>javax.faces.Output</component-family>
        <renderer-type>javax.faces.Text</renderer-type>
        <renderer-class>my.HtmlCustomRenderer</renderer-class>
    </renderer>
</render-kit>
Run Code Online (Sandbox Code Playgroud)

现在你的转换器将被调用null值!

我希望它会有所帮助!