下载全部源代码:http://www.oksonic.cn
为struts-menu的树型菜单加入复选框
版本 0.2
1. 修改一个BUG,子菜单错误的问题
2. 新增一个menuId 属性,用于返回选中的对像(也就是checkBox的value值)
修改部份:CheckListMenuDisplayer.java
原:
protected void displayComponents(MenuComponent menu, int level)
throws JspException, IOException {
MenuComponent[] components = menu.getMenuComponents();
if(menu.isChecked())
checked = CHECKBOX_IS_CHECKED;
else
checked = CHECKBOX_ISNOT_CHECKED;
if (components.length > 0) {
红字部份取消,移到以下位置:
for (int i = 0; i < components.length; i++) {
MenuComponent mMenu = components[i];
if(menu.isChecked())
checked = CHECKBOX_IS_CHECKED.replaceFirst("\\?", mMenu.getMenuId());
else
checked = CHECKBOX_ISNOT_CHECKED.replaceFirst("\\?", mMenu.getMenuId());
// check the permissions on this component
if (isAllowed(components[i])) {
…………
在MenuBase中加入 String menuId 属性,创建get / set 方法
**************************************************************************************
版本0.1
1. 修改net.sf.navigator.menu.MenuBase 类,加入checked属性,生成get/set方法
2. 修改net.sf.navigator.displayer.DisplayerStrings.properties资源文件,在带有<li标签的结束符后面加入{4}
如:lmd.menu.top=\t<li class="menubar">{4}\n
3. 新建net.sf.navigator.displayer.CheckListMenuDisplayer.java文件,此文件的内容为ListMenuDisplayer.java加以修改而成
在类中加入两个常数
private static final String CHECKBOX_IS_CHECKED="<input type='checkbox' name='checked' checked='CHECKED'/>";
private static final String CHECKBOX_ISNOT_CHECKED="<input type='checkbox' name='checked'/>";
/** 用于判断是否选中 **/
private String checked = CHECKBOX_ISNOT_CHECKED;
a) 修改以下方法为下面的内容
public void display(MenuComponent menu) throws JspException, IOException {
if (isAllowed(menu)) {
if(menu.isChecked())
checked = CHECKBOX_IS_CHECKED;
else
checked = CHECKBOX_ISNOT_CHECKED;
out.println(displayStrings.getMessage("lmd.menu.top", null,null,null,null,checked));
displayComponents(menu, 0);
out.println(displayStrings.getMessage("lmd.menu.bottom"));
}
}
protected void displayComponents(MenuComponent menu, int level)
throws JspException, IOException {
MenuComponent[] components = menu.getMenuComponents();
if(menu.isChecked())
checked = CHECKBOX_IS_CHECKED;
else
checked = CHECKBOX_ISNOT_CHECKED;
if (components.length > 0) {
// eliminate spaces in string used for Id
String domId = StringUtils.deleteWhitespace(getMessage(menu.getName()));
// added to create a unique id everytime
domId += ((int) (1000*Math.random()));
String menuClass = "menu";
if (level >= 1) {
menuClass = "submenu";
}
// if there is a location/page/action tag on base item use it
if (menu.getUrl() != null ){
out.println(displayStrings.getMessage("lmd.menu.actuator.link",
domId, getMessage(menu.getTitle()), menuClass,
getMessage(menu.getUrl())));
} else {
out.println(displayStrings.getMessage("lmd.menu.actuator.top",
domId,
getMessage(menu.getTitle()),
menuClass));
}
for (int i = 0; i < components.length; i++) {
// check the permissions on this component
if (isAllowed(components[i])) {
if (components[i].getMenuComponents().length > 0) {
out.println("<li>");
displayComponents(components[i], level + 1);
out.println(displayStrings.getMessage("lmd.menu.actuator.bottom"));
} else {
out.println(displayStrings.getMessage("lmd.menu.item",
components[i].getUrl(),
super.getMenuToolTip(components[i]),
getExtra(components[i]),
this.getMessage(components[i].getTitle()),
checked
));
}
}
}
// close the </ul> for the top menu
if (menuClass.equals("menu")) {
out.println("</ul>");
}
} else {
if (menu.getParent() == null) {
out.println(displayStrings.getMessage("lmd.menu.standalone",
menu.getUrl(),
super.getMenuToolTip(menu),
getExtra(menu),
getMessage(menu.getTitle()),checked));
} else {
out.println(displayStrings.getMessage("lmd.menu.item",
menu.getUrl(),
super.getMenuToolTip(menu),
getExtra(menu),
getMessage(menu.getTitle()), checked));
}
}
}
4. 修改样式文件
@import url(global.css);
.menuList {
margin: 0px;
padding: 10px 0px 10px 15px;
}
li.menubar {
background: url(../images/plus.gif) no-repeat 2.2em 0.5em;
font-size: 12px;
line-height: 1.5em;
list-style: none outside;
}
.menu, .submenu {
display: none;
margin-left: 15px;
padding: 0px;
}
.menu li, .submenu li {
background: url(../images/square.gif) no-repeat 2em 0.5em;
list-style: none outside;
}
li.menubar a.standalone {
background: url(../images/square.gif) no-repeat 2em 0.5em !important;
}
a.actuator, a.standalone {
background-color: transparent;
color: #000;
font-size: 12px;
padding-left: 15px;
text-decoration: none;
}
.menu li a, .submenu li a {
background-color: transparent;
color: #000;
font-size: 12px;
padding-left: 15px;
text-decoration: none;
}
a.actuator:hover, .menu li a:hover, submenu li a:hover, li a.standalone:hover {
text-decoration: underline;
}
/* styles to allow for base links */
li a.base, li a.base:visited {
background-color: transparent;
color: #000;
font-size: 12px;
padding-left: 0px;
text-decoration: none;
}
li a.base:hover {
color: #CC0000;
text-decoration: none;
}
span.key {
text-decoration: underline;
}
a.highlight {
color: red !important;
font-weight: bold;
}
email:oksonic@tom.com