2012-08-14 79 views
1

我在使用glassfish 3.1.2的Myfaces 2.0.2上使用Primefaces 3.2。我已經在firefox 14.0.1和safari 5.1.7中進行過測試,並且在兩者中都收到相同的行爲。當我在下拉列表中選擇一個不是列表中第一個的值時,單擊命令按鈕,看起來所選項目會替換列表中的第一個元素(請參閱下面的示例輸出)。SelectOneMenu似乎改變了支持bean列表中的值

我不應該在我的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用於像我一樣當我使用號碼:selectOneMenu用於。

+1

我建議再次通過一個基本的Java教程。 Java是一種面向對象的語言,可以傳遞對象引用(內存指針),而不是通過複製值傳遞的過程語言,比如PHP或其他。 – BalusC 2012-08-14 20:17:34

+0

我用新的ContainerObject(「String 3」)替換了container.get(0),並確實解決了這個問題。爲了明確,selectedObject的setter被調用了新的值。因爲在這種情況下,引用與列表中的內容相同,它也會修改列表項。這裏的教訓是selectedObject應該總是一個副本,而不是一個引用。 – user1598246 2012-08-14 20:48:53

回答

1
selectedContainerObject = container.get(0); 

我覺得這是你要去哪裏錯了,你有效地設置selectOne對象添加到列表本身的第一個元素的值屬性,所以無論你選擇將在第一個元素結束的名單。

+0

換句話說,要解決這個問題,他需要'selectedContainerObject = new ContainerObject(「String 1」);',只要該類的equals()方法得到正確實現。 – BalusC 2012-08-14 20:16:25