省市联动
步骤分析:
1.表
2.页面上有两个下拉选 省份的下拉选一般是固定的 页面加载的时候读取所有的省份
3.当省份改变的时候,获取省份的信息,发送一个ajax请求,去市的表中查询相应省份的所有市,然后将他们加载到市下拉选上
4.selectProServlet selectCityServlet
技术分析:
json
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。
json格式:
格式1:value可以为任意值
{"key":value,"key1":value1}
格式2:e可以为任意值
[e1,e2]
jsonlib工具类,可以使对象转换成json数据
1.导入jar包
2.使用api
JSONArray.fromObject(对象) 数组和list
JSONObject.fromObject(对象) bean和map
SelectProServlet
package com.feizhu.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.feizhu.domain.Province;
import com.feizhu.service.ProvinceService;
import net.sf.json.JSONArray;
/**
* 查询所有的省
*/
public class SelectProServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用service 查询所有的省份
List<Province> list = null;
try {
list = new ProvinceService().findAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将所有的省份写回去
response.setContentType("text/html;charset=utf-8");
if(list!=null && list.size()>0) {
response.getWriter().print(JSONArray.fromObject(list));
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ProvinceService
package com.feizhu.service;
import java.sql.SQLException;
import java.util.List;
import com.feizhu.dao.ProvinceDao;
import com.feizhu.domain.Province;
public class ProvinceService {
public List<Province> findAll() throws SQLException {
return new ProvinceDao().findAll();
}
}
ProvinceDao
package com.feizhu.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.feizhu.domain.Province;
import com.feizhu.utils.DataSourceUtils;
public class ProvinceDao {
public List<Province> findAll() throws SQLException {
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from province";
return qr.query(sql, new BeanListHandler<>(Province.class));
}
}
SelectCityServlet
package com.feizhu.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.feizhu.domain.City;
import com.feizhu.service.CityService;
import net.sf.json.JSONArray;
/**
* 根据省份id查询所有的市
*/
public class SelectCityServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取省份id
String pid=request.getParameter("pid");
System.out.println(pid);
//调用service 查询所有的市 返回list
List<City> list = null;
try {
list = new CityService().findCitiesByPid(pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将list转换成json 返回页面
if(list!=null && list.size()>0) {
response.getWriter().println(JSONArray.fromObject(list));
}
System.out.println(list);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
CityService
package com.feizhu.service;
import java.sql.SQLException;
import java.util.List;
import com.feizhu.dao.CityDao;
import com.feizhu.domain.City;
public class CityService {
/**
* 通过省份id 查询所有的市
* @param pid
* @return
* @throws SQLException
*/
public List<City> findCitiesByPid(String pid) throws SQLException {
return new CityDao().findCitiesByPid(pid);
}
}
CityDao
package com.feizhu.dao;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.feizhu.domain.City;
import com.feizhu.utils.DataSourceUtils;
public class CityDao {
public List<City> findCitiesByPid(String pid) throws SQLException {
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from city where provinceid= ?";
return qr.query(sql, new BeanListHandler<>(City.class),pid);
}
}
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)