http://hi.baidu.com/jmugis/blog/item/76a761a4c73043f39152ee61.html
12,填写数据库
13,创建index.jsp文件
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/sql.tld" prefix="sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page import="java.util.Map,
javax.servlet.jsp.jstl.sql.Result,
net.sf.navigator.menu.MenuComponent,
net.sf.navigator.menu.MenuRepository"%>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>menu test</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles/global.css" />
<link rel="stylesheet" type="text/css" media="screen" href="styles/menuExpandable.css"/>
<script type="text/javascript" src="scripts/menuExpandable.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="styles/xtree.css"/>
<script type="text/javascript" src="scripts/xtree.js"></script>
<script type="text/javascript">
/* Function for showing and hiding elements that use 'display:none' to hide */
function toggleDisplay(targetId) {
if (document.getElementById) {
target = document.getElementById(targetId);
if (target.style.display == "none"){
target.style.display = "";
} else {
target.style.display = "none";
}
}
}
</script>
</head>
<body>
<sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" user="" password=""
url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Program Files\\eclipse302\\workspace\dbMenu\\dm.mdb"
var="mydb" />
<sql:transaction dataSource="${mydb}">
<sql:query var="menus">
SELECT * FROM dm order by id;
</sql:query>
</sql:transaction>
<%
// I had issues using the existing repository - creating a new one
// seems to solve the problem. If you figure out how to use the default
// Repository and keep your menus from duplicating themselves - please
// let me know!
MenuRepository repository = new MenuRepository();
// Get the repository from the application scope - and copy the
// DisplayerMappings from it.
MenuRepository defaultRepository = (MenuRepository)
application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
repository.setDisplayers(defaultRepository.getDisplayers());
Result result = (Result) pageContext.getAttribute("menus");
Map[] rows = result.getRows();
for (int i=0; i < rows.length; i++) {
MenuComponent mc = new MenuComponent();
Map row = rows[i];
String name = (String) row.get("name");
mc.setName(name);
String parent = (String) row.get("parent_name");
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.get("title");
mc.setTitle(title);
String location = (String) row.get("location");
mc.setLocation(location);
repository.addMenu(mc);
}
pageContext.setAttribute("repository", repository);
%>
menu test
<hr>
<a href="tabbedMenu.jsp?home">static Tab Menu </a>
<hr>
<a href="dbtabbedMenu.jsp?home"> Database Driven Tab Menu </a>
<hr>
<a href="xmlForm.jsp"> static Xml Roles Menu </a>
<hr>
<a href="myRolesForm.jsp"> dababase driven Roles Menu </a>
<hr>
<h1>Database Driven Menu</h1>
<hr>
<div class="dynamicMenu">
<menu:useMenuDisplayer name="ListMenu" repository="repository">
<menu:displayMenu name="DatabaseMenu"/>
<menu:displayMenu name="StandaloneMenu"/>
<menu:displayMenu name="niupi"/>
</menu:useMenuDisplayer>
</div>
<hr>
<div class="dynamicMenu tree">
<script type="text/javascript">
<menu:useMenuDisplayer name="Velocity" config="/templates/xtree.html"
repository="repository">
<c:forEach var="menu" items="${repository.topMenus}">
<menu-el:displayMenu name="${menu.name}"/>
</c:forEach>
</menu:useMenuDisplayer>
</script>
</div>
<hr>
</body>
</html>
14,效果图:
上边部分是listview样式显示的效果
下边部分是xtree样式显示的效果