DWR 制作 ajax 国家地区级联菜单

 

DWR 制作 ajax 国家地区级联菜单

java 文件内容:
Java代码    
  1.   
  2. /**  
  3.  * 主要实现地址的查询  
  4.  */  
  5. public class Address {   
  6.     private AddressService addressService;   
  7.   
  8.     /**  
  9.      * 查询国家 @ Write time: 2007-6-26 下午02:39:07 <Br>  
  10.      * @return List<State><br>  
  11.      */  
  12.     public List getCountries() {   
  13.         List countries = null;   
  14.         countries = addressService.getAllCountry();   
  15.         return countries;   
  16.     }   
  17.   
  18.     /**  
  19.      * 根据国家编号获取省 @ Write time: 2007-6-26 下午02:39:07 <Br>  
  20.      * @param id  
  21.      *            国家编号  
  22.      * @return List<State><br>  
  23.      */  
  24.     public List getStates(String id) {   
  25.         List states = null;   
  26.         if (!isNumber(id)) {   
  27.             System.out.println("Country id is not a number!");   
  28.             states = new LinkedList();   
  29.             return states;   
  30.         }   
  31.   
  32.         states = addressService.getStateByCountryId(Long.parseLong(id));   
  33.         return states;   
  34.     }   
  35.   
  36.     /**  
  37.      * 根据省编号获取市 @ Write time: 2007-6-26 下午02:39:07 <Br>  
  38.      * @param id  
  39.      *            省编号  
  40.      * @return List<City><br>  
  41.      */  
  42.     public List getCities(String id) {   
  43.         List cities = null;   
  44.         if (!isNumber(id)) {   
  45.             System.out.println("State id is not a number");   
  46.             cities = new LinkedList();   
  47.             return cities;   
  48.         }   
  49.   
  50.         cities = addressService.getCityByStateId(Long.parseLong(id));   
  51.   
  52.         return cities;   
  53.     }   
  54.     /**  
  55.      * 根据State 市编号查找 对应 的省  
  56.      */  
  57.     public Object getStateByCityId(String id) {   
  58.   
  59.         if (!isNumber(id)) {   
  60.             System.out.println("City id is not a number");   
  61.             return null;   
  62.         }   
  63.         return addressService.getStateByCityId(Long.parseLong(id));   
  64.     }   
  65.   
  66.     /**  
  67.      * 根据 省编号 查找对应的国家  
  68.      * @author: Sivyer <br>  
  69.      * @write time: 2007-10-12 下午03:14:47  <Br>  
  70.      * @param id  
  71.      * @return<br>  
  72.      */  
  73.     public Object getCountryByStateId(String id) {   
  74.   
  75.         if (!isNumber(id)) {   
  76.             System.out.println("State id is not a number");   
  77.             return null;   
  78.         }   
  79.   
  80.         return addressService.getCountryByStateId(Long.parseLong(id));   
  81.     }   
  82.   
  83.     private boolean isNumber(String s) {   
  84.         Pattern pattern = Pattern.compile("[0-9]");   
  85.         Matcher isNum = pattern.matcher(s);   
  86.         if (isNum.matches()) {   
  87.             return true;   
  88.         } else  
  89.             return false;   
  90.     }   
  91.   
  92.     public AddressService getAddressService() {   
  93.         return addressService;   
  94.     }   
  95.   
  96.     public void setAddressService(AddressService addressService) {   
  97.         this.addressService = addressService;   
  98.     }   
  99. }  
/**
 * 主要实现地址的查询
 */
public class Address {
	private AddressService addressService;

	/**
	 * 查询国家 @ Write time: 2007-6-26 下午02:39:07 <Br>
	 * @return List<State><br>
	 */
	public List getCountries() {
		List countries = null;
		countries = addressService.getAllCountry();
		return countries;
	}

	/**
	 * 根据国家编号获取省 @ Write time: 2007-6-26 下午02:39:07 <Br>
	 * @param id
	 *            国家编号
	 * @return List<State><br>
	 */
	public List getStates(String id) {
		List states = null;
		if (!isNumber(id)) {
			System.out.println("Country id is not a number!");
			states = new LinkedList();
			return states;
		}

		states = addressService.getStateByCountryId(Long.parseLong(id));
		return states;
	}

