简体   繁体   English

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

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

I am using Primefaces 3.2 on Myfaces 2.0.2 using glassfish 3.1.2. 我正在使用glassfish 3.1.2在Myfaces 2.0.2上使用Primefaces 3.2。 I have tested in both firefox 14.0.1 and safari 5.1.7 and receive the same behavior in both. 我已经在Firefox 14.0.1和Safari 5.1.7中进行了测试,并且在两者中都收到了相同的行为。 When I select a value that is not the first in the list in the dropdown, and click the command button, it appears that the selected item replaces the first element in the list (see sample output below). 当我在下拉列表中选择的值不是列表中的第一个元素,然后单击命令按钮时,似乎所选项目将替换列表中的第一个元素(请参见下面的示例输出)。

Am I not supposed to use a straight list in my bean to back a selectOneMenu? 我不应该在bean中使用直接列表来支持selectOneMenu吗? Or am I doing something else wrong? 还是我做错了其他事?

I have created a simple test case that reproduces the issue below: 我创建了一个简单的测试用例,再现了以下问题:

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;
        }
    }   
}

The following is my xhtml. 以下是我的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>

The following is what gets printed out to the console as a result of choosing "String 3" and pressing the submit button. 以下是选择“字符串3”并按提交按钮后打印到控制台的内容。

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

But I expected to see 但我希望看到

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

I also get the same behavior when I use h:selectOneMenu as I do when I use p:selectOneMenu. 当我使用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