省市联动 json ajax
来源:互联网 发布:windows 7经典主题 编辑:程序博客网 时间:2024/04/30 14:43
provice.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$("#provice").change(function(){
//当改变时,获取选中的省份的id
var pid=$(this).val();
$.post("${pageContext.request.contextPath}/CityServlet",{"pid":pid},function(data){
//alert(data);
var $city = $("#city");
$city.html("<option >--请选择--</option>")
$(data).each(function(i,n){
$city.append("<option value=" + n.cid + ">" + n.cname + "</option>");
})
},"json");
})
})
</script>
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<td>籍贯</td>
<td>
<select id="provice" name="provice">
<option value="">--请选择--</option>
<c:forEach var="p" items="${list }">
<option value="${p.pid}">${p.pname}</option>
</c:forEach>
</select>
<select id="city" name="city">
<option >--请选择--</option>
</select>
</td>
</tr>
</table>
</body>
</html>
ProviceServlet
public class ProviceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
FindDao provice = new FindDao();
List<domain> list = provice.provice();
if (list != null) {
request.setAttribute("list", list);
request.getRequestDispatcher("/provice.jsp").forward(request,
response);
} else {
System.out.println("错误提示到页面信息");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CityServlet
public class CityServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("UTF-8");
String pid = request.getParameter("pid");
FindDao city=new FindDao();
List<domain> list = city.city(pid);
//把list转为json数组,数组中保存着json样式
JsonConfig config=new JsonConfig();
//不包括这些数据
config.setExcludes(new String[]{"pid","pname"});
JSONArray jsonArray = JSONArray.fromObject(list, config);
System.out.println(jsonArray.toString());
response.setContentType("text/html;charset=UTF-8");
//以数组的形式响应出去
response.getWriter().println(jsonArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
FindDao
public class FindDao {
public List<domain> provice() throws Exception {
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select *from provice";
List<domain> list = qr.query(sql, new BeanListHandler<domain>(domain.class));
return list;
}
public List<domain> city(String pid) throws Exception{
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select *from city where pid =?";
List<domain> list = qr.query(sql, new BeanListHandler<domain>(domain.class),pid);
return list;
}
}
- 省市联动 json ajax
- ajax与json实现省市二级联动
- JQ省市联动,json
- Ajax联动实现省市联动
- ajax pro省市联动
- ajax省市县联动
- jquery AJAX 省市联动
- AJAX实现省市联动
- 省市联动 ajax
- 省市联动ajax代码
- AJAX实现省市联动
- ajax 省市联动 代码
- 4. AJAX 省市联动
- ajax+js省市联动
- ajax写省市联动
- ajax+json+dom+php+mysql省市县三级联动
- JSP页面使用JSON和ajax实现省市联动
- AJAX+JSON 实现省市县地名三级联动
- 画圆
- studio中http请求方式
- 背包问题的二进制优化
- C++ Move特性.
- Struts2国际化
- 省市联动 json ajax
- 组件、插件、控件的区别
- 编写一个计时器拦截器
- Android WebView 输入框键盘不弹出
- winfrom窗体界面加载闪屏清除
- Struts2与ajax
- Geometry
- 第四范式程晓澄:机器学习如何优化推荐系统
- 装修设计风格