Ajax实例-地域查询匹配
来源:互联网 发布:犀牛社淘宝论坛 编辑:程序博客网 时间:2024/05/16 07:06
两个servlet两个jsp!
- GetAreaAction
package ajax;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.catalina.User;import com.alibaba.fastjson.JSONObject;/** * Servlet implementation class GetCityAction */@WebServlet("/GetAreaAction")public class GetAreaAction extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetAreaAction() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); String citycode = request.getParameter("citycode"); System.out.println(citycode); if("gz".equals(citycode)){ String arealist = "[{\"code\":\"th\",\"name\":\"天河\"},{\"code\":\"hz\",\"name\":\"海珠\"}]"; response.getWriter().write(arealist); }else{ String arealist = "[{\"code\":\"nh\",\"name\":\"南海\"},{\"code\":\"sd\",\"name\":\"顺德\"}]"; response.getWriter().write(arealist); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*//json字符串转换成java对象 JSONObject.parseObject("json字符串",User.class); //java对象转换成json字符串 String jsonstr= JSONObject.toJSONString(List<City> list);*/ String str[]={"","",""}; doGet(request, response); }}
- GetCityAction
package ajax;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class GetCityAction */@WebServlet("/GetCityAction")public class GetCityAction extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetCityAction() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); String citycode = request.getParameter("citycode"); System.out.println(citycode); String citylist = "[{\"code\":\"gz\",\"name\":\"广州\"},{\"code\":\"sz\",\"name\":\"深圳\"}]"; response.getWriter().write(citylist); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
- city.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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>地址选择</title></head><body>城市<select id ="city"></select>区域<select id ="area"></select><script type="text/javascript" src="js/jquery.min.js"> </script></body><script type="text/javascript">$(document).ready(function () { getcity(); $("#city").change(function () { getarea($(this).val()); //alert("test"); }); function getcity(){ $.ajax( { type:"get", url:"GetCityAction", data:{"citycode":"gz"}, success: function (data) { console.log(data); $("#city").empty(); $.each(data,function(n,item){ var option=$("<option></option>"); option.val(item.code); option.text(item.name); option.appendTo($("#city")); }); getarea($("#city").val()); } } ); } function getarea(citycode){ $.getJSON("GetAreaAction", {"citycode":citycode},function (data){ console.log(data); $("#area").empty(); $.each(data,function(n,item){ var option=$("<option></option>"); option.val(item.code); option.text(item.name); option.appendTo($("#area")); }); }); } /* function getarea(citycode){ $.ajax( { async:false,//默认为true,异步请求 dataType:"json", type:"get", url:"GetAreaAction", data:{"citycode":citycode}, success: function (data) { console.log(data); $("#area").empty(); $.each(data,function(n,item){ var option=$("<option></option>"); option.val(item.code); option.text(item.name); option.appendTo($("#area")); }); } } ); }*/});</script></html>
- area.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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>地址选择</title></head><body><select id ="city"></select><script type="text/javascript" src="js/jquery.min.js"> </script></body><script type="text/javascript">$(document).ready(function(){ getcity(); function getcity(){ $.ajax( { type:"get", url:"GetCityAction", data:{"citycode":"gz"}, success:function(data){ $("#city").empty(); $.each(data,function (n,item) { var option=$("<option></option>") option.val(item.code) option.text(item.name); option.appendTo($("#city")) }) /* var option=$("<option></option>") */ } } ); }})</script></html>
0 0
- Ajax实例-地域查询匹配
- oracle查询IP地域信息
- 3行代码实现地域查询
- 用淘宝接口通过ip查询地域
- AJAX实战_搜索框(匹配字符串查询)
- AJAX for PHP简单表数据查询实例
- AJAX for Java简单表数据查询实例
- AJAX For.NET 取数据以及数据查询实例,
- Ajax的async属性(通过ajax请求分页查询实例)
- AJAX查询
- 原生ajax 实现输入框模糊查询,自动匹配数据库数据
- AJAX 实例
- Ajax实例
- Ajax 实例
- ajax实例
- Ajax实例
- Ajax实例
- Ajax 实例
- 线程解析之概念
- struts2中的Action接收表单传递过来的参数有3种方法
- Date问题
- C语言typedef的用法
- 第三方开源库:日期时间选择器(TimePickerDialog)+ 省市区三级联动(CityPicker) + 一级滚动
- Ajax实例-地域查询匹配
- webview有两个方法:setWebChromeClient和setWebClient
- 常用的编程算法
- Codeforce 450B Jzzhu and Sequences
- 专访网易视频云郭再荣:立足技术服务,深耕场景化应用
- ThinkPHP框架配置
- C++ 模板详解
- 汉诺塔问题,有空再研究
- [Usaco08Jan] Cow Contest