使用JQuery实现四级省市区县联动效果
本例使用servlet进行后台数据处理,故一定要先将web项目的相关配置配置完成
以下是整个案例的代码区,使用持久层–>控制层—>视图层进行实现
先建立省市区县的四个实体类
package entity;
public class Province {
private Integer id;
private String name;
private Integer level;
private Integer upid;
public Province() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return “Province [id=” + id + “, name=” + name + “, level=” + level + “, upid=” + upid + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((level == null) ? 0 : level.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((upid == null) ? 0 : upid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Province other = (Province) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (upid == null) {
if (other.upid != null)
return false;
} else if (!upid.equals(other.upid))
return false;
return true;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getUpid() {
return upid;
}
public void setUpid(Integer upid) {
this.upid = upid;
}
}
package entity;
public class City {
private Integer id;
private String name;
private Integer level;
private Integer upid;
public City() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return “Province [id=” + id + “, name=” + name + “, level=” + level + “, upid=” + upid + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((level == null) ? 0 : level.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((upid == null) ? 0 : upid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
City other = (City) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (upid == null) {
if (other.upid != null)
return false;
} else if (!upid.equals(other.upid))
return false;
return true;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getUpid() {
return upid;
}
public void setUpid(Integer upid) {
this.upid = upid;
}
}
package entity;
public class County {
private Integer id;
private String name;
private Integer level;
private Integer upid;
public County() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return “Province [id=” + id + “, name=” + name + “, level=” + level + “, upid=” + upid + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((level == null) ? 0 : level.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((upid == null) ? 0 : upid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
County other = (County) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (upid == null) {
if (other.upid != null)
return false;
} else if (!upid.equals(other.upid))
return false;
return true;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getUpid() {
return upid;
}
public void setUpid(Integer upid) {
this.upid = upid;
}
}
package entity;
public class Street {
private Integer id;
private String name;
private Integer level;
private Integer upid;
public Street() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return “Province [id=” + id + “, name=” + name + “, level=” + level + “, upid=” + upid + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((level == null) ? 0 : level.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((upid == null) ? 0 : upid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Street other = (Street) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (upid == null) {
if (other.upid != null)
return false;
} else if (!upid.equals(other.upid))
return false;
return true;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getUpid() {
return upid;
}
public void setUpid(Integer upid) {
this.upid = upid;
}
}
在进行数据查询之前需要先用jdbc进行数据库连接,注:需要创建配置文件
package utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.mail.HtmlEmail;
public class DBUtils {
private static BasicDataSource ds;
static{
ds=new BasicDataSource();
Properties p=new Properties();
//需要先创建inner.properties文件存放数据连接的数据即className 、url、username、pwd
InputStream fis=DBUtils.class.getClassLoader().
getResourceAsStream(“config”+File.separator+“inner.properties”);
try {
p.load(fis);
ds.setDriverClassName(p.getProperty(“className”));
ds.setUrl(p.getProperty(“url”));
ds.setUsername(p.getProperty(“username”));
ds.setPassword(p.getProperty(“pwd”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static BasicDataSource getBasicDataSource(){
return ds;
}
}
再建立一个类对数据库进行查询---->持久层
package dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import entity.City;
import entity.County;
import entity.Province;
import entity.Street;
import utils.DBUtils;
public class CitysDao {
private QueryRunner qr=new QueryRunner(DBUtils.getBasicDataSource());
//查找所有的省
public List findAllProvince(){
String sql=“select * from os_district where upid=0”;
try {
return qr.query(sql,new BeanListHandler(Province.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//根据省id查找市
public List<City> findByProvinceId(Integer id){
String sql="select * from os_district where upid=?";
try {
return qr.query(sql,new BeanListHandler<City>(City.class),id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//根据市id查找县
public List<County> findByCityId(Integer id){
String sql="select * from os_district where upid=?";
try {
return qr.query(sql,new BeanListHandler<County>(County.class),id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//根据县id查找街道
public List<Street> findByCountyId(Integer id){
String sql="select * from os_district where upid=?";
try {
return qr.query(sql,new BeanListHandler<Street>(Street.class),id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
创建控制器---->servlet进行前后数据连接—>需要引入servlet-api.jar包
package web;
import java.io.IOException;
import java.io.PrintWriter;
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 com.fasterxml.jackson.databind.ObjectMapper;
import dao.CitysDao;
import entity.City;
import entity.County;
import entity.Province;
import entity.Street;
@WebServlet("*.action")
public class ServletAction extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out=response.getWriter();
ObjectMapper om=new ObjectMapper();
String uri=request.getRequestURI();
String path=uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
if("/provinces".equals(path)){
List list=new CitysDao().findAllProvince();
String json=om.writeValueAsString(list);
out.println(json);
}
if("/citys".equals(path)){
Integer provinceId=Integer.parseInt(request.getParameter(“provinceId”));
List list=new CitysDao().findByProvinceId(provinceId);
String json=om.writeValueAsString(list);
out.println(json);
}
if("/countys".equals(path)){
Integer cityId=Integer.parseInt(request.getParameter(“cityId”));
List list=new CitysDao().findByCityId(cityId);
String json=om.writeValueAsString(list);
out.println(json);
}
if("/streets".equals(path)){
Integer countyId=Integer.parseInt(request.getParameter("countyId"));
List<Street> list=new CitysDao().findByCountyId(countyId);
String json=om.writeValueAsString(list);
out.println(json);
}
}
}
html代码