JSF ValueChangeEvent:如何根据我在之前的SelectOneMenu上的选择更改一个SelectOneMenu上的选项?

时间:2010-08-12 04:27:32

标签: java jsf

好的,我已经坚持了几天(轻描淡写)。

说我有一个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();        
}}

1 个答案:

答案 0 :(得分:3)

我认为这里的问题是getFunctions(int i)接受一个参数。由于它是由下面的EL表达式调用的,所以它不应该带参数。

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

此外,ValueChangeEvent的值需要转换为String,而不是Integer。 从你的问题中不确定你是否在一个表单中包装了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();
    }
}