Use of struts menu (Dynamic Data)

Source: Internet
Author: User
The following figure shows how to use Struts-menu in my actual project.
1.
Copy necessary jar library files to dbmenu \ WEB-INF \ Lib
Log4j- 1.2.8 . Jar
Struts-menu-2.3.jar
Copy the menu-config.xml to the struts-menu.tld directory,
And some necessary images and jscripts, CSS to the corresponding directory
Images /*.*
Scripts/menuexpandable. js xtree. js
Styles/menuexpandable.css global.css xtree.css
2.
Modify the struts-config.xml file, add the following section

<Plug-in classname = "net. SF. Navigator. Menu. menuplugin">
<Set-Property = "menuconfig"
Value = "/WEB-INF/menu-config.xml"/>
</Plug-in>

3. Obtain the data and construct a tree menu:
Whatever method is used to obtain data (JDBC, Hibernate ...),
The tree menu we want to construct requires two required elements: 1. name displayed 2. Connected location)
Treebuilder. Java
========================================================== ========================================================== ========================

/*
* Creation date:
*/
Import ....

Import net. SF. Navigator. Menu. menucomponent;
Import net. SF. Navigator. Menu. menurepository;

/**
* @ Author yutengfei
*/
Public class treebuilder {
Protected final static log = logfactory. getlog ("treebuilder ");
Public treebuilder (){}

// The following code generates a tree structure and stores it in the session with the name "treerepository ".
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
// Displayermappings from it.
Menurepository defaultrepository = (menurepository) Context. getattribute (menurepository. menu_repository_key );

Repository. setdisplayers (defaultrepository. getdisplayers ());

// Your code begin
List tree = gettreedatafromdatabase ();//
// Generate a list of data obtained from the database (A Tree node is a notebean object). The method for obtaining data is not provided here
For (iterator it = tree. iterator (); it. hasnext ();)
{
Menucomponent MC = new menucomponent (); // create a menu object (tree node) and set its attributes
Notebean ROW = (notebean) it. Next ()
String name = (string) Row. getname ();
MC. setname (name); // set the name of the displayed Node
String parent = (string) Row. getparentname ();
System. Out. println (name + ", parent is:" + parent );
If (parent! = NULL) {// a "parent" Node exists
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); // you can specify the parent node.
}
String title = (string) Row. gettitle ();
MC. settitle (title );
String location = (string) Row. getlocation ();
MC. setlocation (location );
Repository. addmenu (MC );
}
Request. getsession (). setattribute ("treerepository", repository); // the entire tree structure is stored in the session.
} Catch (exception ex ){
Ex. printstacktrace ();
}

}

}

4. Foreground JSP display tree menu

<% Try {%>
<%
If (session. getattribute ("treerepository") = NULL ){
Treebuilder. initialtree (request, (httpservlet) pagecontext. getpage ());
} %>
<% 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.