ajax json select 多级联动

  • m9_805845
    了解作者
  • 91.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-26 01:47
    上传日期
动态创建option,采用json 对象解析后台动态数据。
select.rar
内容介绍
package com.select; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.simple.JSONArray; import org.json.simple.JSONObject; /** * Servlet implementation class for Servlet: selectData * */ public class selectData extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public selectData() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String action = request.getParameter("action"); JSONObject obj = new JSONObject(); // getRegion 获取该区域的省级 if ("getRegion".equals(action)) { JSONArray regions = new JSONArray(); ArrayList list = (ArrayList) new ArrayList();//数据库中获取第一个集合(区域) Map map1 = null; map1 = new HashMap(); map1.put("name", "beijing"); map1.put("value", 1); list.add(map1); map1 = new HashMap(); map1.put("name", "tianjing"); map1.put("value", 2); list.add(map1); map1 = new HashMap(); map1.put("name", "hunan"); map1.put("value", 3); list.add(map1); for (int i = 0; i < list.size(); i++) { Map element = (HashMap) list.get(i); JSONObject s = new JSONObject(); s.put("name", element.get("name")); s.put("value",element.get("value")); regions.add(s); } obj.put("regions", regions); } // getRegion 获取该区域的省级,市区名称 else if ("getCitys".equals(action)) { String regionValue = request.getParameter("regionValue");//通过id获取城市集合 JSONArray citys = new JSONArray(); if(regionValue.equals("1")){//下面处理一样,通过regionValue 参数 --从数据库中获取一个集合 ArrayList list = (ArrayList) new ArrayList(); Map map1 = null; map1 = new HashMap(); map1.put("name", "北京市一"); map1.put("value", "111"); list.add(map1); map1 = new HashMap(); map1.put("name", "北京市二"); map1.put("value", "222"); list.add(map1); map1 = new HashMap(); map1.put("name", "北京市三"); map1.put("value", "333"); list.add(map1); for (int i = 0; i < list.size(); i++) { Map element = (HashMap) list.get(i); JSONObject s = new JSONObject(); s.put("name", element.get("name")); s.put("value",element.get("value")); citys.add(s); } obj.put("citys", citys); }else if(regionValue.equals("2")){ ArrayList list = (ArrayList) new ArrayList(); Map map1 = null; map1 = new HashMap(); map1.put("name", "天津市一"); map1.put("value", "111"); list.add(map1); map1 = new HashMap(); map1.put("name", "天津市二"); map1.put("value", "222"); list.add(map1); map1 = new HashMap(); map1.put("name", "天津市三"); map1.put("value", "333"); list.add(map1); for (int i = 0; i < list.size(); i++) { Map element = (HashMap) list.get(i); JSONObject s = new JSONObject(); s.put("name", element.get("name")); s.put("value",element.get("value")); citys.add(s); } obj.put("citys", citys); }else if(regionValue.equals("3")){ ArrayList list = (ArrayList) new ArrayList(); Map map1 = null; map1 = new HashMap(); map1.put("name", "长沙市一"); map1.put("value", "111"); list.add(map1); map1 = new HashMap(); map1.put("name", "长沙市二"); map1.put("value", "222"); list.add(map1); map1 = new HashMap(); map1.put("name", "长沙市三"); map1.put("value", "333"); list.add(map1); for (int i = 0; i < list.size(); i++) { Map element = (HashMap) list.get(i); JSONObject s = new JSONObject(); s.put("name", element.get("name")); s.put("value",element.get("value")); citys.add(s); } obj.put("citys", citys); } } response.setContentType("text/html; charset=gb2312"); PrintWriter out = response.getWriter(); out.print(obj); out.flush(); out.close(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
评论
    相关推荐