简单二级联动(Spring MVC JSON Ajax)用的原生JS

pojo中存放假数据,未连接数据库

前台:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>



    
    二级联动



    
    



Spring MVC :控制层

@RequestMapping("/getCity")
public void Level2Linkage(HttpServletResponse response,String id) throws IOException {
    response.setContentType("text/json;charset=UTF-8");
    if (id==null)
    {
        Province province=new Province();
        Listprovinces =province.getallProvinces();
        JSONArray JSONStr=JSONArray.fromObject(provinces);
        response.getWriter().print(JSONStr);
    }
    else{
        City city=new City();
        List cities=city.getCitites(Integer.parseInt(id));
        JSONArray JSONStr=JSONArray.fromObject(cities);
        response.getWriter().print(JSONStr);
    }

}

pojo:

package pojo;

import com.fasterxml.jackson.annotation.JsonBackReference;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Province implements Serializable {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    @JsonBackReference
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Province{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    public static List getallProvinces(){
        List provinces=new ArrayList();
        Province province1=new Province();
        province1.setId(1);province1.setName("北京");
        Province province2=new Province();
        province2.setId(2);province2.setName("上海");
        Province province3=new Province();
        province3.setId(3);province3.setName("南阳");
        provinces.add(province1);
        provinces.add(province2);
        provinces.add(province3);
        return provinces;
    }
}
package pojo;

import com.fasterxml.jackson.annotation.JsonBackReference;

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

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

    public City() {
    }

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

    public Integer getId() {
        return id;
    }
    @JsonBackReference
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    @JsonBackReference
    public void setName(String name) {
        this.name = name;
    }

    public List getCitites(int id){
        List cities=new ArrayList();
        if (id==1)
        {
            cities.add(new City(1,"朝阳区"));
            cities.add(new City(2,"东城区"));
            cities.add(new City(3,"西城区"));
            cities.add(new City(4,"海淀区"));
            cities.add(new City(5,"大兴区"));
        }
        else if (id==2){
           cities.add(new City(1,"浦东新区"));
           cities.add(new City(2,"虹口区"));
           cities.add(new City(3,"闵行区"));
           cities.add(new City(4,"青浦区"));
        }
        else if (id==3){
            cities.add(new City(1,"宛城区"));
        }
        return cities;
    }
}

 

pom.xml  jar包依赖


    
        junit
        junit
        4.11
        test
    
    

    
        net.sf.json-lib
        json-lib
        2.4
        jdk15
    
    
        commons-beanutils
        commons-beanutils
        1.7.0
    
    
        commons-collections
        commons-collections
        3.1
    
    
        commons-lang
        commons-lang
        2.5
    
    
        net.sf.ezmorph
        ezmorph
        1.0.3
    


    
        com.fasterxml.jackson.core
        jackson-core
        2.7.4
    
    
        com.fasterxml.jackson.core
        jackson-annotations
        2.7.4
    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.7.4
    
    
    
        org.codehaus.jackson
        jackson-core-asl
        1.9.13
    

    
        org.codehaus.jackson
        jackson-mapper-asl
        1.9.13
    

你可能感兴趣的:(Ajax,JSON)