云自无心水自闲
天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 128, comments - 167, trackbacks - 0, articles - 6
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
Struts2中doubleselect的使用方法
Posted on 2008-03-27 22:08
云自无心水自闲
阅读(1126)
评论(6)
编辑
收藏
所属分类:
Java
、
心得体会
、
Struts2
在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..
}
Feedback
#
re: Struts2中doubleselect的使用方法[未登录]
回复
更多评论
2008-03-28 12:43 by
allenny
Struts2很多标签的用法要靠猜的,真是很郁闷
#
re: Struts2中doubleselect的使用方法
回复
更多评论
2008-03-29 11:22 by
usherlight
是的,感觉struts2的文档还是不够详细具体。
#
re: Struts2中doubleselect的使用方法
回复
更多评论
2008-04-08 16:44 by
struts2
经测试这段代码有以下几个问题,
1、代码第二行,
private Map<Integer, List<Book>> bookList;
应该是
private Map<Integer, List<Book>> bookMap;
2、book 里面只有一个值,由于没有声明book 类型为singleton,所有你存那么多值,最终在页面最的时候只有一个,那就是 "Head First Design Patterns"
最后谢谢楼主给了我一个提示,最终解决了我的问题。
#
re: Struts2中doubleselect的使用方法
回复
更多评论
2008-04-10 10:40 by
usherlight
谢谢你指出代码中的错误。我这段代码不是在Eclipse这样的集成开发环境里写的,所以出现了一些错误。
#
re: Struts2中doubleselect的使用方法[未登录]
回复
更多评论
2008-04-14 18:09 by
阿伟
不知道为什么总是报:“FreeMarker template error!”的错误???
#
re: Struts2中doubleselect的使用方法[未登录]
回复
更多评论
2008-04-14 18:17 by
阿伟
刚解决,<s:form 里面没加name这个属性,疯掉....
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-04-10 10:48 编辑过
相关文章:
iBatis使用mysql,数据库超时自动关闭问题的解决
用freemarker做模板创建一个struts2自定义标签
Struts2中doubleselect的使用方法
强制JSP页面刷新,防止被服务器缓存
使用struts2中的ognl表达式显示数据表字典项的值
stuts2中session timeout的处理(备忘)
Extjs Tree + JSON + Struts2
Struts2中实现可编辑的表格
Struts2 and Ajax --使用Dojo和Div
获得指定时区的格式化日期字符串
Powered by:
BlogJava
Copyright © 云自无心水自闲
日历
<
2008年3月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
留言簿
(5)
给我留言
查看公开留言
查看私人留言
随笔分类
.Net(6)
Acegi(2)
Ajax(2)
Appfuse(11)
Cairngorm(14)
Dojo(1)
Extjs(1)
Flex(59)
Flex2(50)
iBatis(1)
Java(51)
Struts2(10)
XML(1)
心得体会(40)
数据库(2)
随笔档案
2008年5月 (1)
2008年4月 (2)
2008年3月 (3)
2008年2月 (7)
2008年1月 (3)
2007年12月 (2)
2007年9月 (4)
2007年8月 (3)
2007年7月 (4)
2007年6月 (1)
2007年5月 (1)
2007年4月 (11)
2007年3月 (15)
2007年2月 (11)
2007年1月 (8)
2006年12月 (9)
2006年11月 (3)
2006年10月 (4)
2006年9月 (6)
2006年8月 (3)
2006年6月 (2)
2006年5月 (1)
2006年4月 (3)
2006年3月 (16)
2006年2月 (5)
最新随笔
1. iBatis使用mysql,数据库超时自动关闭问题的解决
2. Html Table表格的小问题
3. 用freemarker做模板创建一个struts2自定义标签
4. Struts2中doubleselect的使用方法
5. 强制JSP页面刷新,防止被服务器缓存
6. 使用struts2中的ognl表达式显示数据表字典项的值
7. Adobe Flex3正式发布了
8. stuts2中session timeout的处理(备忘)
9. Extjs Tree + JSON + Struts2
10. Struts2中实现可编辑的表格
积分与排名
积分 - 88805
排名 - 70
最新评论
1. re: iBatis使用mysql,数据库超时自动关闭问题的解决
..............
--ci
2. re: iBatis使用mysql,数据库超时自动关闭问题的解决
评论内容较长,点击标题查看
--魔域私服
3. re: Flex中使用模块Module的例子
不错,谢谢!
--Apache
4. re: Flex与JavaScript的交互:调用JavaScipt或者被JavaScript调用[未登录]
good
--gg
5. re: Extjs Tree + JSON + Struts2
内容很棒。多谢
--Nicolas
阅读排行榜
1. 连接Oracle 10g时ORA-12514: TNS: 监听进程不能解析在连接描述符中给出的 SERVICE_NAME 错误的解决(11138)
2. Extjs Tree + JSON + Struts2 (3942)
3. Iteration::two的基于Flex的开源项目-- Cairngorm store的学习笔记(三)(2742)
4. Appfuse2.0与Eclipse的真正整合(2673)
5. Tomcat6在JDK1.6下不能启动的解决(2102)
6. Java串口并口程序编写(1869)
7. Iteration::two的基于Flex的开源项目-- Cairngorm store的学习笔记(一)(1516)
8. DevExpress XtraReports的控件介绍(使用帮助)(1508)
9. DevExpress的XtraReports使用心得(帮助文档)(1398)
10. SwisSql Oracle to Sql Server3.0(一个从Oracle迁移到SqlServer数据库工具)的破解手记(1317)
评论排行榜
1. SwisSql Oracle to Sql Server3.0(一个从Oracle迁移到SqlServer数据库工具)的破解手记(18)
2. 连接Oracle 10g时ORA-12514: TNS: 监听进程不能解析在连接描述符中给出的 SERVICE_NAME 错误的解决(16)
3. Java串口并口程序编写(13)
4. Extjs Tree + JSON + Struts2 (10)
5. 使用struts2中的ognl表达式显示数据表字典项的值(8)
60天内阅读排行
1. 使用struts2中的ognl表达式显示数据表字典项的值(1221)
2. Struts2中doubleselect的使用方法(1126)
3. 用freemarker做模板创建一个struts2自定义标签(1056)
4. 强制JSP页面刷新,防止被服务器缓存(361)
5. iBatis使用mysql,数据库超时自动关闭问题的解决(246)
6. Html Table表格的小问题(109)