在添加城市页面,其中有下拉菜单,下拉菜单中要加载所有的省:
public ActionForward preadd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub List pro=ps.browseProvince(); Vector provinces=new Vector(); Iterator it=pro.iterator(); while(it.hasNext()){ Province p=(Province) it.next(); String s=String.valueOf(p.getProvinceid()); provinces.add(new LabelValueBean (p.getProvincename(),s)); } request.setAttribute("provinces",provinces); return mapping.findForward("addCity"); }
在修改城市页面,其中同样有省的下拉菜单,需要加载所有的省的信息,同时,还要从后台向前台传数据,在页面上设置默认值:
public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String id=request.getParameter("id"); City city=cs.loadCity(Integer.parseInt(id)); form=new CityForm(); //向前台页面设置默认值 ((CityForm) form).setcityid(city.getCityid()); ((CityForm) form).setcityCode(city.getCitycode()); ((CityForm) form).setcityName(city.getCityname()); //form.setProvinceid(city.getProvince().getProvinceid()); request.setAttribute("cityForm", form); List pro=ps.browseProvince(); Vector provinces=new Vector(); Iterator it=pro.iterator(); //加载所有的省的信息 while(it.hasNext()){ Province p=(Province) it.next(); String s=String.valueOf(p.getProvinceid()); provinces.add(new LabelValueBean(p.getProvincename(),s)); } request.setAttribute("provinces",provinces); request.setAttribute("theprovince",city.getProvince().getProvinceid()); return mapping.findForward("modifyCity"); }
在 一个省的某个城市中添加某个人,首先此页面需要加载省的信息,以及市的信息,而且需要实现的是选择省的同时,相应的市会在下拉列表中显示,这就需要容器来实现,容器的嵌套使用,以及尤其要注意的就是容器的初始化:
public ActionForward preadd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub //如何获取市的信息?????????? //利用的是容器的嵌套 List pp=new ArrayList(); pp=ps.browseProvince(); Vector provinces=new Vector(); Iterator it=pp.iterator(); List cityes=new ArrayList(); Map mapPC=null; Map map=null; List cityesPC=null; //把每个省和城市放在同一个map中,可以实现的是查询省时城市也会自动显示出来 while(it.hasNext()){ Province province=(Province) it.next(); String provinceId=String.valueOf(province.getProvinceid()); provinces.add(new LabelValueBean(province.getProvincename(),provinceId)); Iterator iter=province.getCityes().iterator();//遍历一个省份中所有的城市 mapPC=new HashMap(); cityesPC=new ArrayList(); while(iter.hasNext()){ City city=(City) iter.next(); map=new HashMap(); String id=String.valueOf(city.getCityid()); map.put("value",id ); map.put("label", city.getCityname()); cityesPC.add(map);//获取每个省相对应的城市
} mapPC.put("province", cityesPC); cityes.add(mapPC); } request.setAttribute("provinces",provinces);//给页面发送省的信息
request.setAttribute("citys",cityes);
return new ActionForward("/addClient.jsp"); }