如何使JSF将空值设置为List

时间:2014-11-24 17:59:15

标签: jsf

为什么JSF不将空值设置回列表(或数组)?

一个例子:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component("testMB")
@Scope("view")
public class TestMB implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2463416120592801345L;

    private List<Double> doublesList;

    private List<Integer> integersList;

    private Double d = 123.456;

    private Integer i = 987;

    @PostConstruct
    public void init() {
        setDoublesList(new ArrayList<Double>());
        setIntegersList(new ArrayList<Integer>());
        for (int i = 0; i < 7; i++) {
            getDoublesList().add(i + 0.123);
            getIntegersList().add(i);
        }
    }

    public List<Double> getDoublesList() {
        return doublesList;
    }

    public void setDoublesList(List<Double> doublesList) {
        this.doublesList = doublesList;
    }

    public List<Integer> getIntegersList() {
        return integersList;
    }

    public void setIntegersList(List<Integer> integersList) {
        this.integersList = integersList;
    }

    public Double getD() {
        return d;
    }

    public void setD(Double d) {
        this.d = d;
    }

    public Integer getI() {
        return i;
    }

    public void setI(Integer i) {
        this.i = i;
    }

    public void doSomething() {
        System.out.println(getDoublesList());
        System.out.println(getIntegersList());
        System.out.println(d);
        System.out.println(i);
    }

}

观点......

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html">
    <h:head>
        <f:facet name="first">
            <meta content='text/html; charset=UTF-8' http-equiv="Content-Type" />
        </f:facet>
    </h:head>

    <h:body>
        <h:form id="form">

            <p:fieldset legend="Doubles List">
                <ui:repeat varStatus="vs1" value="#{testMB.doublesList}">
                    #{v.index}
                    <p:inputText value="#{testMB.doublesList[vs1.index]}">
                        <f:convertNumber />
                    </p:inputText>
                    <br />
                </ui:repeat>
            </p:fieldset>

            <p:fieldset legend="Integers List">
                <ui:repeat varStatus="vs2" value="#{testMB.integersList}">
                    <p:inputText value="#{testMB.integersList[vs2.index]}">
                        <f:convertNumber integerOnly="true" />
                    </p:inputText>
                    <br />
                </ui:repeat>
            </p:fieldset>


            Double: 
            <p:inputText value="#{testMB.d}">
                <f:convertNumber />
            </p:inputText>
            <br />
            <br />

            Integer:
            <p:inputText value="#{testMB.i}">
                <f:convertNumber integerOnly="true" />
            </p:inputText>

            <br /><br /><br /><br />

            <p:commandButton actionListener="#{testMB.doSomething}" update="form" />
        </h:form>
    </h:body>

</f:view>

</html>

如果列表的字段设置为空字符串,则List不会像预期的那样使用空值更新。它只是保持与以前相同的价值。

在web.xml中我有:

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

<context-param>
    <param-name>org.apache.el.parser.COERCE_TO_ZERO</param-name>
    <param-value>false</param-value>
</context-param>

但是如果我将testMB.dtestMB.i输入文本的值设置为空字符串,那么它在托管bean中正确设置为null。

那么,JSF,列表和空值的问题是什么?如何正确实现?

感谢。

2 个答案:

答案 0 :(得分:0)

尝试这样做:

<p:commandButton action="#{testMB.doSomething}" update="form" />

我认为actionListener不会更改变量的值。

答案 1 :(得分:0)

问题解决了。

JSF升级。从2.1.102.2.0

<dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.faces</artifactId>
      <version>2.2.0</version>            
</dependency>