实战Struts-Menu(2)
4:解压缩struts-menu-2.3.zip,将压缩包中的jstl-1.0.6.jar、standard-1.0.6.jar、struts- menu-2.3.jar释放到lib目录中,将压缩包中的struts-menu.tld、struts-menu-el.tld释放到web\WEB -INF目录中,解压缩commons-lang-2.1.zip,将commons-lang-2.1.jar解压缩到lib目录中,注意,在 Struts-Menu的文档中没有看到需要这个包,但是没有这个包却无法成功加载。在web\WEB-INF目录中新建menu- config.xml,内容如下:
<?
xml version="1.0" encoding="UTF-8"
?>
< MenuConfig >
< Displayers >
< Displayer name ="DropDown"
type ="net.sf.navigator.displayer.DropDownMenuDisplayer" />
< Displayer name ="Simple"
type ="net.sf.navigator.displayer.SimpleMenuDisplayer" />
< Displayer name ="CoolMenu"
type ="net.sf.navigator.displayer.CoolMenuDisplayer" />
< Displayer name ="CoolMenu4"
type ="net.sf.navigator.displayer.CoolMenuDisplayer4" />
< Displayer name ="MenuForm"
type ="net.sf.navigator.example.PermissionsFormMenuDisplayer" />
< Displayer name ="ListMenu"
type ="net.sf.navigator.displayer.ListMenuDisplayer" />
< Displayer name ="TabbedMenu"
type ="net.sf.navigator.displayer.TabbedMenuDisplayer" />
< Displayer name ="Velocity"
type ="net.sf.navigator.displayer.VelocityMenuDisplayer" />
</ Displayers >
< Menus >
< Menu name ="DoorSite" title ="DoorSite" description ="Some famous doorsite" width ="50" >
< Item name ="Yahoo" title ="Yahoo" >
< Item name ="YahooIndex" title ="Yahoo Index" location ="http://www.yahoo.com.cn" />
< Item name ="YahooMail" title ="Yahoo Mail" location ="http://cn.mail.yahoo.com" />
</ Item >
< Item name ="Sohu" title ="Sohu" location ="http://www.sohu.com" />
< Item name ="Sina" title ="Sina" location ="http://www.sina.com.cn" />
</ Menu >
</ Menus >
</ MenuConfig >
5:将第四步的示例程序中的struts-menu应用目录下的images、scripts、styles三个目录中的内容复制到web目录下
< MenuConfig >
< Displayers >
< Displayer name ="DropDown"
type ="net.sf.navigator.displayer.DropDownMenuDisplayer" />
< Displayer name ="Simple"
type ="net.sf.navigator.displayer.SimpleMenuDisplayer" />
< Displayer name ="CoolMenu"
type ="net.sf.navigator.displayer.CoolMenuDisplayer" />
< Displayer name ="CoolMenu4"
type ="net.sf.navigator.displayer.CoolMenuDisplayer4" />
< Displayer name ="MenuForm"
type ="net.sf.navigator.example.PermissionsFormMenuDisplayer" />
< Displayer name ="ListMenu"
type ="net.sf.navigator.displayer.ListMenuDisplayer" />
< Displayer name ="TabbedMenu"
type ="net.sf.navigator.displayer.TabbedMenuDisplayer" />
< Displayer name ="Velocity"
type ="net.sf.navigator.displayer.VelocityMenuDisplayer" />
</ Displayers >
< Menus >
< Menu name ="DoorSite" title ="DoorSite" description ="Some famous doorsite" width ="50" >
< Item name ="Yahoo" title ="Yahoo" >
< Item name ="YahooIndex" title ="Yahoo Index" location ="http://www.yahoo.com.cn" />
< Item name ="YahooMail" title ="Yahoo Mail" location ="http://cn.mail.yahoo.com" />
</ Item >
< Item name ="Sohu" title ="Sohu" location ="http://www.sohu.com" />
< Item name ="Sina" title ="Sina" location ="http://www.sina.com.cn" />
</ Menu >
</ Menus >
</ MenuConfig >
6:你的目录结构应该类似如下:
%PROJECT_HOME%\classes
%PROJECT_HOME%\lib
%PROJECT_HOME%\lib\antlr.jar
%PROJECT_HOME%\lib\commons-beanutils.jar
%PROJECT_HOME%\lib\commons-digester.jar
%PROJECT_HOME%\lib\commons-fileupload.jar
%PROJECT_HOME%\lib\commons-lang- 2.1 .jar
%PROJECT_HOME%\lib\commons-logging.jar
%PROJECT_HOME%\lib\commons-validator.jar
%PROJECT_HOME%\lib\jakarta-oro.jar
%PROJECT_HOME%\lib\jstl- 1.0.6 .jar
%PROJECT_HOME%\lib\standard- 1.0.6 .jar
%PROJECT_HOME%\lib\struts.jar
%PROJECT_HOME%\lib\struts-menu- 2.3 .jar
%PROJECT_HOME%\src
%PROJECT_HOME%\src\log4j.properties
%PROJECT_HOME%\src\application.properties
%PROJECT_HOME%\src\application_zh_CN.properties
%PROJECT_HOME%\web
%PROJECT_HOME%\web\images\
%PROJECT_HOME%\web\scripts\
%PROJECT_HOME%\web\styles\
%PROJECT_HOME%\web\WEB-INF
%PROJECT_HOME%\web\WEB-INF\menu-config.xml
%PROJECT_HOME%\web\WEB-INF\struts-bean.tld
%PROJECT_HOME%\web\WEB-INF\struts-config.xml
%PROJECT_HOME%\web\WEB-INF\struts-html.tld
%PROJECT_HOME%\web\WEB-INF\struts-logic.tld
%PROJECT_HOME%\web\WEB-INF\struts-menu.tld
%PROJECT_HOME%\web\WEB-INF\struts-menu-el.tld
%PROJECT_HOME%\web\WEB-INF\struts-nested.tld
%PROJECT_HOME%\web\WEB-INF\struts-tiles.tld
%PROJECT_HOME%\web\WEB-INF\web.xml
%PROJECT_HOME%\lib
%PROJECT_HOME%\lib\antlr.jar
%PROJECT_HOME%\lib\commons-beanutils.jar
%PROJECT_HOME%\lib\commons-digester.jar
%PROJECT_HOME%\lib\commons-fileupload.jar
%PROJECT_HOME%\lib\commons-lang- 2.1 .jar
%PROJECT_HOME%\lib\commons-logging.jar
%PROJECT_HOME%\lib\commons-validator.jar
%PROJECT_HOME%\lib\jakarta-oro.jar
%PROJECT_HOME%\lib\jstl- 1.0.6 .jar
%PROJECT_HOME%\lib\standard- 1.0.6 .jar
%PROJECT_HOME%\lib\struts.jar
%PROJECT_HOME%\lib\struts-menu- 2.3 .jar
%PROJECT_HOME%\src
%PROJECT_HOME%\src\log4j.properties
%PROJECT_HOME%\src\application.properties
%PROJECT_HOME%\src\application_zh_CN.properties
%PROJECT_HOME%\web
%PROJECT_HOME%\web\images\
%PROJECT_HOME%\web\scripts\
%PROJECT_HOME%\web\styles\
%PROJECT_HOME%\web\WEB-INF
%PROJECT_HOME%\web\WEB-INF\menu-config.xml
%PROJECT_HOME%\web\WEB-INF\struts-bean.tld
%PROJECT_HOME%\web\WEB-INF\struts-config.xml
%PROJECT_HOME%\web\WEB-INF\struts-html.tld
%PROJECT_HOME%\web\WEB-INF\struts-logic.tld
%PROJECT_HOME%\web\WEB-INF\struts-menu.tld
%PROJECT_HOME%\web\WEB-INF\struts-menu-el.tld
%PROJECT_HOME%\web\WEB-INF\struts-nested.tld
%PROJECT_HOME%\web\WEB-INF\struts-tiles.tld
%PROJECT_HOME%\web\WEB-INF\web.xml
六、实战Struts-Menu
1:使用配置文件实现静态菜单
新建JSP文件web/static-menu.jsp,内容如下:
<%
@ page contentType
=
"
text/html; charset=GBK
"
%>
<% @ taglib uri = " struts-menu " prefix = " menu " %>
< menu:useMenuDisplayer name ="ListMenu"
bundle ="org.apache.struts.action.MESSAGE" >
< menu:displayMenu name ="DoorSite" />
</ menu:useMenuDisplayer >
运行Tomcat,在IE地址栏输入 http://localhost:8080/mymenu/static-menu.jsp查看
<% @ taglib uri = " struts-menu " prefix = " menu " %>
< menu:useMenuDisplayer name ="ListMenu"
bundle ="org.apache.struts.action.MESSAGE" >
< menu:displayMenu name ="DoorSite" />
</ menu:useMenuDisplayer >
2:实现中文化
> 在src\application_zh_CN.properties中增加下面的内容,Unicode可以通过JDK自带的native2ascii工具得到:
#门户网站
menu.DoorSite = \u95e8\u6237\u7f51\u7ad9
#雅虎
menu.Yahoo = \u96c5\u864e
#雅虎首页
menu.YahooIndex = \u96c5\u864e\u9996\u9875
#雅虎邮件
menu.YahooMail = \u96c5\u864e\u90ae\u4ef6
#搜狐
menu.Sohu = \u641c\u72d0
#新浪
menu.Sina = \u65b0\u6d6a
> 在src\application.properties中增加下面的内容:
menu.DoorSite = \u95e8\u6237\u7f51\u7ad9
#雅虎
menu.Yahoo = \u96c5\u864e
#雅虎首页
menu.YahooIndex = \u96c5\u864e\u9996\u9875
#雅虎邮件
menu.YahooMail = \u96c5\u864e\u90ae\u4ef6
#搜狐
menu.Sohu = \u641c\u72d0
#新浪
menu.Sina = \u65b0\u6d6a
#门户网站
menu.DoorSite = DoorSite
#雅虎
menu.Yahoo = Yahoo
#雅虎首页
menu.YahooIndex = Yahoo Index
#雅虎邮件
menu.YahooMail = Yahoo Mail
#搜狐
menu.Sohu = Sohu
#新浪
menu.Sina = Sina
> 修改menu-config.xml文件<Menus></Menus>部分:
menu.DoorSite = DoorSite
#雅虎
menu.Yahoo = Yahoo
#雅虎首页
menu.YahooIndex = Yahoo Index
#雅虎邮件
menu.YahooMail = Yahoo Mail
#搜狐
menu.Sohu = Sohu
#新浪
menu.Sina = Sina
<
Menus
>
< Menu name ="DoorSite" title ="menu.DoorSite" description ="Some famous doorsite" width ="50" >
< Item name ="Yahoo" title ="menu.Yahoo" >
< Item name ="YahooIndex" title ="menu.YahooIndex" location ="http://www.yahoo.com.cn" />
< Item name ="YahooMail" title ="menu.YahooMail" location ="http://cn.mail.yahoo.com" />
</ Item >
< Item name ="Sohu" title ="menu.Sohu" location ="http://www.sohu.com" />
< Item name ="Sina" title ="menu.Sina" location ="http://www.sina.com.cn" />
</ Menu >
</ Menus >
重起Tomcat,在IE地址栏输入 http://localhost:8080/mymenu/static-menu.jsp查看
< Menu name ="DoorSite" title ="menu.DoorSite" description ="Some famous doorsite" width ="50" >
< Item name ="Yahoo" title ="menu.Yahoo" >
< Item name ="YahooIndex" title ="menu.YahooIndex" location ="http://www.yahoo.com.cn" />
< Item name ="YahooMail" title ="menu.YahooMail" location ="http://cn.mail.yahoo.com" />
</ Item >
< Item name ="Sohu" title ="menu.Sohu" location ="http://www.sohu.com" />
< Item name ="Sina" title ="menu.Sina" location ="http://www.sina.com.cn" />
</ Menu >
</ Menus >
3:结合数据库实现动态菜单
> 新建数据myexamples:
CREATE DATABASE myexamples
> 新建数据库表menu_item:
CREATE TABLE menu_item (
id BIGINT not null ,
parent_name VARCHAR( 30 ) ,
name VARCHAR( 30 ) ,
title VARCHAR( 30 ) ,
titleCN VARCHAR( 30 ) ,
description VARCHAR( 50 ) ,
location VARCHAR( 255 ) ,
target VARCHAR( 10 ) ,
onclick VARCHAR( 100 ) ,
onmouseover VARCHAR( 100 ) ,
onmouseout VARCHAR( 100 ) ,
image VARCHAR( 50 ) ,
altImage VARCHAR( 30 ) ,
tooltip VARCHAR( 100 ) ,
roles VARCHAR( 100 ) ,
page VARCHAR( 255 ) ,
width VARCHAR( 5 ) ,
height VARCHAR( 5 ) ,
forward VARCHAR( 50 ) ,
action VARCHAR( 50 ) ,
primary key (id)
)
id BIGINT not null ,
parent_name VARCHAR( 30 ) ,
name VARCHAR( 30 ) ,
title VARCHAR( 30 ) ,
titleCN VARCHAR( 30 ) ,
description VARCHAR( 50 ) ,
location VARCHAR( 255 ) ,
target VARCHAR( 10 ) ,
onclick VARCHAR( 100 ) ,
onmouseover VARCHAR( 100 ) ,
onmouseout VARCHAR( 100 ) ,
image VARCHAR( 50 ) ,
altImage VARCHAR( 30 ) ,
tooltip VARCHAR( 100 ) ,
roles VARCHAR( 100 ) ,
page VARCHAR( 255 ) ,
width VARCHAR( 5 ) ,
height VARCHAR( 5 ) ,
forward VARCHAR( 50 ) ,
action VARCHAR( 50 ) ,
primary key (id)
)