惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

博客园 - 飞翔

杂记 VI使用技巧 浮躁,太浮躁了 有空看看的一些技术文章 工作流概述 JavaMail 应用 - 飞翔 - 博客园 权限系统概要 --转 看的的有意思的帖子收集 动态编译JAVA程序 Log4j基本使用方法 dom4j 读取xml文件时遇到的怪问题(org.dom4j.DocumentException: no protocol) spring+hibernate 处理oracle clob (编辑中) 多线程 Thread 学习杂记 Oracle插入大量文字 网页 的一些信息 - 飞翔 - 博客园 Struts Validator验证器使用指南 错误汇总 谈IFRAME标签的使用 appfuse 杂记 - 飞翔 - 博客园
Struts menu的使用(动态数据)
飞翔 · 2005-08-30 · via 博客园 - 飞翔

 以下是我在实际项目中使用了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 文件,增加如下部分

   <plug-in className="net.sf.navigator.menu.MenuPlugIn">
      
<set-property property="menuConfig"
        value
="/WEB-INF/menu-config.xml"/>
   
</plug-in>

3.获得数据,构造树状菜单:
  无论何种方法获得数据(JDBC,HIBERNATE。。。),
    我们要构造的树状菜单需要2个必须元素: 1.显示的名称(name) 2.连接到的目标位置(location)
  TreeBuilder.java
=================================================================================================

/*
 * 创建日期 2005-8-6
 */
import ....

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