以下是我在实际项目中使用了struts-menu,现将他们写出来已供参考
1.
拷贝必要的jar库文件到dbMenu/WEB-INF/lib
log4j-
1.2.8
.jar
struts-menu-2.3.jar
拷贝menu-config.xml ,struts-menu.tld 到WEB-INF目录下,
及一些必须的图片和jscript,css到相应的目录
Images/*.*
Scripts/ menuExpandable.js xtree.js
Styles/ menuExpandable.css global.css xtree.css
2.
修改struts-config.xml 文件,增加如下部分 3.获得数据,构造树状菜单:
无论何种方法获得数据(JDBC,HIBERNATE。。。),
我们要构造的树状菜单需要2个必须元素: 1.显示的名称(name) 2.连接到的目标位置(location)
TreeBuilder.java
=================================================================================================
/*
* 创建日期 2005-8-6
*/
import ....
<plug-in className="net.sf.navigator.menu.MenuPlugIn">
<set-property property="menuConfig"
value
="/WEB-INF/menu-config.xml"/>
</plug-in>
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;
/**
* @author yutengfei
*/
public class TreeBuilder {
protected final static Log log = LogFactory.getLog("TreeBuilder");
public TreeBuilder() { }
// 以下代码生成树结构,并以"treerepository"名储存在session 中
public static void initialTree(HttpServletRequest request, HttpServlet servlet){
try {
MenuRepository repository = new MenuRepository();
ServletContext context = servlet.getServletContext()
// Get the repository from the application scope - and copy the
// DisplayerMappings from it.
MenuRepository defaultRepository = (MenuRepository) context.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
//your code begin
List tree = getTreeDataFromDataBase(); //
// 产生从数据库中获得的数据列表(一个树节点是一个NoteBean 对象) ,获得数据的方法这里没有提供
for(Iterator it = tree.iterator();it.hasNext();)
{
MenuComponent mc = new MenuComponent(); //建立一个menu对象(树节点),然后设置他的属性
NoteBean row =(NoteBean) it.next()
String name = (String) row.getName();
mc.setName(name); //设置显示的节点名称
String parent = (String) row.getParentname();
System.out.println(name + ", parent is: " + parent);
if (parent != null) {//存在“父”节点
MenuComponent parentMenu = repository.getMenu(parent);
if (parentMenu == null) {
System.out.println("parentMenu '" + parent + "' doesn't exist!");
// create a temporary parentMenu
parentMenu = new MenuComponent();
parentMenu.setName(parent);
repository.addMenu(parentMenu);
}
mc.setParent(parentMenu); //设置“父”节点
}
String title = (String) row.getTitle();
mc.setTitle(title);
String location = (String) row.getLocation();
mc.setLocation(location);
repository.addMenu(mc);
}
request.getSession().setAttribute("treerepository", repository); //整个树结构储存在session中
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
4.前台jsp显示树菜单
<%try{%>
<%
if (session.getAttribute("treerepository") == null) {
TreeBuilder .initialTree(request, (HttpServlet) pageContext.getPage());
}%>
<%List list = new ArrayList();
MenuRepository repository = (MenuRepository) session.getAttribute("treerepository");
list = repository.getTopMenus();
String name = "";%>
<div class="dynamicMenu"><menu:useMenuDisplayer name="ListMenu" repository="treerepository">
<%for (Iterator it = list.iterator(); it.hasNext();) {
name = ((MenuComponent) it.next()).getName();%>
<menu:displayMenu name="<%=name%>" />
<%}%>
</menu:useMenuDisplayer></div>
<%}catch(Exception e){
e.printStackTrace();
}
%>
5 over