2010-08-12 44 views
1

好吧,我一直堅持這一兩天(輕描淡寫)。JSF ValueChangeEvent:如何根據我在之前的SelectOneMenu上的選擇更改一個SelectOneMenu上的選項?

說我有一個selectOneMenu用於

<h:selectOneMenu id="selectFamily" 
     valueChangeListener="#{menuBean.changeFamily}"  
     onclick="submit()" 
     immediate="true" > 
     <f:selectItems id="familyNames" value="#{menuBean.familyNames}" /> 
     </h:selectOneMenu> 

而且我想改變基於這個以前selectOneMenu用於選擇 的選項上的另一個selectOneMenu用於選項。

<h:selectOneMenu id="selectFunction" immediate="true"> 
     <f:selectItems id="functions" value="#{menuBean.functions}" /> 
    </h:selectOneMenu> 

我如何做到這一點?讀書的時候,我有一對夫婦的如何做到這一點的想法,但我似乎無法得到它的權利。我很抱歉,我只是在這裏傾倒豆類,希望有人能指導我。

公共類MenuBean使用{

/** Creates a new instance of MenuBean */ 
public MenuBean() { 
} 

private SelectItem[] familyNames = { 
    new SelectItem((Integer) 1,"Operations"), 
    new SelectItem((Integer) 2, "Special"), 
    new SelectItem((Integer) 3, "Support")}; 

public SelectItem[] getFamilyNames() { 
    return familyNames; 
} 

private SelectItem[] functions = {new SelectItem("Select Function")}; 

private SelectItem[] operationsFunctions = { 
    new SelectItem("Air/Ocean Freight"), 
    new SelectItem("Customs"), 
    new SelectItem("Land Transport"), 
    new SelectItem("Logistics/SCM"), 
    new SelectItem("Rail Transport"), 
    new SelectItem("Special") 
}; 

private SelectItem[] specialFunctions = { 
    new SelectItem("General Management"), 
    new SelectItem("Quality & Processes") 
}; 

private SelectItem[] supportFunctions = { 
    new SelectItem("Finance/Controlling"), 
    new SelectItem("Human Resources"), 
    new SelectItem("ICT"), 
    new SelectItem("Legal Affairs"), 
    new SelectItem("Marketing/Public Relations"), 
    new SelectItem("Procurement"), 
}; 

public SelectItem[] getFunctions(int n) { 

    if (n==1) { 
     functions = operationsFunctions; 
    } else if (n==2) { 
     functions = specialFunctions; 
    } else if (n==3) { 
     functions = supportFunctions; 
    } 

    return functions; 
} 

public void setFunctions(SelectItem[] function) { 
    this.functions = function; 
} 

public void changeFamily(ValueChangeEvent event) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    int value = (Integer) event.getNewValue(); 
    setFunctions(getFunctions(value)); 

    context.renderResponse();   
}} 
+0

您是否使用JSF1.2或2? – Dejell 2010-08-12 05:33:21

+0

我使用JSF 2. – 2010-08-12 06:56:04

回答

3

我覺得這裏的問題是,getFunctions(int i)以帶一個參數。由於它由下面的EL表達式調用,所以它不應該帶一個參數。

<f:selectItems id="functions" value="#{menuBean.functions}" /> 

此外,ValueChangeEvent的值需要被轉換爲字符串,而不是整數。 從你的問題不確定你是否已經在表單中包裝了selectItem,如果沒有,你也必須這樣做。我能想到的最後一件事是backingBean的範圍,它需要在回發中生存。

我想下面的例子中與會話範圍的managedBean和JSF 1.2,它的工作:

<h:form> 
     <h:selectOneMenu id="selectFamily" 
         valueChangeListener="#{menuBean.changeFamily}" 
         immediate="true" 
         onchange="submit()"> 
      <f:selectItems id="familyNames" value="#{menuBean.familyNames}"/> 
     </h:selectOneMenu> 

     <h:selectOneMenu id="selectFunction" immediate="true"> 
      <f:selectItems id="functions" value="#{menuBean.functions}"/> 
     </h:selectOneMenu> 
    </h:form> 

public class MenuBean { 
    private SelectItem[] familyNames = { 
      new SelectItem(1, "Operations"), 
      new SelectItem(2, "Special"), 
      new SelectItem(3, "Support")}; 

    public SelectItem[] getFamilyNames() { 
     return familyNames; 
    } 

    private SelectItem[] functions = {new SelectItem("Select Function")}; 

    private SelectItem[] operationsFunctions = { 
      new SelectItem("Air/Ocean Freight"), 
      new SelectItem("Customs"), 
      new SelectItem("Land Transport"), 
      new SelectItem("Logistics/SCM"), 
      new SelectItem("Rail Transport"), 
      new SelectItem("Special") 
    }; 

    private SelectItem[] specialFunctions = { 
      new SelectItem("General Management"), 
      new SelectItem("Quality & Processes") 
    }; 

    private SelectItem[] supportFunctions = { 
      new SelectItem("Finance/Controlling"), 
      new SelectItem("Human Resources"), 
      new SelectItem("ICT"), 
      new SelectItem("Legal Affairs"), 
      new SelectItem("Marketing/Public Relations"), 
      new SelectItem("Procurement"), 
    }; 

    public SelectItem[] getFunctions() { 
     return functions; 
    } 

    private void switchSelectedFunctions(int n) { 
     if (n == 1) { 
      setFunctions(operationsFunctions); 
     } else if (n == 2) { 
      setFunctions(specialFunctions); 
     } else if (n == 3) { 
      setFunctions(supportFunctions); 
     } 
    } 

    public void setFunctions(SelectItem[] function) { 
     this.functions = function; 
    } 

    public void changeFamily(ValueChangeEvent event) { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     int value = Integer.valueOf(((String) event.getNewValue())); 
     switchSelectedFunctions(value); 
     context.renderResponse(); 
    } 
} 
+0

欣賞它的ebaxt,但它仍然無法正常工作。 – 2010-08-12 07:47:30

+0

我試了一下,看到更新的答案。 – ebaxt 2010-08-12 09:25:37

+0

gee感謝它的工作!標記和Integer.valueOf做出了不同的感謝。 :) – 2010-08-12 09:47:10

相關問題