cityForm.java如下
package com.zzcq.stationsrv.system.city;import org.apache.struts.action.*;
import javax.servlet.http.*; public class CityForm extends ActionForm {
private int areaID;
private String areaName;
private String cityCode;
private int cityID;
private String cityName;
private int countryID;
private String countryName;
private String operate;
public int getAreaID() {
return areaID;
}
public void setAreaID(int areaID) {
this.areaID = areaID;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public int getCityID() {
return cityID;
}
public void setCityID(int cityID) {
this.cityID = cityID;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
/**@todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
public String getOperate() {
return operate;
}
public void setOperate(String operate) {
this.operate = operate;
}
}
package com.zzcq.stationsrv.system.city;import org.apache.struts.action.*;
import javax.servlet.http.*; public class CityForm extends ActionForm {
private int areaID;
private String areaName;
private String cityCode;
private int cityID;
private String cityName;
private int countryID;
private String countryName;
private String operate;
public int getAreaID() {
return areaID;
}
public void setAreaID(int areaID) {
this.areaID = areaID;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public int getCityID() {
return cityID;
}
public void setCityID(int cityID) {
this.cityID = cityID;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
/**@todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
public String getOperate() {
return operate;
}
public void setOperate(String operate) {
this.operate = operate;
}
}
显示国家标签如下:
<html:select name="cityForm" property="countryName" style="width:160">
<html:option value="">请选择</html:option>
<html:options collection ="cityShow" property="value" labelProperty="label"/>
</html:select>显示省份标签如下:
<html:select name="cityForm" property="areaName" style="width:160">
<html:option value="">请选择</html:option>
<html:options collection="areaShow" property="value" labelProperty="label"/>
</html:select>我现在就不知道如何写js代码来实现这个连动功能
那位兄弟能否帮我的js代码填写完整
或者我写得不完善的地方,帮我修改一下?
非常感谢
以下是html页面取出select的值的js方法:<html>
<head>
<title> test select book change </title>
<script language="javascript">
function doShowCountry(scode) {
window.alert(scode);
yourform.sumbit();
}
</script>
<head>
<body>
<select name="selCountry" onchange="doShowCountry(selCountry.options[selCountry.selectedIndex].value)">
<option value="001">China</option>
<option value="002">American</option>
</select>
<body>
Struct也应该相同,也可以通过post到同一个页面来实现更新当前页的其它select控件中的数据。(get 和 set)
只是实现了你的基本需求,具体的逻辑很简单,就是按国家的排列顺序来
存储城市的2维数组顺序,具体应该不用我详细解释了吧:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>example</title>
</head>
<script language="JavaScript">
<!--
var citiesValue = new Array;
citiesValue[0] = new Array;
citiesValue[0][0] = "value: 国家111的城市111";
citiesValue[0][1] = "value: 国家111的城市222";
citiesValue[0][2] = "value: 国家111的城市333";
citiesValue[1] = new Array;
citiesValue[1][0] = "value: 国家222的城市111";
citiesValue[1][1] = "value: 国家222的城市222";
citiesValue[2] = new Array;
citiesValue[2][0] = "value: 国家333的城市111";
citiesValue[2][1] = "value: 国家333的城市222";
citiesValue[2][2] = "value: 国家333的城市333";
citiesValue[2][3] = "value: 国家333的城市444";
var cities = new Array;
cities[0] = new Array;
cities[0][0] = "国家111的城市111";
cities[0][1] = "国家111的城市222";
cities[0][2] = "国家111的城市333";
cities[1] = new Array;
cities[1][0] = "国家222的城市111";
cities[1][1] = "国家222的城市222";
cities[2] = new Array;
cities[2][0] = "国家333的城市111";
cities[2][1] = "国家333的城市222";
cities[2][2] = "国家333的城市333";
cities[2][3] = "国家333的城市444";
function selectCities(){
var country_index = country.selectedIndex; while(city.options.length>0) {
city.options.remove(0);
}
for(var i=0; i<cities[country_index].length; i++) {
var newCity = document.createElement("OPTION");
newCity.value = citiesValue[country_index][i];
newCity.text = cities[country_index][i];
city.add(newCity);
}
}function showValue() {
alert('country.value='+country.value);
alert("country显示名称="+country.options[country.selectedIndex].text);
alert('city.value='+city.value);
alert("city显示名称="+city.options[city.selectedIndex].text);
}window.onload=function() {
selectCities();
}
//-->
</script>
<body>
<table border=1>
<tr>
<td>
<select name="country" onchange="selectCities()">
<option value="001">国家111</option>
<option value="002">国家222</option>
<option value="003">国家333</option>
</select>
</td>
</tr>
<tr>
<td>
<select name="city">
</select>
</td>
</tr>
<table>
<tr>
<td><input type="button" value="当前选中的值" onclick="showValue()"></td>
</tr>
</table>
</table>
</body>
</html>
反正我给你做个例子,也跑去查查具体的用法的,
所以我就不给你完全实现了,做个简单例子,
你可以去多看看,然后优化下处理方式,现在给的例子我自己感觉不是很好,
不过我不想花太多时间来仔细考虑这个问题,因为对我来说价值不大,而且剩下的你自己可以搞定的应该。
难点是如何生成前面的填充二位数组的程序,我想struts本身是解决不了这个问题的,我建议你制作一个taglib,根据传入的request的参数产生js。
这样肯定可以。
其实生成填充数组很容易的啊,后台查询的时候排好序,然后直接生成一个JAVA的数组也可以,接着直接输出,要不到页面直接输出对应的数组也可以,
本身数据库中要做查询的记录相关很容易组织好数据的。当然使用数组不是很好,如果我要用,那么会使用XML来处理数据,从数据库搜索出来结果集就直接装成DOM对象返回到页面,页面做一个数据岛,每次改变COUNTRY时就从数据岛中查询
出改变后COUNTRY的城市信息。(相关处理方式可以在MSDN上找到例子的)我不喜欢taglib,taglib对处理数据几乎就没有好处,唯一的好处是,在HTML页面中看起来
都是标签,其实还是嵌入JAVA代码,
如果一个功能模块需要处理,那么我宁可直接写一个处理类,在逻辑层调用这个处理类处理数据,把处理完成的数据传递到展示层展示,如果这样不爽即使直接在页面调用这个处理类来处理数据,我看不出这么做比用TAGLIB差哪。
如果说页面处理,我还是最喜欢HTC了,可复用性很高。
最近在研究XML,感觉用它才能真正的分层,至少把展示层和逻辑层几乎完全剥离了。
<SCRIPT LANGUAGE="JavaScript">
<!--
function changeEx(){
var parentID;
var selIndex;
parentID=document.form1.main.options[document.form1.main.selectedIndex].value;
document.form1.hidParentID.value=parentID;
//document.form1.submit();
location.href="本页?parentID="+parentID;
}//-->
</SCRIPT>在主类型中的onchange事件调用这个函数,从类型选择的时候"select .....where parentID='"+ request.getParameter("hidParentID")"+ "'";就好了,不知道对你有没有帮助了