1.第一个例子:
<s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00" headerValue="00"></s:select>
2.第二个例子:
<s:select list="#{1:'aa',2:'bb',3:'cc'}" label="abc" listKey="key" listValue="value" headerKey="0" headerValue="aabb">
list 是要显示的集合,listKey是要显示的元素id,listValue是要显示的元素的值,headerKey是默认头部显示的值的id,headerValue是默认头部显示的元素的值
3.第三个例子:
<%
HashMap map = new LinkedHashMap();
map.put(1,"aaa");map.put(2,"bbb");
map.put(3,"ccc");
request.setAttribute("map",map);
request.setAttribute("aa","2");
%>
<s:select list="#request.map" label="abc"
listKey="key" listValue="value"
value="#request.aa" headerKey="0"
headerValue="aabb"></s:select>
<s:select list="discussions" listKey="id" listValue="discussionName" name="seldis" value="%{subject.discussion.id}"></s:select>
这是struts中的select标签,discussions是List类型对象,id是调用对象中getId方法,value类似,主要是这里的value值,它采用ognl语言,参数是当前传过来对象的id值,select标签会根据这个value值与listvalue值是否相同决定当前条目是否默认显示。达到一种修改时可以保持原选择内容的效果。
<s:select label="商品分类" name="categoryParentId"
id="topList" emptyOption="true"
value="%{commodity.commodityCategory.id }"
list="commoditiesPagination.items"
listKey="id" listValue="categoryName"
headerKey="1" headerValue="-商品类型-"
onchange="categorySelect()"/>
=============================================================
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 classProvince{
int id;
String name;
//set和get方法
}
package com.student.bean;
public classCity{
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来取出第二级下拉框的数据集合。
分享到:
相关推荐
struts2 doubleselect标签的用法例子,功能是实现一个级联的下拉列表,一般书上的例子都用到了三目运算符,没有实际的使用价值。本例是一个完整的例子,已经调试运行正常。
struts2 doubleselect 实例
struts2 标签动态数据用法, 在实际应用中,list往往是action返回的一个List,listKey和listValue来显示第一级下拉框,doubleList往往是一个Map, List<DataObject>>,其中Map中的Key值是第一级下拉框的listKey。
J2EE中使用struts2实现的select和doubleselect标签
用struts2 doubleselect标签实现的二级联动下拉菜单
struts2中doubleselect标签的具体应用实例,Hibernate、Struts2、Spring三框架整合
网络上找了很多Struts2级联doubleselect的实例,都没又找到可以直接的使用的源代码,给大家分享一下,可直接导入进行测试
Struts2的doubleselect标签演示,完整片断代码
应用extjs4和struts2开发的省市区三级联动完整示例,导入myeclipse即可用,无需再导入任何jar包
Struts2-Double-Select-Example
从数据库中读取数据,hibernate,struts2,spring三框架整合
Struts2中doubleselect的使用方法,在Struts2的官方网站上,有doubleselect的用法示例,但是那个例子比较简单,且静态的。
5.1 Struts标签的使用方法 59 5.2 表单标签的共同属性 60 5.3 form标签 62 5.4 textfield、password、hidden标签 63 5.5 submit标签 65 5.6 reset标签 65 5.7 label标签 66 5.8 head标签 66 5.9 textarea...
doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现doubleselect实现...
struts2+json_jquery实现联动.struts2的标签doubleselect无法实现第二级有数据.只好改为json来接收对象实现
doubleselect. \
5.1 Struts标签的使用方法 59 5.2 表单标签的共同属性 60 5.3 form标签 62 5.4 textfield、password、hidden标签 63 5.5 submit标签 65 5.6 reset标签 65 5.7 label标签 66 5.8 head标签 66 5.9 textarea标签 66 ...
Struts 2 标签库(文档手册) Tags-API-CLSW-JSP <%@ taglib prefix="s" uri="/struts-tags" %> 就能使用struts2.0的标签库 下面就介绍每个标签的具体应用实例说明:按字母排列 A: 1. 2. <s:a href=""></s:a>-...