繁体   English   中英

SelectOneMenu似乎改变了后备bean列表中的值

[英]SelectOneMenu seems to mutate the values in the backing bean list

我正在使用glassfish 3.1.2在Myfaces 2.0.2上使用Primefaces 3.2。 我已经在Firefox 14.0.1和Safari 5.1.7中进行了测试,并且在两者中都收到了相同的行为。 当我在下拉列表中选择的值不是列表中的第一个元素,然后单击命令按钮时,似乎所选项目将替换列表中的第一个元素(请参见下面的示例输出)。

我不应该在bean中使用直接列表来支持selectOneMenu吗? 还是我做错了其他事?

我创建了一个简单的测试用例,再现了以下问题:

package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "testBean")
@ViewScoped
public class TestBean implements Serializable
{
    private List<ContainerObject> container;
    private ContainerObject selectedContainerObject;

    public TestBean()
    {
        container= new ArrayList<ContainerObject>();
    }

    @PostConstruct
    public void initialize()
    {
        container.add(new ContainerObject("String 1"));
        container.add(new ContainerObject("String 2"));
        container.add(new ContainerObject("String 3"));
        container.add(new ContainerObject("String 4"));
        container.add(new ContainerObject("String 5"));
        selectedContainerObject = container.get(0);
    }

    public List<ContainerObject> getContainer()
    {
        return container;
    }

    public ContainerObject getSelectedContainerObject()
    {
        return selectedContainerObject;
    }

    public void listContainer(ActionEvent event)
    {
        for(ContainerObject c : container)
            System.out.println(c.getValue());
    }

    public class ContainerObject
    {
        String value;

        public ContainerObject(String value)
        {
            this.value = value;
        }

        public String getValue()
        {
            return value;
        }

        public void setValue(String value)
        {
            this.value = value;
        }
    }   
}

以下是我的xhtml。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:html
    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">

    <h:head>
    </h:head>
    <h:body>
        <h:form id="container" enctype="multipart/form-data">
            <p:selectOneMenu id="c" value="#{testBean.selectedContainerObject.value}">
                <f:selectItems value="#{testBean.container}" var="container" 
                    itemLabel="#{container.value}" itemValue="#{container.value}" />
            </p:selectOneMenu>
            <p:commandButton value="submit list" 
                actionListener="#{testBean.listContainer}"/>
        </h:form>
    </h:body>
</h:html>

以下是选择“字符串3”并按提交按钮后打印到控制台的内容。

INFO: String 3
INFO: String 2
INFO: String 3
INFO: String 4
INFO: String 5

但我希望看到

INFO: String 1
INFO: String 2
INFO: String 3
INFO: String 4
INFO: String 5

当我使用h:selectOneMenu时,也得到与使用p:selectOneMenu时相同的行为。

selectedContainerObject = container.get(0);

我认为这是您要出问题的地方,实际上是将selectOne对象的value属性设置为列表本身的第1个元素,因此选择的内容最终都将出现在列表的第一个元素中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM