我有三个组合框。第一个填充了值。当我从中选择一个值时,第二个必须更新。之后,当我从第二个组合中选择一个值时,应该填充第三个。我知道这已经传达了,但是有两个组合。超过两个它不起作用。你能告诉我我做错了什么吗?
这是视图:
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Choose the produce type:"/>
<h:selectOneMenu id="firstList"
value="#{userBean.firstItem}"
valueChangeListener="#{userBean.valueChanged}">
<f:selectItems value="#{userBean.firstList}"
var="element"
itemLabel="#{element.label}"
itemValue="#{element.value}" />
<a4j:support reRender="outputPanel"
event="onchange"
ajaxSingle="true"
bypassUpdates="false" />
</h:selectOneMenu>
<a4j:outputPanel id="outputPanel">
<h:selectOneMenu value="#{userBean.secondItem}"
valueChangeListener="#{userBean.valueChanged}">
<f:selectItems value="#{userBean.secondList}"
var="element"
itemLabel="#{element.label}"
itemValue="#{element.value}" />
<a4j:support reRender="outputPanel"
event="onchange"
ajaxSingle="true"
bypassUpdates="false" />
</h:selectOneMenu>
<h:selectOneMenu value="#{userBean.thirdItem}"
valueChangeListener="#{userBean.valueChanged}">
<f:selectItems value="#{userBean.thirdList}"
var="element"
itemLabel="#{element.label}"
itemValue="#{element.value}" />
</h:selectOneMenu>
</a4j:outputPanel>
</h:panelGrid>
</h:form>
这是 Bean(这是一个没有任何意义的示例代码):
@Name("userBean")
public class UserBean
{
@Logger private Log log;
@In StatusMessages statusMessages;
private String firstItem="";
private String secondItem="";
private String thirdItem="";
public List<SelectItem> firstList = new ArrayList<SelectItem>();
public List<SelectItem> secondList = new ArrayList<SelectItem>();
public List<SelectItem> thirdList = new ArrayList<SelectItem>();
private static final String [] FRUITS = {"Banana", "Cranberry", "Blueberry", "Orange"};
private static final String [] VEGETABLES = {"Potatoes", "Broccoli", "Garlic", "Carrot"};
private static final String [] GIRLS = {"Victoria", "Nana", "Etc.."};
public UserBean() {
SelectItem item = new SelectItem("fruits", "Fruits");
firstList.add(item);
item = new SelectItem("vegetables", "Vegetables");
firstList.add(item);
item = new SelectItem("girls", "Girls");
firstList.add(item);
for (int i = 0; i < FRUITS.length; i++) {
item = new SelectItem(FRUITS[i]);
}
}
public List<SelectItem> getFirstList() {
return firstList;
}
public List<SelectItem> getSecondList() {
return secondList;
}
public List<SelectItem> getThirdList() {
return thirdList;
}
public static String[] getFRUITS() {
return FRUITS;
}
public static String[] getVEGETABLES() {
return VEGETABLES;
}
public static String[] getGIRLS() {
return GIRLS;
}
public void valueChanged(ValueChangeEvent event){
String[] currentItems;
String id = ((HtmlSelectOneMenu)event.getSource()).getId();
if (id.equals("firstList")) {
currentItems = FRUITS;
for (int i = 0; i < currentItems.length; i++) {
SelectItem item = new SelectItem(currentItems[i]);
secondList.add(item);
}
thirdList.clear();
} else {
currentItems = GIRLS;
for (int i = 0; i < currentItems.length; i++) {
SelectItem item = new SelectItem(currentItems[i]);
thirdList.add(item);
}
}
}
public String getFirstItem() {
return firstItem;
}
public void setFirstItem(String firstItemArg) {
firstItem = firstItemArg;
}
public String getSecondItem() {
return secondItem;
}
public void setSecondItem(String secondItemArg) {
secondItem = secondItemArg;
}
public String getThirdItem() {
return thirdItem;
}
public void setThirdItem(String thirdItemArg) {
thirdItem = thirdItemArg;
}
问题是,当我从第二个组合框中选择一个值(其值是动态加载的)时,页面会重新加载(而不是执行 ajax 请求)并且我有异常:
请求处理期间的异常:由 javax.servlet.ServletException 引起,消息为:“java.util.NoSuchElementException”
第二个列表从第一个列表成功更新,但之后出现了问题。
提前致谢!干杯!