backbean的所有代码
- package?org.hblz.common.web.jsf; ??
- ??
- import?javax.faces.component.html.HtmlSelectOneListbox; ??
- import?javax.faces.event.ValueChangeEvent; ??
- import?javax.faces.model.SelectItem; ??
- ??
- import?org.apache.log4j.Logger; ??
- ??
- public?class?UserBean?{ ??
- ??
- ????@SuppressWarnings("unused") ??
- ????private?static?final?Logger?log?=?Logger.getLogger(UserBean.class); ??
- ??
- ????private?String?message; ??
- ??
- ????private?SelectItem[]?countyList?=?new?SelectItem[0]; ??
- ??
- ????private?Long?countyId; ??
- ??
- ????private?HtmlSelectOneListbox?countySelectList; ??
- ??
- ????private?SelectItem[]?townList?=?new?SelectItem[0]; ??
- ??
- ????private?Long?townId; ??
- ??
- ????private?HtmlSelectOneListbox?townSelectList; ??
- ??
- ????public?void?processCountySelectListChange(ValueChangeEvent?event)?{ ??
- ??
- ????????this.message?=?"hello?county!"; ??
- ??
- ????????Long?newValue?=?(Long)?event.getNewValue(); ??
- ??
- ????????townList?=?null; ??
- ??
- ????????townList?=?new?SelectItem[10]; ??
- ????????for?(int?i?=?0;?i?<?10;?i++)?{ ??
- ????????????SelectItem?st?=?new?SelectItem(new?Long(i),?"Town?No."?+?i ??
- ????????????????????+?"?of?County?No."?+?newValue.toString()); ??
- ????????????townList[i]?=?st; ??
- ????????} ??
- ????} ??
- ??
- ????public?void?processTownSelectListChange(ValueChangeEvent?event)?{ ??
- ????} ??
- ??
- ????public?Long?getCountyId()?{ ??
- ????????return?countyId; ??
- ????} ??
- ??
- ????public?void?setCountyId(Long?countyId)?{ ??
- ????????this.countyId?=?countyId; ??
- ????} ??
- ??
- ????public?SelectItem[]?getCountyList()?{ ??
- ????????countyList?=?new?SelectItem[30]; ??
- ????????for?(int?i?=?0;?i?<?30;?i++)?{ ??
- ????????????SelectItem?st?=?new?SelectItem(new?Long(i),?"No."?+?i); ??
- ????????????countyList[i]?=?st; ??
- ????????} ??
- ??
- ????????return?countyList; ??
- ????} ??
- ??
- ????public?void?setCountyList(SelectItem[]?countyList)?{ ??
- ??
- ????????this.countyList?=?countyList; ??
- ????} ??
- ??
- ????public?javax.faces.component.html.HtmlSelectOneListbox?getCountySelectList()?{ ??
- ????????return?countySelectList; ??
- ????} ??
- ??
- ????public?void?setCountySelectList( ??
- ??
- ????javax.faces.component.html.HtmlSelectOneListbox?countySelectList)?{ ??
- ????????this.countySelectList?=?countySelectList; ??
- ????} ??
- ??
- ????public?String?getMessage()?{ ??
- ????????return?message; ??
- ????} ??
- ??
- ????public?void?setMessage(String?message)?{ ??
- ????????this.message?=?message; ??
- ????} ??
- ??
- ????public?Long?getTownId()?{ ??
- ????????return?townId; ??
- ????} ??
- ??
- ????public?void?setTownId(Long?townId)?{ ??
- ????????this.townId?=?townId; ??
- ????} ??
- ??
- ????public?SelectItem[]?getTownList()?{ ??
- ??
- ????????if?(townList?==?null)?{ ??
- ????????????townList?=?new?SelectItem[0]; ??
- ????????} ??
- ??
- ????????return?townList; ??
- ????} ??
- ??
- ????public?void?setTownList(SelectItem[]?townList)?{ ??
- ????????this.townList?=?townList; ??
- ????} ??
- ??
- ????public?HtmlSelectOneListbox?getTownSelectList()?{ ??
- ????????return?townSelectList; ??
- ????} ??
- ??
- ????public?void?setTownSelectList(HtmlSelectOneListbox?townSelectList)?{ ??
- ????????this.townSelectList?=?townSelectList; ??
- ????} ??
- } ??
配置文件很简单
- <!---->xml?version="1.0"?encoding="UTF-8"?>??
- <!---->>??
- ??
- <faces-config>??
- ???? ??
- ????<managed-bean>??
- ????????<managed-bean-name>userBeanmanaged-bean-name>??
- ????????<managed-bean-class>??
- ????????????org.hblz.common.web.jsf.UserBean ??
- ????????managed-bean-class>??
- ????????<managed-bean-scope>requestmanaged-bean-scope>??
- ????managed-bean>??
- ???? ??
- ???? ??
- faces-config>??
- ??
?
下面是JSP代码
xml 代码
- <%@?page?language="java"?pageEncoding="ISO-8859-1"%>??
- <%@?taglib?uri="http://java.sun.com/jsf/html"?prefix="h"%>??
- <%@?taglib?uri="http://java.sun.com/jsf/core"?prefix="f"%>??
- ??
- <% ??
- ????String?path?=?request.getContextPath(); ??
- ????String?basePath?=?request.getScheme()?+?"://" ??
- ????????????+?request.getServerName()?+?":"?+?request.getServerPort() ??
- ????????????+?path?+?"/"; ??
- %>??
- ??
- <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
- <html>??
- ????<head>??
- ????????<base?href="<%=basePath%>">??
- ??
- ????????<title>My?JSF?'hello.jsp'?starting?page</title>??
- ? ??
- ????</head>??
- ??
- ????<body>??
- ????????<f:view>??
- ????????????<h:form>??
- ????????????????<h:selectOneListbox?id="countySelectList"??
- ????????????????????value="#{userBean.countyId}"?binding="#{userBean.countySelectList}"??
- ????????????????????size="1"?onchange="this.form.submit()"??
- ????????????????????valueChangeListener="#{userBean.processCountySelectListChange}"??
- ????????????????????immediate="true">??
- ????????????????????<f:selectItems?value="#{userBean.countyList}"?/>??
- ????????????????</h:selectOneListbox>??
- ??
- ????????????????<h:selectOneListbox?id="townSelectList"?value="#{userBean.townId}"??
- ????????????????????binding="#{userBean.townSelectList}"?size="1"??
- ????????????????????onchange="this.form.submit()"??
- ????????????????????valueChangeListener="#{userBean.processTownSelectListChange}"??
- ????????????????????immediate="true">??
- ????????????????????<f:selectItems?value="#{userBean.townList}"?/>??
- ????????????????</h:selectOneListbox>??
- ??
- ??
- ????????????????<h:outputText?value="#{userBean.message}"></h:outputText>??
- ????????????</h:form>??
- ????????</f:view>??
- ????</body>??
- </html>??
1 楼
bingxue2332
2007-04-19
问题是第一次打开和第一次操作都没有问题,但是当操作一次在下拉后就出现了错误,错误信息如下:
java.util.NoSuchElementException
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:96)
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:119)
javax.faces.component.UISelectOne.matchValue(UISelectOne.java:141)
javax.faces.component.UISelectOne.validateValue(UISelectOne.java:114)
javax.faces.component.UIInput.validate(UIInput.java:634)
javax.faces.component.UIInput.executeValidate(UIInput.java:838)
javax.faces.component.UIInput.processDecodes(UIInput.java:383)
javax.faces.component.UIForm.processDecodes(UIForm.java:144)
javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:872)
javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:306)
com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:79)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
java.util.NoSuchElementException
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:96)
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:119)
javax.faces.component.UISelectOne.matchValue(UISelectOne.java:141)
javax.faces.component.UISelectOne.validateValue(UISelectOne.java:114)
javax.faces.component.UIInput.validate(UIInput.java:634)
javax.faces.component.UIInput.executeValidate(UIInput.java:838)
javax.faces.component.UIInput.processDecodes(UIInput.java:383)
javax.faces.component.UIForm.processDecodes(UIForm.java:144)
javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:872)
javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:306)
com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:79)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
2 楼
jones
2007-04-20
表达式:#{userBean.countyId}计算的值没有包含在下拉列表的值中,
selectOneListbox是通过value值来确定那个被选中的,如果List提供的可选值中没有value属性指定的值就会 NoSuchElementException
这个异常是说value的值在控件所提供的选项中不存在具有这个值的选项的意思!
selectOneListbox是通过value值来确定那个被选中的,如果List提供的可选值中没有value属性指定的值就会 NoSuchElementException
这个异常是说value的值在控件所提供的选项中不存在具有这个值的选项的意思!
3 楼
penghao122
2007-04-20
断点跟踪一下。。
某些是空的
某些是空的
4 楼
snakeskin
2008-04-08
验证器出现了错误,在validate phase这个阶段,获得的下拉列表为空,所以验证失败,用session bean能解决这个问题