如果此处没有帮助到你,你可以进入这个链接,相信你可以找到你的答案:
http://blog.csdn.net/xuanzhangran/article/details/55051237
将页面上的省市区保存到表中:
前台代码快:
<script src="<%=basePath%>js/business/suRegionAddress.js" type="text/javascript"></script>//引入js
<script type="text/javascript">
window.onload=function(){
//所在区域联动列表
setup();//加载的时候就执行里面的代码:如下的js代码有显示
}
</script>
<div class="row">
<span>收货地址:</span>
<select style="WIDTH: 80px" id="province" name="provinceName" >
<option>选择省份</option>
<c:forEach items="${provinceList}" var="province">
<c:if test="${province.name == businessFpAddress.provinceName }">
<option value="${province.id}" selected="selected">${province.name}</option>
</c:if>
<c:if test="${province.name != businessFpAddress.provinceName}">
<option value="${province.id}">${province.name}</option>
</c:if>
</c:forEach>
</select>
<select style="WIDTH: 80px" id="city" name="cityName" >
<c:if test="${not empty businessFpAddress.cityName}">
<option value="${businessFpAddress.cityName}">${businessFpAddress.cityName}</option>
</c:if>
<c:if test="${empty businessFpAddress.cityName}">
<option value="">选择城市</option>
</c:if>
</select>
<select style="WIDTH: 90px" id="area" name="districtName" >
<c:if test="${not empty businessFpAddress.districtName}">
<option value="${businessFpAddress.districtName}">${businessFpAddress.districtName}</option>
</c:if>
<c:if test="${empty businessFpAddress.districtName}">
<option value="">选择区域</option>
</c:if>
</select>
</div>
is代码块:
/***
* 省市区三级联动:
* 三个下拉列表id分别是:province、city、area
* @returns
*/
function setup() {
//获取城市
$("#province").click(function () {//当点击省份的下拉选的时候,选中之后,城市进行联动
$.ajax({
url: "/BusinessManagament/getCityByProvinceId.html?id="+$("#province").val(),
type: "POST",
dataType:'json',
success:function(data){
$("#city").empty(); //清空下拉列表
$.each(data,function(i,item){
$("#city").append(" <option value='" + item.id + "'>" + item.name + "</option>");
});
}
});
});
//获取区县
$("#city").click(function () {//当选择城市的下拉选的时候,区域进行联动
$.ajax({
url: "/BusinessManagament/getAreaByCityId.html?id="+$("#city").val(),
type: "POST",
dataType:'json',
success:function(data){
$("#area").empty(); //清空下拉列表
$.each(data,function(i,item){
$("#area").append(" <option value='" + item.id + "'>" + item.name + "</option>");
});
}
});
});
}
后台代码块:
@Controller
@RequestMapping("/BusinessInfo")
public class BusinessFpAddressAction {
@Resource
SuRegionServer regionServer;//省市区列表的server
@Resource
BusinessFpAddressService businessFpAddressService;//本页面的server
@RequestMapping("/saveBusinessFpAddress.html")
public String saveBusinessFpAddress(Model model, HttpServletRequest request,
@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
@ModelAttribute("businessFpAddress") SuBusinessFpAddress businessFpAddress){
SuBusiness business = WebUtil.getLoginUser(request);//登录人信息
Long businessId = business.getId();
//省市区一般以数字的形式传入进来的,判断是否为数字
if(isNumeric(businessFpAddress.getProvinceName())){
//通过对应的省份的数字代号查出对应点的省份的名称
SuRegion region1 = regionServer.selectByPrimaryKey(Long.parseLong(businessFpAddress.getProvinceName()));
if(region1 != null){
businessFpAddress.setProvinceName(region1.getName());
}
}
//通过对应的城市的数字代号查出对应点的城市的名称
if(isNumeric(businessFpAddress.getCityName())){
SuRegion region2 = regionServer.selectByPrimaryKey(Long.parseLong(businessFpAddress.getCityName()));
if(region2 != null){
businessFpAddress.setCityName(region2.getName());
}
}
//通过对应的区的数字代号查出对应点的区的名称
if(isNumeric(businessFpAddress.getDistrictName())){
SuRegion region3 = regionServer.selectByPrimaryKey(Long.parseLong(businessFpAddress.getDistrictName()));
if(region3 != null){
businessFpAddress.setDistrictName(region3.getName());
}
}
//保存省市区地址
businessFpAddress.setBusinessId(businessId);
businessFpAddress.setCreateDateTime(new Date());
businessFpAddress.setStatus(Byte.parseByte("1"));
Integer count = businessFpAddressService.insertSelective(businessFpAddress);
if(count > 0){//如果保存成功,重定向页面
flag = "1";
return "redirect:/BusinessManagament/businessFpAddress.html?flag="+flag;
}
//此处很重要,就是将省市区的联动列表带到前台区,用到了引入的js里的代码
model.addAttribute("provinceList", regionServer.getAllProvices());
return "redirect:/BusinessManagament/businessFpAddress.html?flag="+flag;
}
//判断是否为数字
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
}
/**
* 根据省份id获取城市数据后直接使用@ResponseBody装成json数据
*
* @param id
* 省份ID
* @param response
* @return
* @return
*/
@RequestMapping("/getCityByProvinceId.html")
@ResponseBody
public String getCityByProvinceId(
@RequestParam(value = "id", required = false) Long id) {
List<SuRegion> cityList = suRegionServer.getAllCitys(id);//根据省份的id查城市
return JSON.toJSONString(cityList);
}
/**
* 根据城市id获取区域数据后直接使用@ResponseBody装成json数据
*
* @param id
* @return
*/
@RequestMapping("/getAreaByCityId.html")
@ResponseBody
public String getAreaByCityId(
@RequestParam(value = "id", required = false) Long id) {
List<SuRegion> areaList = suRegionServer.getAllAreas(id);//根据城市的id查区域
return JSON.toJSONString(areaList);
}
mapping 的sql语句:其中grade=2代表城市,grade=3代表区域。
<select id="getAllCitys" resultMap="BaseResultMap" parameterType="com.idorabox.entity.SuRegion">
select
<include refid="Base_Column_List" />
from tbl_su_region
where grade = 2
<if test="id != null">
and parent = #{id,jdbcType=BIGINT}
</if>
</select>
<!--获取 区县列表 -->
<select id="getAllAreas" resultMap="BaseResultMap" parameterType="com.idorabox.entity.SuRegion">
select
<include refid="Base_Column_List" />
from tbl_su_region
where grade = 3
<if test="id != null">
and parent = #{id,jdbcType=BIGINT}
</if>
</select>
注意事项:
(1):加载的时候执行三级联动的setup()方法;
(2):一般省市区都是数字的形式,所以需要将获取传过来的数字 在表中查出名称保存到数据库中;
(3):点击省份的时候,联动城市,点击城市的时候联动区域。