DWR 制作 ajax 国家地区级联菜单
java 文件内容:
js 文件内容
dwr.xml 配置文件
示例jsp文件
- /**
- * 主要实现地址的查询
- */
- 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;
- }
- }
/**
* 主要实现地址的查询
*/
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 文件内容
- <!--
- /*
- 作者: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);
- }
- //-->
<!--
/*
作者: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 配置文件
- <!-- 地址信息配置 -->
- <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"/>
<!-- 地址信息配置 -->
<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文件
- <%
- String basePath = request.getContextPath();
- %>
- <html>
- <head>
- <script type='text/javascript'
- src='<%=basePath%>/dwr/interface/Address.js'></script>
- <script type='text/javascript' src='<%=basePath%>/dwr/engine.js'></script>
- <script type='text/javascript' src='<%=basePath%>/dwr/util.js'></script>
- <script type="text/javascript" src="<%=basePath%>/js/day.js"></script>
- <script type='text/javascript' src='<%=basePath%>/js/address.js'></script>
- </head>
- <body>
- 国家:
- <select name="companyuser.countryId" id="selCountry"
- οnchange="getStates()" >
- </select>
- 这段代码是初始化:如果没有初始值,可只执行 initAddress()方法
- <script type="text/javascript">
- defaultCountry = 1; //初始值;
- defaultState = 1; //初始值;
- defaultCity = 1; //初始值;默认是 -1
- initAddress();
- </script>
- <br/> 省:
- <select name="companyuser.stateId" id="selState" οnchange="getCities()">
- </select>
- <br/>市:
- <select name="companyuser.cityId" id="selCity">
- </select>
- </body>