Struts2中doubleselect的使用方法

在Struts2的官方网站上,有doubleselect的用法示例,但是那个例子比较简单。
<s:doubleselect label="doubleselect test2" name="menu" list="#{'fruit':'Nice Fruits', 'other':'Other Dishes'}" doubleName="dishes" doubleList="top == 'fruit' ? {'apple', 'orange'} : {'monkey', 'chicken'}" />
上面的例子演示了doubleselect的基本用法,但是其list和doubleList都是固定的。尤其是doublelist的切换使用了3目运算符 ? :,并没有太大的实际使用价值。在实际应用中,list往往是action返回的一个List<DataObject>,listKey和listValue来显示第一级下拉框,doubleList往往是一个Map<Integer, List<DataObject>>,其中Map中的Key值是第一级下拉框的listKey。
举个例子:
Data Object:
public   class  Book  {
        
private int id;
        
private String name;
        
private int categoryId;

        
// getter and setter..
}


public   class  Category  {
        
private int id;
        
private String name;

        
// getter and setter..
}


JSP:
<s:doubleselect list="categoryList" listKey="id" listValue="name"
   doubleName="bookId" doubleList="bookMap.get(top.id)" doubleListKey="id" doubleListValue="name" theme="simple"/>

此处要注意的是top的用法,开始我以为top就是指代list的值,所以使用的是bookMap.get(top),但是二级下拉框一直是空白,后来我突然想到说不定top是一个Category实例呢,尝试了一下top.id,果然成功了。

Action:

public   class  DemoAction {
        
private  Map < Integer, List < Book >>  bookMap;
        
private  List < Category >  categoryList;

        
public  String execute()  throws  Exception {
                categoryList 
=   new  ArrayList < Cateogry > ();

                Category category;
                category 
=   new  Category();
                category.setId(
1 );
                category.setName(
" Fiction " );
                categoryList.add(category);
                category 
=   new  Category();
                category.setId(
2 );
                category.setName(
" Java " );
                categoryList.add(category);

 

                bookMap 
=   new  HashMap < Integer, List < Book >> ();

                List
< Book >  bookList  =   new  ArrayList < Book > ();
                Book book;
                book 
=   new  Book();
                book.setId(
1 );
                book.setName(
" Harry Porter " );
                book.setCategoryId(
1 );
                bookList.add(book);

                book 
=   new  Book();
                book.setId(
2 );
                book.setName(
" Nightmare " );
                book.setCategoryId(
1 );
                bookList.add(book);

                bookMap.put(
1 , bookList);

 

                bookList 
=   new  ArrayList < Book > ();
                book 
=   new  Book();
                book.setId(
3 );
                book.setName(
" Thinking in Java " );
                book.setCategoryId(
2 );
                bookList.add(book);
                book 
=   new  Book();
                book.setId(
4 );
                book.setName(
" Head First Design Patterns " );
                book.setCategoryId(
2 );
                bookList.add(book);

                bookMap.put(
2 , bookList);


                
return  SUCCESS;
        }

        
//  getter and setter..
}





你可能感兴趣的:(Struts2中doubleselect的使用方法)