gpt4 book ai didi

java - JSF 值更改事件 : how do I change the options on one SelectOneMenu based on my selection on a previous SelectOneMenu?

转载 作者:行者123 更新时间:2023-11-29 06:20:07 25 4
gpt4 key购买 nike

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

假设我有一个 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>

我该怎么做?四处阅读,我有一些关于如何做的想法,但我似乎无法做到正确。很抱歉,我只是在这里转储 bean 类,希望有人能指导我。

public class 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();
}}

最佳答案

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

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

此外,ValueChangeEvent 的值需要转换为字符串,而不是整数。从你的问题中不确定你是否已经将 selectItem 包装在一个表单中,如果没有,你也必须这样做。我能想到的最后一件事是 backingBean 的范围,它需要在回发后继续存在。

我使用 session 范围的 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();
}
}

关于java - JSF 值更改事件 : how do I change the options on one SelectOneMenu based on my selection on a previous SelectOneMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3464763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com