这是一个Servlet应用。。
该版本用到了Json-lib工具包,关于Json-lib的使用,请参见我的博文
http://blog.csdn.net/jadyer/archive/2011/02/01/6171659.aspx
首先是web.xml文件
<servlet-mapping>
<servlet-name>InitProvinceServlet</servlet-name>
<url-pattern>/servlet/InitProvinceServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetCityServlet</servlet-name>
<url-pattern>/servlet/GetCityServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetAreaServlet</servlet-name>
<url-pattern>/servlet/GetAreaServlet</url-pattern>
</servlet-mapping>
<!-- 在version="2.5"中,可以直接在<welcome-file-list>里执行Servlet应用 -->
<welcome-file-list>
<welcome-file>servlet/InitProvinceServlet</welcome-file>
</welcome-file-list>
</web-app>
然后是用来显示省市三级联动效果的index.jsp页面
function getCitys(provinceID){
//若省份项选中了【==请选择省份==】则清空城市列表数据,并显示【==请选择城市==】
if('0' == provinceID){
clearCitys();
return;
}
xmlHTTP.open('GET', '${pageContext.request.contextPath}/servlet/GetCityServlet?provinceID='+provinceID, true);
xmlHTTP.onreadystatechange = callbackCitys; //设置回调函数
xmlHTTP.setRequestHeader("if-Modified-Since", "0"); //设置不使用浏览器缓存
xmlHTTP.send(null); //发送请求
}
function callbackCitys(){
if(4==xmlHTTP.readyState && 200==xmlHTTP.status){
//此时返回的数据类似于【[{"areas":[],"cityID":4,"cityName":"哈尔滨","description":"著名的冰城","parentID":0},{"areas":[],"cityID":5,"cityName":"佳木斯","description":"传说的贼城","parentID":0},{"areas":[],"cityID":6,"cityName":"双鸭山","description":"满族发源地","parentID":0}]】
var citys = eval(xmlHTTP.responseText);
clearCitys();
var city = document.getElementById("city");
for(var i=0; i<citys.length; i++){
city.options[i+1] = new Option(citys[i].cityName, citys[i].cityID); //new Option(text,value)
}
}
}
//清空城市列表数据
function clearCitys(){
var city = document.getElementById('city');
document.getElementById("area").innerHTML = "";
city.length = 0;
city.options[0] = new Option('==请选择城市==', '0');
}
function getArea(cityID){
if('0' == cityID){
document.getElementById("area").innerHTML = "";
return;
}
xmlHTTP.open('GET', '${pageContext.request.contextPath}/servlet/GetAreaServlet?cityID='+cityID, true);
xmlHTTP.onreadystatechange = callbackArea;
xmlHTTP.setRequestHeader("if-Modified-Since", "0");
xmlHTTP.send(null);
}
function callbackArea(){
if(4==xmlHTTP.readyState && 200==xmlHTTP.status){
//此时返回的数据类似于【{"areas":[{"areas":[],"cityID":13,"cityName":"巴彦县","description":"我的家乡","parentID":0},{"areas":[],"cityID":14,"cityName":"延寿县","description":"延寿山庄","parentID":0},{"areas":[],"cityID":15,"cityName":"木兰县","description":"慈航古寺","parentID":0}],"cityID":4,"cityName":"哈尔滨","description":"著名的冰城","parentID":0}】
var city = eval('(' + xmlHTTP.responseText + ')');
var msg = "<p><b>" + city.cityName +"市为:" + city.description + "</b><br/>";
if(0 == city.areas.length){
msg += "没有区县";
}else{
msg += "其下有" + city.areas.length + "个区县,依次为:<br/>";
var areas = city.areas;
for(var i=0; i<areas.length; i++){
msg += ("名称:" + areas[i].cityName + " 描述:" + areas[i].description + "<br/>");
}
}
msg += "</p>";
document.getElementById("area").innerHTML = msg;
}
}
</script>
省份:
<select id="province" onchange="getCitys(this.value)">
<option value="0">==请选择省份==</option>
<c:forEach items="${requestScope.provinces}" var="province">
<option value="${province.cityID}">${province.cityName}</option>
</c:forEach>
</select>
城市:
<select id="city" onchange="getArea(this.value)">
<option>==请选择城市==</option>
</select>
<div id="area"></div>
然后是用于从数据库执行查询操作的CityDao.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.jadyer.model.City;
/**
* 这里的代码,应该重构一下,但为了演示方便,就没有重构
*/
public class CityDao {
/**
* 根据父ID得到所有一级子类
* @see 如果传递为0,则得到所有根节点
*/
public List<City> findByParentID(int parentID){
String sql = "select cityID, cityName, description from city where parentID=?";
List<City> citys = new ArrayList<City>();
try {
Context context = new InitialContext();
//有关jdbc/oracleds连接池的配置,参见http://blog.csdn.net/jadyer/archive/2010/11/10/6001023.aspx
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, parentID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
citys.add(new City(rs.getInt("cityID"), rs.getString("cityName"), rs.getString("description")));
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return citys;
}
/**
* 根据ID得到该城市的详细信息
*/
public City fingByCityID(int cityID){
String sql = "select cityName, description from city where cityID=?";
City city = null;
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, cityID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
city = new City(cityID, rs.getString("cityName"), rs.getString("description"));
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return city;
}
}
用于储存City信息的实体类
import java.util.List;
public class City {
private Integer parentID;
private Integer cityID;
private String cityName;
private String description;
private List<City> areas; //新增。。用于储存该城市下所有区县的详细信息
/* 五个属性的getter和setter方法略 */
public City(){}
public City(Integer cityID, String cityName, String description){
this.cityID = cityID;
this.cityName = cityName;
this.description = description;
}
}
用于初始化所有省份信息的InitProvinceServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jadyer.dao.CityDao;
@SuppressWarnings("serial")
public class InitProvinceServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
CityDao cityDao = new CityDao();
//获取所有省份的信息
request.setAttribute("provinces", cityDao.findByParentID(0));
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
用于获取指定省份下的所有城市信息的GetCityServlet.java
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import com.jadyer.dao.CityDao;
import com.jadyer.model.City;
@SuppressWarnings("serial")
public class GetCityServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
int provinceID = Integer.parseInt(request.getParameter("provinceID"));
CityDao cityDao = new CityDao();
//返回该省份下的所有城市的信息
List<City> citys = cityDao.findByParentID(provinceID);
JSONArray jsonObject = JSONArray.fromObject(citys);
response.getWriter().println(jsonObject.toString());
}
}
用于获取指定城市下的所有区县信息的GetAreaServlet.java
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.jadyer.dao.CityDao;
import com.jadyer.model.City;
@SuppressWarnings("serial")
public class GetAreaServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
int cityID = Integer.parseInt(request.getParameter("cityID"));
CityDao cityDao = new CityDao();
//得到该城市下所有区县的详细信息的集合
List<City> areas = cityDao.findByParentID(cityID);
//得到当前城市的详细信息
City city22 = cityDao.fingByCityID(cityID);
city22.setAreas(areas);
JSONObject jsonObject = JSONObject.fromObject(city22);
response.getWriter().println(jsonObject.toString());
}
}
最后是数据库脚本文件
--创建序列
create sequence city_sequence increment by 1 start with 1 nomaxvalue nocycle;
--添加一级菜单
insert into city values(city_sequence.nextval, 0, 1, '黑龙江', '东北三省的黑龙江省');
insert into city values(city_sequence.nextval, 0, 2, '吉林省', '东北三省的吉林省');
insert into city values(city_sequence.nextval, 0, 3, '辽宁省', '东北三省的辽宁省');
--添加二级菜单
insert into city values(city_sequence.nextval, 1, 4, '哈尔滨', '著名的冰城');
insert into city values(city_sequence.nextval, 1, 5, '佳木斯', '传说的贼城');
insert into city values(city_sequence.nextval, 1, 6, '双鸭山', '满族发源地');
insert into city values(city_sequence.nextval, 2, 7, '长春', '中国汽车之都');
insert into city values(city_sequence.nextval, 2, 8, '辽源', '中国琵琶之乡');
insert into city values(city_sequence.nextval, 2, 9, '通化', '中国医药之城');
insert into city values(city_sequence.nextval, 3, 10, '沈阳', '中国优秀旅游城市');
insert into city values(city_sequence.nextval, 3, 11, '大连', '中国第一海军广场');
insert into city values(city_sequence.nextval, 3, 12, '铁岭', '中国小品王的家乡');
--添加三级菜单
--这里只提供了黑龙江省的三级联动数据,关于其它省份的数据,请自行添加
insert into city values(city_sequence.nextval, 4, 13, '巴彦县', '我的家乡');
insert into city values(city_sequence.nextval, 4, 14, '延寿县', '延寿山庄');
insert into city values(city_sequence.nextval, 4, 15, '木兰县', '慈航古寺');
insert into city values(city_sequence.nextval, 5, 16, '抚远县', '白四爷庙');
insert into city values(city_sequence.nextval, 5, 17, '汤原县', '西郎君城');
insert into city values(city_sequence.nextval, 5, 18, '桦川县', '鱼米之乡');
insert into city values(city_sequence.nextval, 6, 19, '集贤县', '大菩提寺');
insert into city values(city_sequence.nextval, 6, 20, '宝清县', '雁窝之岛');
insert into city values(city_sequence.nextval, 6, 21, '友谊县', '中苏友谊');