我有三个组合框。第一个是填充值。当我从中选择一个值时,必须更新第二个值。之后,当我从第二个组合中选择一个值时,应该填充第三个组合。我知道这已经传达了,但有两个组合。有两个以上它不起作用。你能告诉我我做得不好吗?
以下是观点:
<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”
第二个列表已从第一个列表成功更新,但之后出现问题。
提前致谢! 干杯!
答案 0 :(得分:1)
确保您的bean具有比Request更广的范围,例如Conversation范围。看起来你正在使用JSF 1.2,在这种情况下,RichFaces有一个<a4j:keepAlive>
标记组件,用于将托管bean的范围从Request提升到View(在后台使用会话变量)。这个选项可能就是您所需要的。有两种方法可以实现它
使用标记组件。这将使bean只为此页面具有“查看范围”。
<a4j:keepAlive bean="userBean" />
<h:form>
<h:panelGrid columns="2">
<!-- the rest of your code... -->
使用标记注释。这将使bean在每个页面上都使用“View Scope”。
@Name("userBean")
@KeepAlive
public class UserBean {
//your managed bean code...
}
注意:要使用此标记(作为组件或注释),bean必须具有Request scope。