jsp页面调用:
所在地区:<jsp:include flush="true" page="/common/city/selectCity.jsp" />
One :
City实体
public class City {
/**
* 标识 id
*/
private Integer id;
/**
* 城市名 cityname
*/
private String cityname;
/**
* 省份标识 parentid
*/
private Integer parentid;
/**
* 城市类型 citycategory
*/
private Integer citycategory;
/**
* 地区 areaid
*/
private Integer areaid;
private String orderby; // 排序
public String getOrderby()
{
if (orderby == null)
orderby = " id ASC";
return orderby;
}
public void setOrderby(String orderby) {
this.orderby = orderby;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public Integer getParentid() {
return parentid;
}
public void setParentid(Integer parentid) {
this.parentid = parentid;
}
public Integer getCitycategory() {
return citycategory;
}
public void setCitycategory(Integer citycategory) {
this.citycategory = citycategory;
}
public Integer getAreaid() {
return areaid;
}
public void setAreaid(Integer areaid) {
this.areaid = areaid;
}
}
Two:
用Serlvlet将City数据加入内存
public class CityServlet extends HttpServlet {
public void init() {
try {
CityBiz cityBiz = (CityBiz) ContextConf.getContext().getBean("cityBiz");
/**
* 把省加载到内存
*/
City city = new City();
city.setParentid(0);
List listProvince = cityBiz.search(city);
this.getServletContext().setAttribute("listProvince", listProvince);
/**
* 把市加载到内存
*/
for (int i = 0; i < listProvince.size(); i++) {
City cityProvince = (City) listProvince.get(i);
city.setParentid(cityProvince.getId());
List listCity = cityBiz.search(city);
this.getServletContext().setAttribute("listCity" + cityProvince.getId(), listCity);
//把区/县加载到内存
for(int j = 0 ; j < listCity.size() ; j++ ){
City countyCity = (City)listCity.get(j);
city.setParentid(countyCity.getId());
List listCounty = cityBiz.search(city);
this.getServletContext().setAttribute("listCity" + countyCity.getId(), listCounty);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("城市加载内存失败");
}
}
}
Three:
Action
public class CityAction extends BaseAction {
private CityBiz cityBiz;
private City item;
private int parentid; //父级城市
private List cityList;
/**
* 通过父类查询子类
* */
public String getCityListByParentId()
{
try
{
//声明所用的初始值
getResponse().setCharacterEncoding("GBK");
getResponse().setContentType("text/html; charset=GBK");
PrintWriter out = null;
out=getResponse().getWriter();
//查询数据库
if(getParentid()==0)
{
cityList=(List)getRequest().getSession().getServletContext().getAttribute("listProvince");
}
else
{
cityList=(List)getRequest().getSession().getServletContext().getAttribute("listCity"+getParentid());
}
String resOut="";
for(int i=0;i<cityList.size();i++)
{
item=(City)cityList.get(i);
resOut=resOut+"<option value='"+item.getId()+"'>"+item.getCityname()+"</option>";
}
//输出所选项
out.print(resOut);
out.close();
return null;
}
catch(Exception e)
{
e.printStackTrace();
return "error";
}
}
Four:
Spring、Struts 配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="cityDao" class="com.dao.impl.CityDaoImpl"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="cityBiz" class="com.biz.impl.CityBizImpl"> <property name="cityDao" ref="cityDao" /> </bean> <bean id="cityAction" class="com.action.CityAction"> <property name="cityBiz" ref="cityBiz" /> </bean> </beans>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="city" extends="struts-default" namespace="/city">
<!-- 根据父类id获取子类列表 -->
<action name="getCityListByParentId" class="com.health2.ssi.action.CityAction" method="getCityListByParentId">
<result name="success">/common/city/selectCity.jsp</result>
<result name="error">/common/city/selectCity.jsp</result>
</action>
</package>
</struts>
Five:
selectCity.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %> <%@ taglib prefix="s" uri="/struts-tags"%> <script type="text/javascript" src="/js/public/jquery.js"></script> <script type="text/javascript" src="/js/public/city.js"></script> <input type="hidden" id="provinceselectnameid" name="provinceselectname" value="0"/> <input type="hidden" id="cityselectnameid" name="cityselectname" value="0"/> <input type="hidden" id="provinceandcitynameid" name="provinceandcityname" value="0"/> <input type="hidden" id="parentselectidper" value="<s:property value="parentid"/>"/> <input type="hidden" id="cityselectidper" value="<s:property value="cityid"/>"/> <select name="provincename" id="provincenameid"> <option value="0" selected="selected">请选择省</option> <s:iterator value="#application['listProvince']"> <option value="<s:property value="id"/>"><s:property value="cityname"/></option> </s:iterator> </select>省 <select name="cityname" id="citynameid"> <option value="0" selected="selected">请选择市</option> </select>市
Six:
city.js
$(document).ready(function(){ //加载 if($("#parentselectidper").val()>0) { $("#provincenameid").attr("value",$("#parentselectidper").val());//为省选中值 if($("#parentselectidper").val()>0) { $.ajax({ type: "POST", url: "/city/getCityListByParentId.do", processData:false, data: "parentid="+$("#parentselectidper").val(), async: false, success: function(data){ $("#provinceselectnameid").val($("#provincenameid").val());//设置省 $("#provinceandcitynameid").val($("#provincenameid").val());//设置整体 $("#citynameid").html('<option value="0" selected="selected">请选择市</option>'+data); if($("#cityselectidper").val()>0) { $("#citynameid").attr("value",$("#cityselectidper").val());//为市选中值 } else { $("#citynameid").attr("value",0);//为市选中值 } } }); } } else { $("#provincenameid").attr("value",0);//为省选中值 } //选择省 $("#provincenameid").change(function(){ if($("#provincenameid").val()>0) { $.ajax({ type: "POST", url: "/city/getCityListByParentId.do", processData:false, data: "parentid="+$("#provincenameid").val(), async: false, success: function(data){ $("#provinceselectnameid").val($("#provincenameid").val());//设置省 $("#provinceandcitynameid").val($("#provincenameid").val());//设置整体 $("#citynameid").html('<option value="0" selected="selected">请选择市</option>'+data); } }); } }); //选择市 $("#citynameid").change(function(){ $("#cityselectnameid").val($("#citynameid").val());//设置市 $("#provinceandcitynameid").val($("#citynameid").val());//设置整体 }); });