一、 Struts2提供了级联下拉选择框doubleselect标签,这对简化诸如省份/城市这样的典型需求的操作是大有好处的.
如: “省份->城市”的级联菜单。
(1)省份类(封装setXXX and getXXX)
package com.hlx.entity; public class Province { private int pid; private String pname; }(2)城市类(封装setXXX and getXXX)
public class City { private int cid; private String cname; }(3)Action类
public class ProvinceCity extends ActionSupport { // map集合,key映射省份,value映射该省份的城市 private Map<Province, List<City>> map; public Map<Province, List<City>> getMap() { return map; } public void setMap(Map<Province, List<City>> map) { this.map = map; } @Override public String execute() throws Exception { // 江苏省 Province pro1 = new Province(1, "江苏省"); // 所在的城市 List<City> citys = new ArrayList<City>(); citys.add(new City(11, "南京")); citys.add(new City(22, "常州")); citys.add(new City(33, "苏州")); // 浙江省 Province pro2 = new Province(2, "浙江省"); // 所在的城市 List<City> citys1 = new ArrayList<City>(); citys1.add(new City(10, "杭州")); citys1.add(new City(20, "宁波")); citys1.add(new City(30, "温州")); //添加到集合中 map = new HashMap<Province, List<City>>(); map.put(pro1, citys); map.put(pro2, citys1); return SUCCESS; } }
<style type="text/css"> .doubleselect br{ display:none; } </style> </head> <body> <div class="doubleselect"> <s:form action="doubleSelectPost" name="form1"> <s:doubleselect formName="form1" label="省份" name="pid" list="map.keySet()" listKey="pid" listValue="pname" doubleList="map[top]" doubleName="id" doubleListKey="cid" doubleListValue="cname" > </s:doubleselect> </s:form> </div> </body> </html>
属性说明:
效果如下: