struts-menu 经典例子

http://hi.baidu.com/jmugis/blog/item/76a761a4c73043f39152ee61.html


使用struts-menu实现动态菜单-基于数据库的配置(转)(2)
2008-01-19 19:56

12,填写数据库
未标题-4.jpg

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,效果图:
未标题-5.jpg

上边部分是listview样式显示的效果

下边部分是xtree样式显示的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值