	/**
	 * 根据省编号获取市 @ Write time: 2007-6-26 下午02:39:07 <Br>
	 * @param id
	 *            省编号
	 * @return List<City><br>
	 */
	public List getCities(String id) {
		List cities = null;
		if (!isNumber(id)) {
			System.out.println("State id is not a number");
			cities = new LinkedList();
			return cities;
		}

		cities = addressService.getCityByStateId(Long.parseLong(id));

		return cities;
	}
	/**
	 * 根据State 市编号查找 对应 的省
	 */
	public Object getStateByCityId(String id) {

		if (!isNumber(id)) {
			System.out.println("City id is not a number");
			return null;
		}
		return addressService.getStateByCityId(Long.parseLong(id));
	}

	/**
	 * 根据 省编号 查找对应的国家
	 * @author: Sivyer <br>
	 * @write time: 2007-10-12 下午03:14:47  <Br>
	 * @param id
	 * @return<br>
	 */
	public Object getCountryByStateId(String id) {

		if (!isNumber(id)) {
			System.out.println("State id is not a number");
			return null;
		}

		return addressService.getCountryByStateId(Long.parseLong(id));
	}

	private boolean isNumber(String s) {
		Pattern pattern = Pattern.compile("[0-9]");
		Matcher isNum = pattern.matcher(s);
		if (isNum.matches()) {
			return true;
		} else
			return false;
	}

	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}
}



js 文件内容
Java代码 复制代码  收藏代码
  1.   
  2.   
  3. <!--   
  4. /*  
  5.     作者:Sivyer 2007-6  
  6.     功能:地址控件,初始化函数: initAddress()   
  7.     注: init()时,需要在页面里定义 country/state/city 的select  
  8.         initDiv(),只需要定义一个 Div ,name 设为 "addressDiv"  
  9.    
  10. */  
  11.   
  12.   
  13. var selCountry = "selCountry";   
  14. var selState = "selState";   
  15. var selCity = "selCity";   
  16.   
  17. var defaultCountry = -1;   
  18. var defaultState = -1;   
  19. var defaultCity = -1;   
  20.   
  21. var countryHead =   { '-1':'... 请选择国家...'};   
  22. var stateHead =     { '-1':'...请选择所在省...'};   
  23. var cityHead =      { '-1':'...请选择所在市...'};   
  24.   
  25. function setDefaultValue(_country,_state,_city){   
  26.     defaultCountry = _country;   
  27.     defaultState = _state;   
  28.     defaultCity = _city;   
  29.        
  30. }   
  31.   
  32. function initAddress() {   
  33.   
  34.     if(defaultCountry ==""){   
  35.         defaultCountry == -1;   
  36.     }   
  37.     if(defaultState == ""){   
  38.         defaultState == -1;   
  39.     }   
  40.     if(defaultCity == ""){   
  41.         defaultCity == -1;   
  42.     }   
  43.        
  44.     getCountries();   
  45. }   
  46.   
  47. /*  
  48. 返回国家列表  
  49. */  
  50. function getCountries() {   
  51. try{   
  52.        
  53.     Address.getCountries(function (countries) {   
  54.            
  55.         clearCountry();   
  56.            
  57.         DWRUtil.addOptions(selCountry , countries, "id""name");   
  58.         DWRUtil.setValue(selCountry,defaultCountry);   
  59.            
  60.         getStates();   
  61.     });   
  62.     }catch(e){   
  63.         alert("获取市信息时出错" + e);   
  64.     }   
  65. }   
  66.   
  67. function getStates() {   
  68.     try{   
  69.        
  70.     var countryId = getSelectId(selCountry);   
  71.   
  72.     if (countryId != -1) {   
  73.         Address.getStates(countryId, function (states) {   
  74.             clearState();   
  75.             DWRUtil.addOptions(selState, states, "id""name");   
  76.                
  77.             DWRUtil.setValue(selState,defaultState);   
  78.             defaultState = getSelectId(selState);   
  79.             getCities();   
  80.         });   
  81.     } else {   
  82.         clearState();   
  83.         clearCity();   
  84.         getCities();   
  85.     }   
  86.     }catch(e){   
  87.         alert("获取省信息时出错!" + e);   
  88.     }   
  89.        
  90. }   
  91. function getCities() {   
  92.     try{   
  93.         var stateId = getSelectId( selState );   
  94.         var countryId = getSelectId( selCountry );   
  95.            
  96.         if (stateId != -1 && countryId != -1) {   
  97.             Address.getCities(stateId, function (cities) {   
  98.                 clearCity();   
  99.                 DWRUtil.addOptions(selCity, cities, "id""name");   
  100.                    
  101.                 if(getSelectId(selState) != -1 && getSelectId(selCountry) != -1){   
  102.                     DWRUtil.setValue(selCity,defaultCity);   
  103.                     defaultCity = getSelectId(selCity);   
  104.                 }   
  105.             });   
  106.         }else if(stateId == -1 && countryId == -1 && defaultCity != -1){   
  107.             clearCity();   
  108.         }   
  109.            
  110.     }catch(e){   
  111.         alert("获取市信息时出错!" + e);   
  112.     }   
  113. }   
  114.   
  115. function clearCountry(){   
  116.     defaultCountry = -1;   
  117.     DWRUtil.removeAllOptions(selCountry);   
  118.     DWRUtil.addOptions(selCountry,countryHead);   
  119. }   
  120. function clearState(){   
  121.     defaultState = -1;   
  122.     DWRUtil.removeAllOptions(selState);   
  123.     DWRUtil.addOptions(selState,stateHead);   
  124. }   
  125. function clearCity(){   
  126.     defaultCity = -1;   
  127.     DWRUtil.removeAllOptions(selCity);   
  128.     DWRUtil.addOptions(selCity,cityHead);   
  129. }   
  130.   
  131. /*  
  132. *作者:Sivyer Email:pxysea@163.com  
  133. *功能:获取选择的地址编号  
  134. *参数:name 可值可以为 country,state,city  
  135. *返回:如果选择正确 对应编号,否则返回 -1;  
  136. */  
  137. function getSelectId( name ){   
  138.     var id;   
  139.     if(name == selCountry){   
  140.         id = DWRUtil.getValue(selCountry );   
  141.     }else if( name == selState ){   
  142.         id = DWRUtil.getValue(selState);   
  143.        
  144.     }else if( name == selCity ){   
  145.         id = DWRUtil.getValue(selCity);   
  146.     }   
  147.     if(isNaN(id)){   
  148.         return -1;   
  149.            
  150.     }else{   
  151.         return id;   
  152.     }   
  153. }   
  154.   
  155. function setDefaultValue(sel,value){   
  156.     DWRUtil.setValue(sel,value);   
  157. }   
  158. //-->  
