Struts2中的doubleselect使用

因为项目需要用到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<DataBean>, 而doubleList就可以用Map<String, List<DataBean>>来存储, 其中map的键名String与第一级下拉框相关联.

例:

    两个DataBean

 

public class Father {
    int id;
String name;

  //getter and setter

}
    
 
public class Son {
    int id;
    String name;
    int fatherId;
    //getter and setter
 
}

 Action:

 

public class DemoAction {
        private List<Father> fatherList;
        private Map<Integer, List<Son>> sonMap;

        public String execute() throws Exception {
                fatherList= new ArrayList<Father>();

                Father father;
                father= new Father();
                father.setId(1);
                father.setName("Test");
                fatherList.add(father);

                father = new Category();
                father.setId(2);
                father.setName("Test2");
                fatherList.add(father);

 

                sonMap = new HashMap<Integer, List<Son>>();

                List<Son> sonList = new ArrayList<Son>();
                Son son;
                son= new Book();
                son.setId(1);
                son.setName("SonTest");
                son.setFatherId(1);
                sonList.add(son);

                son= new Book();
                son.setId(2);
                son.setName("SonTest2");
                son.setFatherId(1);
                sonList.add(son);

                sonMap.put(1, sonList);

                sonList = new ArrayList<Son>();
                son= new Book();
                son.setId(3);
                son.setName("SonTest3");
                son.setFatherId(2);
                sonList.add(son);

                son= new Book();
                son.setId(4);
                son.setName("SonTest4");
                son.setFatherId(2);
                sonList.add(son);

                sonMap.put(2, sonList);


                return SUCCESS;
        }

        // getter and setter..
}

 JSP:

 

<s:doubleselect list="fatherList" listKey="id" listValue="name" 
doubleName="sonId" doubleList="sonMap.get(top.id)" doubleListKey="id" doubleListValue="name" 
theme="simple"/>  

 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值