当前位置: 代码迷 >> Web前端 >> struts2 doubleselect示范
  详细解决方案

struts2 doubleselect示范

热度:291   发布时间:2012-06-30 17:20:13.0
struts2 doubleselect示例

doubleselect算是Struts2中稍微复杂点的表单标签, 尤其官方示例比较简单, 没有太大的实用价值.

?

<s:doubleselect label="doubleselect test2" name="menu" list="#{'fruit':'Nice Fruits', 'other':'Other Dishes'}" doubleName="dishes" doubleList="top == 'fruit' ? {'apple', 'orange'} : {'monkey', 'chicken'}" />

?

从这个官方示例中可以看到基本用法, list开头的代表的是第一级下拉框的属性, doubleList开头的则是第二级下拉框. 与select标签的属性基本是对应的.

?

在实际应用中, 我们比较关注的就是list和doubleList这两个属性了, list是存放的第一级下拉框的数据集合, 比如List<FatherBean>, 而doubleList就可以用Map<String, List<SonBean>>来存储, 其中map的键名String与第一级下拉框相关联.

?

现在来展示一个我写的示例:

先写两个javaBean,一般情况下是跟数据库的表相关联的:

package com.student.bean;

public class Province {
int id;
String name;
//set和get方法

}

package com.student.bean;

public class City {
int id;
String name;
int provinceId;
//set和get方法
}

再来写Action,用来往web页面发送数据:

package com.student.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.student.bean.Province;
import com.student.bean.City;

public class DoubleSelectAction extends ActionSupport {
private List<Province> provinceList;
private Map<Integer, List<City>> cityMap;

public List<Province> getProvinceList() {
return provinceList;
}

public void setProvinceList(List<Province> provinceList) {
this.provinceList = provinceList;
}

public Map<Integer, List<City>> getCityMap() {
return cityMap;
}

public void setCityMap(Map<Integer, List<City>> cityMap) {
this.cityMap = cityMap;
}

public String execute() throws Exception {
provinceList= new ArrayList<Province>();

Province province;
province = new Province();
province.setId(1);
province.setName("山西省");
provinceList.add(province);

province = new Province();
province.setId(2);
province.setName("陕西省");
provinceList.add(province);

province = new Province();
province.setId(3);
province.setName("河南省");
provinceList.add(province);


cityMap = new HashMap<Integer, List<City>>();

List<City> cityList = new ArrayList<City>();
City city;
city = new City();
city.setId(1);
city.setName("太原市");
city.setProvinceId(1);
cityList.add(city);

city= new City();
city.setId(2);
city.setName("大同市");
city.setProvinceId(1);
cityList.add(city);

city= new City();
city.setId(3);
city.setName("长治市");
city.setProvinceId(1);
cityList.add(city);

cityMap.put(1, cityList);

cityList = new ArrayList<City>();
city= new City();
city.setId(4);
city.setName("西安市");
city.setProvinceId(2);
cityList.add(city);

city= new City();
city.setId(5);
city.setName("汉中市");
city.setProvinceId(2);
cityList.add(city);

city= new City();
city.setId(6);
city.setName("咸阳市");
city.setProvinceId(2);
cityList.add(city);

cityMap.put(2, cityList);

cityList = new ArrayList<City>();
city= new City();
city.setId(7);
city.setName("郑州市");
city.setProvinceId(3);
cityList.add(city);

city= new City();
city.setId(8);
city.setName("开封市");
city.setProvinceId(3);
cityList.add(city);

city= new City();
city.setId(8);
city.setName("洛阳市");
city.setProvinceId(3);
cityList.add(city);

cityMap.put(3, cityList);


return SUCCESS;

}
}

现在写配置文件:

<action name="doubleSelect" class="com.student.action.DoubleSelectAction">
<result name="success">/tagsDoubleSelect.jsp</result>
</action>

?

最后写显示级联下拉菜单的web页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>struts2级联下拉菜单示例</title>
</head>
<body>

<s:form name="f1">
<s:doubleselect list="provinceList" listKey="id" listValue="name"
doubleName="cityId"
doubleList="cityMap.get(top.id)" doubleListKey="id" doubleListValue="name"/>
</s:form>
</body>
</html>

?

此处要注意的是top的用法,top代表的就是list当前选中的对象, 所以top.id对应的就是当前选中的Province对象中的ID, cityMap.get(top.id)即根据当前选中的Province对象中的ID来取出第二级下拉框的数据集合

?

  相关解决方案