2010-08-12 3 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

+0

JSF 2를 사용하고 있습니다. –

답변

3

나는 여기에 문제가 getFunctions이 매개 변수를 (내가 INT) 있다고 생각합니다. 아래의 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에 감사하지만 작동하지 않습니다. –

+0

나는 그것을 독자적으로 시도했다. 업데이트 된 답변을 보아라. – ebaxt

+0

gee 고마워요! 태그와 Integer.valueOf가 차이를 만들었습니다. 감사하게 생각합니다. :) –

관련 문제