1.struts.xml
2.Bean.java
package cn.itcast.javaee.js.provincecityarea;/** * 实体,封装省份和城市 * @author AdminTC */public class Bean { private String province;//省份 private String city;//城市 public Bean(){} public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; }}
3. Action
package cn.itcast.javaee.js.provincecityarea;import java.util.List;import com.opensymphony.xwork2.ActionSupport;/** * 后台控制器*/public class ProvinceCityAreaAction extends ActionSupport{ //业务层 private ProvinceCityAreaService provinceCityAreaService = new ProvinceCityAreaService(); //Bean是实体,封装省份和城市 private Bean bean; public Bean getBean() { return bean; } public void setBean(Bean bean) { this.bean = bean; } /** * 根据省份查询城市 */ public String findCityByProvince() throws Exception{ cityList = provinceCityAreaService.findCityByProvince(bean.getProvince()); //将List集合转成JSON文本 return SUCCESS; } /** * 根据城市查询区域 */ public String findAreaByCity() throws Exception{ areaList = provinceCityAreaService.findAreaByCity(bean.getCity()); //将List集合转成JSON文本 return SUCCESS; } private ListcityList;//需要转成JSON的集合,且为其设置值 private List areaList;//需要转成JSON的集合,且为其设置值 public List getCityList() { //插件会调用getXxx()方法来获取需要转成JSON的集合 return cityList; } public List getAreaList() { return areaList; } }
4.Ajax异步请求
<%@ page language="java" pageEncoding="UTF-8"%>省份-城市-区域三级联动