struts2中<s:doubleselect/>标签的使用
先看bean中的代码:
//城市对象
public class CityBean {
private int cityid;
private String cityname;
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
}
//区域对象
public class AreaBean {
private int areaid;
private String areaname;
private int cityid;
public int getAreaid() {
return areaid;
}
public void setAreaid(int areaid) {
this.areaid = areaid;
}
public String getAreaname() {
return areaname;
}
public void setAreaname(String areaname) {
this.areaname = areaname;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
}
//action中代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sf.bean.AreaBean;
import sf.bean.CityBean;
import com.opensymphony.xwork2.ActionSupport;
public class CityAction extends ActionSupport {
//map映射cityid--区域列表
private Map<Integer, List<AreaBean>> areaMap = new HashMap<Integer, List<AreaBean>>();
//list装所有city集合
private List<CityBean> cityBeans = new ArrayList<CityBean>();
public Map<Integer, List<AreaBean>> getAreaMap() {
return areaMap;
}
public void setAreaMap(Map<Integer, List<AreaBean>> areaMap) {
this.areaMap = areaMap;
}
public List<CityBean> getCityBeans() {
return cityBeans;
}
public void setCityBeans(List<CityBean> cityBeans) {
this.cityBeans = cityBeans;
}
public String select() throws Exception {
//组装数据
for (int i = 1; i <=3; i++) {
CityBean cityBean = new CityBean();
cityBean.setCityid(i);
cityBean.setCityname("cityname"+i);
cityBeans.add(cityBean);
List<AreaBean> areaBeans = new ArrayList<AreaBean>();
//i==1 j=1 2,3
//i==2 j=4 5 6
//i=3 j==7 8 9
for (int j = 3*i-2; j <= 3*i; j++) {
AreaBean areaBean = new AreaBean();
areaBean.setAreaid(j);
areaBean.setAreaname("area"+i+"-"+j);
areaBean.setCityid(i);
areaBeans.add(areaBean);
}
areaMap.put(i, areaBeans);
}
return "city";
}
}
//jsp页面写法
<h3>
使用s:doubleselect生成级联下拉列表框 必须要结合表单form一起使用 </h3>
< s:form action="userAction!add" method="POST" name="userForm"
namespace="/system">
<!-- theme:simple xhtml css_xhtml struts2不提供ajax -->
<table border="1" width="60%" bordercolor="#cc0000" cellspacing="0"
cellpadding="0">
<tr>
<td >
<!-- areaMap[top.cityid]: top是指前一个下拉框整个的选中项cityBean cityid就是指cityBean中cityid
所以areaMap[top.cityid]指map集合中第一个下拉框选中的id对应的list集合
list里面每个元素放的是AreaBean
-->
<s:doubleselect
tooltip="选择城市操作"
label="请选择城市"
name="city"
list="cityBeans"
listKey="cityid"
listValue="cityname"
doubleList="areaMap[top.cityid]"
doubleName="area"
doubleListKey="areaid"
doubleListValue="areaname"
/>
</td>
</tr>
</table>
</s:form>
doubleselect属性说明:
tooltip指显示图标的显示文字
label 指生成组件前的标签文字
name 指第一个下拉框的表单name属性
list 指第一个下拉框的集合
listKey 指第一个下拉框的选项的键值 提交后天的值 --对应集合里单个对象里面的属性
listValue 指第一个下拉框中的选项显示值 --对应集合里单个对象里面的属
doubleList 指第二个下拉框要使用的集合
doubleName 指第二个下拉框的表单name属性
doubleListKey 指第二个下拉框中的选项中的键值 --对应集合里单个对象里面的属性
doubleListValue 指第二个下拉框中的选项显示值 --对应集合里单个对象里面的属性
上面jsp代码出来的效果是两个下拉框分两行显示的如何让它们一行显示呢?比较简单的做法是:
定义样式:
<style type="text/css">
.doubleselect br{
display: none;
}
< /style>
< div class="doubleselect">把<s:form>的代码放进来就好啦</div
//城市对象
public class CityBean {
private int cityid;
private String cityname;
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
}
//区域对象
public class AreaBean {
private int areaid;
private String areaname;
private int cityid;
public int getAreaid() {
return areaid;
}
public void setAreaid(int areaid) {
this.areaid = areaid;
}
public String getAreaname() {
return areaname;
}
public void setAreaname(String areaname) {
this.areaname = areaname;
}
public int getCityid() {
return cityid;
}
public void setCityid(int cityid) {
this.cityid = cityid;
}
}
//action中代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sf.bean.AreaBean;
import sf.bean.CityBean;
import com.opensymphony.xwork2.ActionSupport;
public class CityAction extends ActionSupport {
//map映射cityid--区域列表
private Map<Integer, List<AreaBean>> areaMap = new HashMap<Integer, List<AreaBean>>();
//list装所有city集合
private List<CityBean> cityBeans = new ArrayList<CityBean>();
public Map<Integer, List<AreaBean>> getAreaMap() {
return areaMap;
}
public void setAreaMap(Map<Integer, List<AreaBean>> areaMap) {
this.areaMap = areaMap;
}
public List<CityBean> getCityBeans() {
return cityBeans;
}
public void setCityBeans(List<CityBean> cityBeans) {
this.cityBeans = cityBeans;
}
public String select() throws Exception {
//组装数据
for (int i = 1; i <=3; i++) {
CityBean cityBean = new CityBean();
cityBean.setCityid(i);
cityBean.setCityname("cityname"+i);
cityBeans.add(cityBean);
List<AreaBean> areaBeans = new ArrayList<AreaBean>();
//i==1 j=1 2,3
//i==2 j=4 5 6
//i=3 j==7 8 9
for (int j = 3*i-2; j <= 3*i; j++) {
AreaBean areaBean = new AreaBean();
areaBean.setAreaid(j);
areaBean.setAreaname("area"+i+"-"+j);
areaBean.setCityid(i);
areaBeans.add(areaBean);
}
areaMap.put(i, areaBeans);
}
return "city";
}
}
//jsp页面写法
<h3>
使用s:doubleselect生成级联下拉列表框 必须要结合表单form一起使用 </h3>
< s:form action="userAction!add" method="POST" name="userForm"
namespace="/system">
<!-- theme:simple xhtml css_xhtml struts2不提供ajax -->
<table border="1" width="60%" bordercolor="#cc0000" cellspacing="0"
cellpadding="0">
<tr>
<td >
<!-- areaMap[top.cityid]: top是指前一个下拉框整个的选中项cityBean cityid就是指cityBean中cityid
所以areaMap[top.cityid]指map集合中第一个下拉框选中的id对应的list集合
list里面每个元素放的是AreaBean
-->
<s:doubleselect
tooltip="选择城市操作"
label="请选择城市"
name="city"
list="cityBeans"
listKey="cityid"
listValue="cityname"
doubleList="areaMap[top.cityid]"
doubleName="area"
doubleListKey="areaid"
doubleListValue="areaname"
/>
</td>
</tr>
</table>
</s:form>
doubleselect属性说明:
tooltip指显示图标的显示文字
label 指生成组件前的标签文字
name 指第一个下拉框的表单name属性
list 指第一个下拉框的集合
listKey 指第一个下拉框的选项的键值 提交后天的值 --对应集合里单个对象里面的属性
listValue 指第一个下拉框中的选项显示值 --对应集合里单个对象里面的属
doubleList 指第二个下拉框要使用的集合
doubleName 指第二个下拉框的表单name属性
doubleListKey 指第二个下拉框中的选项中的键值 --对应集合里单个对象里面的属性
doubleListValue 指第二个下拉框中的选项显示值 --对应集合里单个对象里面的属性
上面jsp代码出来的效果是两个下拉框分两行显示的如何让它们一行显示呢?比较简单的做法是:
定义样式:
<style type="text/css">
.doubleselect br{
display: none;
}
< /style>
< div class="doubleselect">把<s:form>的代码放进来就好啦</div