js代码:
function selectProvince(provinceId) {
??? ProvinceAction.getChildByParentId(provinceId, returnProvince);//根据省的id得到市
}
// 回调函数
function returnProvince(data) {//返回一个map<id,name>
??? var city;
??? var city = $("city");
??? dwr.util.removeAllOptions(city);
??? dwr.util.addOptions(city, {'':'=请选择='});
??? dwr.util.addOptions(city, data);//把市添加进去
}
function selectCity(){//选择城市
??? var city = $("city");
??? var city_index = city.selectedIndex;
??? var city_value = city.options[city_index].value;
??? ProvinceAction.getChildByParentId(city_value, returnCountry);//根据市的id得到县
}
function returnCountry(data){
??? var country = $("country");
??? dwr.util.removeAllOptions(country);
??? dwr.util.addOptions(country, {'':'=请选择='});
??? dwr.util.addOptions(country, data);//把县添加进去
}
?
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
??? pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/util.js'></script>
<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/interface/ProvinceAction.js'></script>
<script type="text/javascript" src='js/province.js'></script>
</head>
<body>
省
<select id="province" onchange="selectProvince(this.options[this.selectedIndex].id)">
??? <option>=请选择=</option>
??? <s:iterator id="province" value="list">
??? ??? <option id='<s:property value="id"/>'><s:property value="name" /></option>
??? </s:iterator>
</select>
市<select id="city" onchange="selectCity(this.options[this.selectedIndex].id)">
??? <option>=请选择=</option>
</select>
区/县<select id="country">
??? ??? <option>=请选择=</option>
??? </select>
<span id="showAge"></span>
</body>
</html>
?
Java代码:
package com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bean.Province;
import com.dao.ProvinceDao;
import com.db.DBUtil;
public class ProvinceDaoImpl implements ProvinceDao {
??? @Override
??? public List<Province> findAllProvince() {
??? ??? Connection conn = DBUtil.getConnection();
??? ??? PreparedStatement ps = null;
??? ??? List<Province> list = null;
??? ??? try {
??? ??? ??? ps = conn.prepareStatement("select * from provice where parentid='0'");
??? ??? ??? list = executeQuery(ps);
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? } finally {
??? ??? ??? DBUtil.close(ps);
??? ??? }
??? ??? return list;
??? }
??? @Override
??? public Map<String,String> getChildByParentId(String parentId) {
??? ??? System.out.println("----"+parentId);
??? ??? Connection conn = DBUtil.getConnection();
??? ??? PreparedStatement ps = null;
??? ??? Map<String,String> map = new HashMap<String,String>();
??? ??? ResultSet rs = null;
??? ??? try {
??? ??? ??? ps = conn.prepareStatement("select * from provice where parentId=?");
??? ??? ??? ps.setString(1, parentId);
??? ??? ??? rs = ps.executeQuery();
??? ??? ??? while(rs.next()){
??? ??? ??? ??? map.put(rs.getString("id"), rs.getString("name"));
??? ??? ??? }
??? ??? } catch (SQLException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return map;
??? }
???
??? public List<Province> executeQuery(PreparedStatement ps){
??? ??? List<Province> list = null;
??? ??? ResultSet rs = null;
??? ??? try {
??? ??? ??? rs = ps.executeQuery();
??? ??? ??? list = new ArrayList<Province>();
??? ??? ??? while (rs.next()) {
??? ??? ??? ??? String id = rs.getString("id");
??? ??? ??? ??? String name = rs.getString("name");
??? ??? ??? ??? String parentId = rs.getString("parentid");
??? ??? ??? ??? Province province = new Province();
??? ??? ??? ??? province.setId(id);
??? ??? ??? ??? province.setName(name);
??? ??? ??? ??? province.setParentId(parentId);
??? ??? ??? ??? list.add(province);
??? ??? ??? }
??? ??? } catch (SQLException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return list;
??? }
}
?
dwr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
??? <allow>
??? ??? <create creator="new" javascript="ProvinceAction">
??? ??? ??? <param name="class" value="com.action.ProvinceAction"></param>
??? ??? </create>
??? ??? <convert match="com.bean.Province" javascript="Province" converter="bean"/>
??? </allow>
</dwr>