<!--
/*
 	作者:Sivyer 2007-6
	功能:地址控件,初始化函数: initAddress() 
	注: init()时,需要在页面里定义 country/state/city 的select
		initDiv(),只需要定义一个 Div ,name 设为 "addressDiv"
 
*/


var selCountry = "selCountry";
var selState = "selState";
var selCity = "selCity";

var defaultCountry = -1;
var defaultState = -1;
var defaultCity = -1;

var countryHead = 	{ '-1':'... 请选择国家...'};
var stateHead = 	{ '-1':'...请选择所在省...'};
var cityHead = 		{ '-1':'...请选择所在市...'};

function setDefaultValue(_country,_state,_city){
	defaultCountry = _country;
	defaultState = _state;
	defaultCity = _city;
	
}

function initAddress() {

	if(defaultCountry ==""){
		defaultCountry == -1;
	}
	if(defaultState == ""){
		defaultState == -1;
	}
	if(defaultCity == ""){
		defaultCity == -1;
	}
	
	getCountries();
}

/*
返回国家列表
*/
function getCountries() {
try{
	
	Address.getCountries(function (countries) {
		
		clearCountry();
		
		DWRUtil.addOptions(selCountry , countries, "id", "name");
		DWRUtil.setValue(selCountry,defaultCountry);
		
		getStates();
	});
	}catch(e){
		alert("获取市信息时出错" + e);
	}
}

