当提交JSF中的空字段时,Double接收0.00

时间:2014-12-10 18:28:55

标签: jsf-2

我需要的人在使用JSF提交空字段时收到null:

我的班级:

@RequestScoped
@ManagedBean(name = "teste")
public class Teste {

    private Double valor;

    public Double getValor() {
        return valor;
    }

    public void setValor(Double valor) {
        this.valor = valor;
    }

}

和我的编辑文字元素:

<h:inputText value = "#{teste.nota}" converter="notaConverter">
<h:inputText>

我的转换器:

@FacesConverter(forClass = Long.class, value = "notaConverter")
public class NotaConverter implements Converter {

    public NotaConverter() {}

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
        return value == null ? null : value.toString();
    }

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
        if (value == null || value.isEmpty()) return null;
        Double valor = new Double(value);

        return valor;
    }

}

但是当我调用托管bean的集合时,我收到'0.00'值。为什么?

1 个答案:

答案 0 :(得分:0)

添加到web.xml

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>