1, jsp页面内容,注意html,body等要规范.
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://struts-menu.sf.net/tag" prefix="menu" %>
<%@ taglib uri="http://struts-menu.sf.net/tag-el" prefix="menu-el" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
</head>
<body>
<div id="content">
<script type="text/javascript" src="./scripts/coolmenus3.js"></script>
<!-- Custom config for this example -->
<script type="text/javascript" src="./scripts/coolmenu2-config.js"></script>
<menu:useMenuDisplayer name="CoolMenu" repository="repository">
<c:forEach var="menu" items="${repository.topMenus}">
<menu-el:displayMenu name="${menu.name}"/>
</c:forEach>
</menu:useMenuDisplayer>
<br>
<!-- put the form on a layer for coolmenus to handle Netscape4
this is indicated by setting oCMenu.hideForm("document.formlayer")
see the javscript coolmenu configuration file.
-->
<ilayer id="formLayer">
<form action="someaction.do">
options:
<select name="select1" style="width:300px">
<option>option value</option>
</select>
</form>
</ilayer>
</div>
</body>
</html>
2,上述的repository从request来,下面列出action代码。由此可见,struts menu不一定要依赖struts1.
public String showMenu() throws AppException{
request.setAttribute("repository",
treeMenuCreator(0));
return "success";
}
private Object treeMenuCreator(int moduleId) throws AppException{
MenuRepository repository = new MenuRepository();
repository.setLoadParam("/WEB-INF/menu-config.xml");
repository.setServletContext(context);
try {
repository.load();
context.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository);
if (log.isDebugEnabled()) {
log.debug("struts-menu initialization successful");
}
} catch (LoadableResourceException lre) {
throw new AppException("Failure initializing struts-menu: " +
lre.getMessage());
}
repository.setDisplayers(repository.getDisplayers());
//根据moduleId获取属于某个菜单栏目的那些菜单
//List<Integer> list=menuManager.getMenusByModuleId(moduleId);
//返回某菜单栏目的顶层菜单,顶层菜单就是parentId is null
//List topMenus = menuManager.loadTopMenusByModuleId(moduleId);
//遍历菜单
//for (Object o : topMenus) {
// buildTreeMenu(repository, (Menu) o, list, null);
//}
//////////////临时 start
MenuComponent mc = new MenuComponent();
mc.setName("文件");
mc.setTitle("文件");
mc.setParent(null);
repository.addMenu(mc);
MenuComponent mc1=new MenuComponent();
mc1.setName("新建");
mc1.setTitle("新建");
mc1.setParent(mc);
repository.addMenu(mc1);
MenuComponent mc2=new MenuComponent();
mc2.setName("Word");
mc2.setTitle("Word");
mc2.setParent(mc1);
mc2.setUrl("http://www.baidu.com");
mc2.setTarget("_self");
repository.addMenu(mc2);
MenuComponent mc3=new MenuComponent();
mc3.setName("Excel");
mc3.setTitle("Excel");
mc3.setParent(mc1);
mc3.setUrl("http://www.google.com");
mc3.setTarget("_self");
repository.addMenu(mc3);
MenuComponent mc4=new MenuComponent();
mc4.setName("保存");
mc4.setTitle("保存");
mc4.setParent(mc);
mc4.setUrl("http://www.excelerate-tech.com");
mc4.setTarget("_self");
repository.addMenu(mc4);
MenuComponent mc5=new MenuComponent();
mc5.setName("帮助");
mc5.setTitle("帮助");
mc5.setParent(null);
mc5.setUrl(http://www.sina.cn);
mc5.setTarget("_self");
repository.addMenu(mc5);
////////////// 临时 end
return repository;
}
//递归构造菜单
//菜单属性有: id,序号seq,名称title,提示tip,连接url,目标窗口target,父菜单parentMenu,子菜单集合childMenus
/**//*private void buildTreeMenu(MenuRepository repository, Menu menu, List<Integer> menuIds,MenuComponent parentMenu) {
if (menuIds.contains(menu.getId().intValue())) {
MenuComponent mc = new MenuComponent();
mc.setName(menu.getTitle());
mc.setTitle(menu.getTip());
mc.setParent(parentMenu);
Set set = menu.getChildMenu();
if(set!=null && set.size()>0){
for (Object o : set) {
buildTreeMenu(repository, (Menu) o, menuIds,mc);
}
}else{
mc.setPage(menu.getForward());
mc.setTarget(menu.getTarget());
}
repository.addMenu(mc);
}
}*/