function getStates() {
	try{
	
	var countryId = getSelectId(selCountry);

	if (countryId != -1) {
		Address.getStates(countryId, function (states) {
			clearState();
			DWRUtil.addOptions(selState, states, "id", "name");
			
			DWRUtil.setValue(selState,defaultState);
			defaultState = getSelectId(selState);
			getCities();
		});
	} else {
		clearState();
		clearCity();
		getCities();
	}
	}catch(e){
		alert("获取省信息时出错!" + e);
	}
	
}
function getCities() {
	try{
		var stateId = getSelectId( selState );
		var countryId = getSelectId( selCountry );
		
		if (stateId != -1 && countryId != -1) {
			Address.getCities(stateId, function (cities) {
				clearCity();
				DWRUtil.addOptions(selCity, cities, "id", "name");
				
				if(getSelectId(selState) != -1 && getSelectId(selCountry) != -1){
					DWRUtil.setValue(selCity,defaultCity);
					defaultCity = getSelectId(selCity);
				}
			});
		}else if(stateId == -1 && countryId == -1 && defaultCity != -1){
			clearCity();
		}
		
	}catch(e){
		alert("获取市信息时出错!" + e);
	}
}

function clearCountry(){
	defaultCountry = -1;
	DWRUtil.removeAllOptions(selCountry);
	DWRUtil.addOptions(selCountry,countryHead);
}
function clearState(){
	defaultState = -1;
	DWRUtil.removeAllOptions(selState);
	DWRUtil.addOptions(selState,stateHead);
}
function clearCity(){
	defaultCity = -1;
	DWRUtil.removeAllOptions(selCity);
	DWRUtil.addOptions(selCity,cityHead);
}

/*
*作者:Sivyer Email:pxysea@163.com
*功能:获取选择的地址编号
*参数:name 可值可以为 country,state,city
*返回:如果选择正确 对应编号,否则返回 -1;
*/
function getSelectId( name ){
	var id;
	if(name == selCountry){
		id = DWRUtil.getValue(selCountry );
	}else if( name == selState ){
		id = DWRUtil.getValue(selState);
	
	}else if( name == selCity ){
		id = DWRUtil.getValue(selCity);
	}
	if(isNaN(id)){
		return -1;
		
	}else{
		return id;
	}
}

function setDefaultValue(sel,value){
	DWRUtil.setValue(sel,value);
}
//-->




dwr.xml 配置文件
Java代码 复制代码  收藏代码
  1. <!-- 地址信息配置 -->   
  2.          <create creator="spring" javascript="Address">   
  3.             <param name="beanName"  
  4.                 value="addressBean" />   
  5.         </create>   
  6.         <convert match="com.mp.job.hibernate.vo.Country" javascript="Country" converter="bean"/>   
  7.         <convert match="com.mp.job.hibernate.vo.City" javascript="City" converter="bean"/>   
  8.         <convert match="com.mp.job.hibernate.vo.State" javascript="State" converter="bean"/>  
<!-- 地址信息配置 -->
         <create creator="spring" javascript="Address">
			<param name="beanName"
				value="addressBean" />
		</create>
		<convert match="com.mp.job.hibernate.vo.Country" javascript="Country" converter="bean"/>
		<convert match="com.mp.job.hibernate.vo.City" javascript="City" converter="bean"/>
		<convert match="com.mp.job.hibernate.vo.State" javascript="State" converter="bean"/>


示例jsp文件
Java代码 复制代码  收藏代码
  1. <%   
  2. String basePath = request.getContextPath();   
  3. %>   
  4.   
  5. <html>   
  6.     <head>   
  7.         <script type='text/javascript'  
  8.             src='<%=basePath%>/dwr/interface/Address.js'></script>   
  9.         <script type='text/javascript' src='<%=basePath%>/dwr/engine.js'></script>   
  10.         <script type='text/javascript' src='<%=basePath%>/dwr/util.js'></script>   
  11.         <script type="text/javascript" src="<%=basePath%>/js/day.js"></script>   
  12.         <script type='text/javascript' src='<%=basePath%>/js/address.js'></script>   
  13.   
  14.  </head>   
  15. <body>   
  16.    国家:   
  17.    <select name="companyuser.countryId" id="selCountry"  
  18.     οnchange="getStates()"  >   
  19.    </select>   
  20.    这段代码是初始化:如果没有初始值,可只执行 initAddress()方法   
  21.    <script type="text/javascript">   
  22.      defaultCountry = 1//初始值;   
  23.      defaultState = 1//初始值;   
  24.      defaultCity =  1//初始值;默认是 -1   
  25.       initAddress();   
  26.    </script>   
  27.    <br/> 省:   
  28.    <select name="companyuser.stateId" id="selState" οnchange="getCities()">   
  29.    </select>   
  30.   <br/>市:   
  31.   <select name="companyuser.cityId" id="selCity">   
  32.   
  33.   </select>   
  34. </body> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值