二级联动,实现前一个select下拉框改变,后一个随之改变

今天学习了jQuery中的Ajax,简单的实现一个二级联动的实例,废话不多说,直接上代码

//这是jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here




	省:
	 
	市:
	





//这是servlet的代码
package com.coding.province;

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 net.sf.json.JSON;
import net.sf.json.JSONSerializer;
@WebServlet("/choose")
public class ChooseServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置响应的字符集,防止乱码
		resp.setContentType("text/html;charset=utf8");
		//得到拼接过来的参数
		String param = req.getParameter("name");
		//判断参数的值是否符合条件
		if (param.equals("province")) {
			//传过来的请求想要省的名称的响应,所以获取省份响应给他
			List pro = Province.getAllProvince();
			//拼接成一个json数组形式的字符串返回回去
			//就是这个样子[{"id":1,"name":"安徽"},{"id":2,"name":"广东"},{"id":3,"name":"云南"}]
			//纯手动拼接
			StringBuffer sbf1=new StringBuffer();
			sbf1.append("[");
			for (int i = 0; i < pro.size(); i++) {
				if (i==pro.size()-1) {
					sbf1.append("{\"id\":"+pro.get(i).getId()+",\"name\":\""+pro.get(i).getName()+"\"}");
				}else {
					sbf1.append("{\"id\":"+pro.get(i).getId()+",\"name\":\""+pro.get(i).getName()+"\"},");
				}
			}
			
			sbf1.append("]");
			//发送响应
			resp.getWriter().print(sbf1);
		}
		//这是城市的响应
		//导入工具包可以轻松将list转换成json序列,不用进行手动拼接
		else {
			String cityId = req.getParameter("cityId");
			List list = City.getCityByProvinceId(Long.valueOf(cityId));
			JSON json = JSONSerializer.toJSON(list);
			resp.getWriter().print(json);
		}
	}
}


//下面是添加城市和省份的代码
package com.coding.province;

import java.util.ArrayList;
import java.util.List;

public class Province {

	private Long id;
	private String name;

	public Province(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Province() {
		super();
	}

	public static List getAllProvince() {
		List provinces = new ArrayList();
		provinces.add(new Province(1L, "安徽"));
		provinces.add(new Province(2L, "广东"));
		provinces.add(new Province(3L, "云南"));
		return provinces;
	}
}



package com.coding.province;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class City {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public City() {

	}

	public City(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	/**
	 * 根据省份id查询省份中的城市!
	 * 
	 * @return
	 */
	public static List getCityByProvinceId(Long id) {
		
		List citys = new ArrayList();
		
		if (id == 1) {
			citys = Arrays.asList(
					new City(1L,"合肥"),
					new City(2L,"阜阳"),
					new City(3L,"淮南"),
					new City(4L,"黄山"),
					new City(5L,"宿州"),
					new City(6L,"六安"),
					new City(7L,"蚌埠")
			);
		} else if (id == 2) {
			citys = Arrays.asList(
					new City(11L,"广州"),
					new City(12L,"佛山"),
					new City(13L,"东莞")
			);
		} else if (id == 3) {
			citys = Arrays.asList(
					new City(21L,"昆明"),
					new City(22L,"玉溪"),
					new City(23L,"丽江")
			);
		}
		return citys;
	}
}

将list序列化用到jar包https://download.csdn.net/download/yang_xinqiao/10679729

你可能感兴趣的:(java基础)