基于Seam框架实现省、市、区县的联动选择

[原创于:http://happydev.iteye.com]

 

要实现省、市、区县的联动选择有两种方案:

一种是将省市区县的所有数据给到客户端由浏览器用js来控制选择和显示,但数据量太大,会导致页面太大,所以这种方案不可取;

二是用Ajax来实现联动选择,本文给出的就是在Seam框架下的基于ajax省、市、区县的联动选择实现。

 

一、准备好省、市、区县的所有数据,并提供相关访问组件

 

省、市、区县的所有数据见附件。

省、市、区县的访问组件类:

 

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("locationUtil")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class LocationUtil {
	private ArrayList provinces = new ArrayList();
	private Map> map_cities = new HashMap>();
	private Map> map_districts = new HashMap>();
	private Map map_pid = new HashMap();
	private Map map_cid = new HashMap();
	
	/**
	 * 取得所有省份数据
	 * @return
	 */
	public ArrayList getProvinces(){
		return provinces;
	}
	/**
	 * 根据省取得所有市数据
	 * @param province
	 * @return
	 */
	public ArrayList getCities(String province){
		return map_cities.get(province);
	}
	/**
	 * 根据市取得所有区县数据
	 * @param city
	 * @return
	 */
	public ArrayList getDistricts(String city){
		return map_districts.get(city);
	}
	public Collection getAllCities(){
		return map_cid.values();
	}
	
	@Create
	public void init(){
		SAXReader reader = new SAXReader(); 
		try {
			Document docDistricts = reader.read(this.getClass().getResourceAsStream("Districts.xml"));
			Document docCities = reader.read(this.getClass().getResourceAsStream("Cities.xml"));
			Document docProvinces = reader.read(this.getClass().getResourceAsStream("Provinces.xml"));
			for (Iterator i = docProvinces.getRootElement().elementIterator("Province"); i.hasNext();) {
				Element e = i.next(); 
				provinces.add(e.getText());
				map_pid.put(e.attributeValue("ID"), e.getText());
			}
			for (Iterator i = docCities.getRootElement().elementIterator("City"); i.hasNext();) {
				Element e = i.next(); 
				map_cid.put(e.attributeValue("ID"), e.getText());
				String pName = map_pid.get(e.attributeValue("PID"));
				ArrayList list = map_cities.get(pName);
				if (list == null){
					list = new ArrayList();
					map_cities.put(pName, list);
				}
				list.add(e.getText());
			}
			for (Iterator i = docDistricts.getRootElement().elementIterator("District"); i.hasNext();) {
				Element e = i.next(); 
				String cName = map_cid.get(e.attributeValue("CID"));
				ArrayList list = map_districts.get(cName);
				if (list == null){
					list = new ArrayList();
					map_districts.put(cName, list);
				}
				list.add(e.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	
}

 

 

二、业务层代码

 

@Name("entProfileHome")
public class EntProfileHome extends EntityHome {

	
	/**
	 * 定义一个临时的数据实体,以免在省、市、区县联动选择时对实际数据造成干扰
	 */
	private EntProfile temp;
	
	/**
	 * 省份被选择修改后的ajax事件动作,清空已关联的市和县区数据
	 */
	public void changeProvince(){
		temp.setCity(null);
		temp.setDistrict(null);
	}
	/**
	 * 市被选择修改后的ajax事件动作,清空已关联的县区数据
	 */
	public void changeCity(){
		temp.setDistrict(null);
	}
	
	public void startEdit(){
		temp = new EntProfile();
......
		temp.setProvince(getInstance().getProvince());
		temp.setCity(getInstance().getCity());
		temp.setDistrict(getInstance().getDistrict());
......
		
	}
	
	public void updateEdit(){
......
		getInstance().setProvince(temp.getProvince());
		getInstance().setCity(temp.getCity());
		getInstance().setDistrict(temp.getDistrict());
......
		
		update();
		temp = null;
	}
	
	@Override
	@Begin
	public void create() {
		super.create();
	}

	public EntProfile getTemp() {
		return temp;
	}
	public void setTemp(EntProfile temp) {
		this.temp = temp;
	}
	

}

 

 

 

三、选择页面

 

			
				选择地点
				
					
					
				
				
					
					
				
				
					
				
			

 

 

 

 

 

你可能感兴趣的:(Seam,框架,JBoss,Ajax,UI)