//首先创建 两级联动的两个实体类 省市类package com.javamail.controller.vo;import java.util.List;public class Province { private int pid; private String pname; private Listcities; public Province(int pid, String pname, List cities) { super(); this.pid = pid; this.pname = pname; this.cities = cities; } public Province() { // TODO Auto-generated constructor stub } public List getCities() { return cities; } public void setCities(List cities) { this.cities = cities; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } }//城市类package com.javamail.controller.vo;public class City { private int cid; private String cname; private int parentid; public City(int cid, String cname) { super(); this.cid = cid; this.cname = cname; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public int getParentid() { return parentid; } public void setParentid(int parentid) { this.parentid = parentid; } public City() { // TODO Auto-generated constructor stub } public City(int cid, String cname, int parentid) { super(); this.cid = cid; this.cname = cname; this.parentid = parentid; } }//后台 组合数据 用jackson把list转换成 json格式的数据传到页面package com.javamail.controller;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import java.util.ArrayList;import java.util.List;import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.javamail.controller.vo.City;import com.javamail.controller.vo.Province;@Controller@RequestMapping(value="/threelinkage")public class ThreeLinkage { @RequestMapping(value="redirt") public ModelAndView redirt(){ ModelMap modelMap = new ModelMap(); //创建第一个省下的城市 List cs1 = new ArrayList (); City c1=new City(); c1.setCid(10); c1.setCname("涿州市"); c1.setParentid(1); City c2=new City(); c2.setCid(11); c2.setCname("高碑店"); c2.setParentid(1); cs1.add(c1); cs1.add(c2); //创建第一个省 Province province = new Province(); province.setPid(1); province.setPname("河北省"); province.setCities(cs1); //创建第二个省城市 List cs2 = new ArrayList (); City c3=new City(); c3.setCid(21); c3.setCname("朝阳区"); c3.setParentid(2); City c4=new City(); c4.setCid(11); c4.setCname("海淀区"); c4.setParentid(2); cs2.add(c3); cs2.add(c4); //创建第二个省 Province province2 = new Province(); province2.setPid(2); province2.setCities(cs2); province2.setPname("北京"); //放在一个List里面 List p = new ArrayList (); p.add(province); p.add(province2); //把list转换成json格式 ObjectMapper mapper = new ObjectMapper(); Writer strWriter = new StringWriter(); try { mapper.writeValue(strWriter, p); modelMap.put("data",strWriter); System.out.println(strWriter); } catch (JsonGenerationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ModelAndView modelAndView = new ModelAndView("threelinkage",modelMap); return modelAndView; }}//jsp页面接收数据 转成json对象 进行操作<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here