jsf的问题[有答案后马上给分]

bingxue2332 2007-04-23 05:30:37
package org.hblz.web.jsf.backbean;

import java.util.List;

public class HelloBean {
private SendbookService bookService;

public SendbookService getBookService() {
return bookService;
}

public void setBookService(SendbookService bookService) {
this.bookService = bookService;
}

@SuppressWarnings("unchecked")
public void setTownListValue(ValueChangeEvent event) {
Long ctId = (Long) event.getNewValue();

if (ctId != null) {
List<Town> lst = this.getBookService().getTownList(ctId);

if (lst != null && lst.size() > 0) {
this.townList = new SelectItem[lst.size() + 1];

this.townList = new SelectItem[lst.size() + 1];
this.townList[0] = new SelectItem(new Long(-1), "请选择");
for (int i = 0; i < lst.size(); i++) {
this.townList[i + 1] = new SelectItem(lst.get(i).getId(),
lst.get(i).getName());
}
} else {
this.townList = new SelectItem[1];
this.townList[0] = new SelectItem(new Long(-1), "请选择");
}
}

}

public void setVillageListValue(ValueChangeEvent event) {
System.out.println("setVillageListValue");


Long tnId = (Long) event.getNewValue();
if (tnId != null) {
List<Village> lst = this.getBookService().getVillageList(tnId);

if (lst != null && lst.size() > 0) {
this.villageList = new SelectItem[lst.size() + 1];
this.villageList[0] = new SelectItem(new Long(-1), "请选择");
for (int i = 0; i < lst.size(); i++) {
this.villageList[i + 1] = new SelectItem(
lst.get(i).getId(), lst.get(i).getName());
}
} else {
this.villageList = new SelectItem[1];
this.villageList[0] = new SelectItem(new Long(-1), "请选择");
}

}
}

private HtmlForm mainForm;

private HtmlSelectOneListbox selectCounty;

private HtmlSelectOneListbox selectTown;

private HtmlSelectOneListbox selectVillage;

private SelectItem[] townList = null;

private SelectItem[] countyList = null;

private SelectItem[] villageList = null;

private Long countyId;

private Long townId;

private Long villageId;

public Long getCountyId() {
return countyId;
}

public void setCountyId(Long countyId) {
this.countyId = countyId;
}

public void setCountyList(SelectItem[] countyList) {
this.countyList = countyList;
}

public HtmlForm getMainForm() {
return mainForm;
}

public void setMainForm(HtmlForm mainForm) {
this.mainForm = mainForm;
}

public HtmlSelectOneListbox getSelectCounty() {
return selectCounty;
}

public void setSelectCounty(HtmlSelectOneListbox selectCounty) {
this.selectCounty = selectCounty;
}

public HtmlSelectOneListbox getSelectTown() {
return selectTown;
}

public void setSelectTown(HtmlSelectOneListbox selectTown) {
this.selectTown = selectTown;
}

public HtmlSelectOneListbox getSelectVillage() {
return selectVillage;
}

public void setSelectVillage(HtmlSelectOneListbox selectVillage) {
this.selectVillage = selectVillage;
}

public Long getTownId() {
return townId;
}

public void setTownId(Long townId) {
this.townId = townId;
}

public void setTownList(SelectItem[] townList) {
this.townList = townList;
}

public Long getVillageId() {
return villageId;
}

public void setVillageId(Long villageId) {
this.villageId = villageId;
}

public void setVillageList(SelectItem[] villageList) {
this.villageList = villageList;
}

@SuppressWarnings("unchecked")
public SelectItem[] getCountyList() {
if (countyList == null) {
if (this.countyList == null || this.countyList.length == 0) {
HttpSession session = (HttpSession) FacesContext
.getCurrentInstance().getExternalContext().getSession(
true);// 获得session

User u = (User) session.getAttribute("user");

List<County> countyList = this.getBookService()
.getCountyList(u);
this.countyList = new SelectItem[countyList.size() + 1];
SelectItem s = new SelectItem(new Long(-1), "请选择");
this.countyList[0] = s;
for (int i = 0; i < countyList.size(); i++) {
SelectItem item = new SelectItem(countyList.get(i).getId(),
countyList.get(i).getName());
this.countyList[i + 1] = item;
}
}
}

return countyList;
}

@SuppressWarnings("unchecked")
public SelectItem[] getTownList() {
if (this.townList == null || this.townList.length == 0
|| this.townList.length == 1) {
if (this.countyId != null) {
List<Town> lst = this.getBookService().getTownList(
this.countyId);

if (lst != null && lst.size() > 0) {
this.townList = new SelectItem[lst.size() + 1];

this.townList = new SelectItem[lst.size() + 1];
this.townList[0] = new SelectItem(new Long(-1), "请选择");
for (int i = 0; i < lst.size(); i++) {
this.townList[i + 1] = new SelectItem(lst.get(i)
.getId(), lst.get(i).getName());
}
}
} else {
this.townList = new SelectItem[1];
this.townList[0] = new SelectItem(new Long(-1), "请选择");
}
}
return townList;
}

public SelectItem[] getVillageList() {
if (this.villageList == null) {
this.villageList = new SelectItem[1];
this.villageList[0] = new SelectItem(new Long(-1), "请选择");
}

if (this.townId != null) {
List<Village> lst = this.getBookService().getVillageList(townId);

if (lst != null && lst.size() > 0) {
this.villageList = new SelectItem[lst.size() + 1];
this.villageList[0] = new SelectItem(new Long(-1), "请选择");
for (int i = 0; i < lst.size(); i++) {
this.villageList[i + 1] = new SelectItem(
lst.get(i).getId(), lst.get(i).getName());
}
} else {
this.villageList = new SelectItem[1];
this.villageList[0] = new SelectItem(new Long(-1), "请选择");
}
}

return villageList;
}
}

