我正在尝试根据选择的单选按钮选项填充下拉框。我使用的是富脸但不能这样做。我还在同一页面中有其他输入,这些输入将存储在支持bean中。我使用的是jsf 1.2。任何人都可以帮忙。
我切换到jsf 2.0和富有面孔4
这是我的代码,但它永远不会起作用
new file.xhtml
<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:form>
<h:selectOneMenu value="#{selectsBean.currentType}" valueChangeListener="#{selectsBean.valueChanged}">
<f:selectItems value="#{selectsBean.firstList}" />
<a4j:ajax event="valueChange" render="second" execute="@this" />
</h:selectOneMenu>
<a4j:outputPanel id="second" layout="block">
<h:selectOneMenu value="#{selectsBean.currentType}" rendered="#{not empty selectsBean.currentType}">
<f:selectItems value="#{selectsBean.secondList}" />
</h:selectOneMenu>
</a4j:outputPanel>
</h:form>
</ui:composition>
package test.com;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
@ManagedBean(name = "selectsBean")
@RequestScoped
public class SelectsBean {
private static final String[] FRUITS = { "", "Banana", "Cranberry", "Blueberry", "Orange" };
private static final String[] VEGETABLES = { "", "Potatoes", "Broccoli", "Garlic", "Carrot" };
private String currentItem = "";
private String currentType = "";
private List<SelectItem> firstList = new ArrayList<SelectItem>();
private List<SelectItem> secondList = new ArrayList<SelectItem>();
public SelectsBean() {
SelectItem item = new SelectItem("", "");
firstList.add(item);
item = new SelectItem("fruits", "Fruits");
firstList.add(item);
item = new SelectItem("vegetables", "Vegetables");
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 static String[] getFRUITS() {
return FRUITS;
}
public static String[] getVEGETABLES() {
return VEGETABLES;
}
public void valueChanged(ValueChangeEvent event) {
secondList.clear();
if (null != event.getNewValue()) {
String[] currentItems;
if (((String) event.getNewValue()).equals("fruits")) {
currentItems = FRUITS;
} else {
currentItems = VEGETABLES;
}
for (int i = 0; i < currentItems.length; i++) {
SelectItem item = new SelectItem(currentItems[i]);
secondList.add(item);
}
}
}
public String getCurrentType() {
return currentType;
}
public void setCurrentType(String currentType) {
this.currentType = currentType;
}
public String getCurrentItem() {
return currentItem;
}
public void setCurrentItem(String currentItem) {
this.currentItem = currentItem;
}
}
答案 0 :(得分:0)
您使用了错误的事件。将ajax事件更改为change
,然后重试:<a4j:ajax event="change" render="second" />
。