jquery+struts2+json省市县三级联动

前台页面


	
	
	


struts.xml

 
	
	 
	
		
action类

@Controller
public class ProvinceAction extends ActionSupport{
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private CityService cityService;
	@Autowired
	private TownService townService;

	private Town town;
	private List townList;

	public Town getTown() {
		return town;
	}
	public void setTown(Town town) {
		this.town = town;
	}
	public List getTownList() {
		return townList;
	}
	public void setTownList(List townList) {
		this.townList = townList;
	}
	private City city;
	private List cityList;

	public City getCity() {
		return city;
	}
	public void setCity(City city) {
		this.city = city;
	}
	public List getCityList() {
		return cityList;
	}
	public void setCityList(List cityList) {
		this.cityList = cityList;
	}
	private Province province;
	private List provinceList;
	public Province getProvince() {
		return province;
	}
	public void setProvince(Province province) {
		this.province = province;
	}
	public List getProvinceList() {
		return provinceList;
	}
	public void setProvinceList(List provinceList) {
		this.provinceList = provinceList;
	}
	private int province_id;
	private int city_id;

	public int getCity_id() {
		return city_id;
	}
	public void setCity_id(int city_id) {
		this.city_id = city_id;
	}
	public int getProvince_id() {
		return province_id;
	}
	public void setProvince_id(int province_id) {
		this.province_id = province_id;
	}
	public String ajaxProvince(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=UTF-8");     
		response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码    
		PrintWriter writer=null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		provinceList=provinceService.findAllProvince();
		JSONArray jsonArray = JSONArray.fromObject(provinceList); 
		writer.print(jsonArray.toString());
		return null;
	}
	public String ajaxCity(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=UTF-8");     
		response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码    
		PrintWriter writer=null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		provinceList=provinceService.findAllProvince();
		cityList=cityService.findAllCity(province_id);
		JSONArray jsonArray = JSONArray.fromObject(cityList); 
		writer.print(jsonArray.toString());
		return null;
	}
	public String ajaxTown(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=UTF-8");     
		response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码    
		PrintWriter writer=null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		townList=townService.findAllTown(city_id);
		JSONArray jsonArray = JSONArray.fromObject(townList); 
		writer.print(jsonArray.toString());

		return null;
	}

}
--the end--


你可能感兴趣的:(javaweb)