问题是,在我点击第一个下拉框的时候第二个下拉框可以随着变化,但是下拉第二个下拉框的时候第二个的信息丢失了,第三个下拉框也没有反应。这个是为什么?

jsp的代码是这样的:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ 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 binding="#{bookBean.mainForm}" id="mainForm">

County:<h:selectOneListbox binding="#{helloBean.selectCounty}"
id="selectCounty"
valueChangeListener="#{helloBean.setTownListValue}"
onchange="this.form.submit();" size="1"
value="#{helloBean.countyId}" immediate="true">
<f:selectItems value="#{helloBean.countyList}" />
</h:selectOneListbox>




Town:<h:selectOneListbox binding="#{helloBean.selectTown}"
id="selectTown" onchange="this.form.submit();" size="1"
value="#{helloBean.townId}"
valueChangeListener="#{helloBean.setVillageListValue}"
immediate="true">
<f:selectItems value="#{helloBean.townList}" />
</h:selectOneListbox>


Village:<h:selectOneListbox binding="#{helloBean.selectVillage}"
value="#{helloBean.villageId}" onchange="this.form.submit()"
id="selectVillage" size="1" immediate="true">
<f:selectItems value="#{helloBean.villageList}" />
</h:selectOneListbox>

</h:form>
</f:view>

</body>
</html>
...全文
429 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingxue2332 2007-04-25
  • 打赏
  • 举报
回复
to WIN_ANGEL(金麟岂是池中物,一遇风云便化龙!)

think you!

i will have a try!
WIN_ANGEL 2007-04-23
  • 打赏
  • 举报
回复
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<HTML>
<HEAD>
<style>
<!--
.px10{
padding-bottom: 10px;
}
-->
</style>
<title>a4j-support with Selects</title>
</HEAD>
<body>
<f:view>
<h:outputText value="block设置块显示位置"/>
<a4j:region selfRendered="true">
<h:form>
<a4j:status startStyle="font-color:red"/>
<h:outputText value="tpl" style="display:block"/>
<h:panelGroup style="display:block;" styleClass="px10">
<h:selectOneMenu value="#{Bean.tpl}">
<f:selectItems value="#{Bean.maptpl}" />
<a4j:support event="onchange" action="#{Bean.blockset}"
reRender="Option1"/>
</h:selectOneMenu>
</h:panelGroup>

<h:outputText value="layout" />
<h:panelGroup style="display:block" id="Option1" styleClass="px10">
<h:selectOneMenu value="#{Bean.layout}">
<f:selectItems value="#{Bean.maplayout}" />
<a4j:support event="onchange" action="#{Bean.frameset}"
reRender="Option2" />
</h:selectOneMenu>
</h:panelGroup>

<h:outputText value="frame" />
<h:panelGroup style="display:block" id="Option2" styleClass="px10">
<h:selectOneMenu value="#{Bean.frame}">
<f:selectItems value="#{Bean.mapframe}"/>
</h:selectOneMenu>
</h:panelGroup>
<a4j:commandButton value="设置" action="#{Bean.submitset}"></a4j:commandButton>
</h:form>
</a4j:region>
</f:view>
</body>
</HTML>

应该能看懂吧
<a4j:support event="onchange" action="#{Bean.blockset}"
reRender="Option1"/>
这句表示当下拉菜单改变时 调用Bean里的blockset方法(这个方法作用就是改变另一个下拉列表)
reRender="Option1"表示刷新id="Option1"的h:panelGroup 因为h:selectOneMenu在h:panelGroup里 所以就是刷新了这个下拉菜单
WIN_ANGEL 2007-04-23
  • 打赏
  • 举报
回复
用ajax4jsf

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