Struts-Menu是一组从基于XML的配置文件中生成多种样式的菜单的JSP Tags,并且可以结合API开发通过数据库生成的动态菜单。本文将通过实战Struts-Menu,为大家揭开Struts-Menu的面纱。
一、简介
Struts-Menu是一组从基于XML的配置文件中生成多种样式的菜单的JSP Tags,并且可以结合API开发通过数据库生成的动态菜单。Struts-Menu支持国际化和多种权限控制。
二、运行环境
Windows 2000 Professional
JDK 1.4.2_03
Eclipse 3.1
Tomcat 5.0.28
Tomcat Plugin 3.1Beta
Struts 1.2.7
Commons-Lang 2.1
Commons-Collections 3.1
Struts-Menu 2.3
MySQL 4.1.10a-nt
三、下载与安装
1:从http://java.sun.com下载J2SDK,当前1.4.x系列的最新版本为1.4.2_08
2:从http://www.eclipse.org下载Eclipse,当前最新版本为3.1正式版
3:从http://jakarta.apache.org/tomcat下载Tomcat,当前5.x系列的最新版本为5.0.28
4:从http://www.sysdeo.com/eclipse/tomcatplugin下载Eclipse的Tomcat插件,对应Eclipse3.1x的最新版本为3.1Beta
5:从http://struts.apache.org下载Struts,当前最新版本为1.2.7
6:从http://jakarta.apache.org/commons/下载Commons-Lang,当前最新版本为2.1,下载Commons-Collections,当前最新版本为3.1
7:从http://struts-menu.sourceforge.net下载Struts Menu,当前最新版本为2.3
8:从http://www.mysql.com下载MySQL数据库,4.x系列的最新版本是4.1.12a
9:MySQL、JDK、Eclipse、Tomcat和TomcatPlugin的安装及配置请参考相关资料
四、运行示例程序
1:安装好Tomcat后,解压缩struts-menu-2.3.zip,将struts-menu.war释放到Tomcat安装目录下的webapps下,运行Tomcat
2:在地址栏输入http://localhost:8080/struts-menu
五、安装与配置
1:在Eclipse中新建Tomcat项目,Context为/mymenu,Subdirectory为/web
2:在项目目录下面新建lib目录和web目录及WEB-INF,在web/WEB-INF目录下新建web.xml,内容如下:
< ?xml version="1.0" encoding="ISO-8859-1"?>
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
< DISPLAY-NAME>My Example Application -- Vinton Lee < !-- ============= The Struts ActionServlet Configuration ============= --> < SERVLET> < SERVLET-NAME>action< /SERVLET-NAME> < SERVLET-CLASS>org.apache.struts.action.ActionServlet< /SERVLET-CLASS> < INIT-PARAM> < PARAM-NAME>config< /PARAM-NAME> < PARAM-VALUE>/WEB-INF/struts-config.xml< /PARAM-VALUE> < /INIT-PARAM> 1 < /SERVLET> < !-- ================================================================== --> < !-- ============= The Struts Action Servlet Mapping ================== --> < SERVLET-MAPPING> < SERVLET-NAME>action< /SERVLET-NAME> < URL-PATTERN>*.do< /URL-PATTERN> < /SERVLET-MAPPING> < !-- The Welcome File List --> < WELCOME-FILE-LIST> < WELCOME-FILE>index.jsp< /WELCOME-FILE> < /WELCOME-FILE-LIST>
< !-- =============== The Struts Taglib Definition ===================== --> < TAGLIB> < TAGLIB-URI>struts-bean < TAGLIB-LOCATION>/WEB-INF/struts-bean.tld< /TAGLIB-LOCATION> < /TAGLIB> < TAGLIB> < TAGLIB-URI>struts-html< /TAGLIB-URI> < TAGLIB-LOCATION>/WEB-INF/struts-html.tld< /TAGLIB-LOCATION> < /TAGLIB> < TAGLIB> < TAGLIB-URI>struts-logic< /TAGLIB-URI> < TAGLIB-LOCATION>/WEB-INF/struts-logic.tld< /TAGLIB-LOCATION> < /TAGLIB> < TAGLIB> < TAGLIB-URI>struts-nested < TAGLIB-LOCATION>/WEB-INF/struts-nested.tld< /TAGLIB-LOCATION> < /TAGLIB> < TAGLIB> < TAGLIB-URI>struts-tiles< /TAGLIB-URI> < TAGLIB-LOCATION>/WEB-INF/struts-tiles.tld< /TAGLIB-LOCATION> < /TAGLIB> < !-- ================================================================== --> < !-- ============= The Struts-Menu Taglib Definition ================== --> < TAGLIB> < TAGLIB-URI>struts-menu< /TAGLIB-URI> < TAGLIB-LOCATION>/WEB-INF/struts-menu.tld < /TAGLIB> < !-- ================================================================== -->
|
3:解压缩struts-1.2.7.zip,将压缩包中的lib目录下所有的8个jar释放到lib目录中,将5个tld文件释放到web\WEB-INF目录中,在web\WEB-INF目录中新建struts-config.xml,内容如下:
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
< !-- ========== Data Source Configuration =============================== --> < DATA-SOURCES />
< !-- ========== Form Bean Definitions =================================== --> < FORM-BEANS />
< !-- ========== Global Exception Definitions ============================ --> < GLOBAL-EXCEPTIONS />
< !-- ========== Global Forward Definitions ============================== --> < GLOBAL-FORWARDS />
< !-- ========== Action Mapping Definitions ============================== --> < ACTION-MAPPINGS />
< !-- ========== Controller Configuration ================================ -->
< !-- ========== Message Resources Definitions =========================== --> < MESSAGE-RESOURCES parameter="application" />
< !-- ========== Plug Ins Configuration ================================== --> < PLUG-IN className="net.sf.navigator.menu.MenuPlugIn"> < SET-PROPERTY value="/WEB-INF/menu-config.xml" property="menuConfig" /> < /PLUG-IN> |
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 type="net.sf.navigator.displayer.DropDownMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.SimpleMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.CoolMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.CoolMenuDisplayer4"/> < DISPLAYER type="net.sf.navigator.example.PermissionsFormMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.ListMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.TabbedMenuDisplayer"/> < DISPLAYER type="net.sf.navigator.displayer.VelocityMenuDisplayer"/> < /DISPLAYERS>
< MENUS> < MENU title=DoorSite name="DoorSite" width="50" description="Some famous doorsite" > < ITEM title="Yahoo Index" name="YahooIndex" ? location=" < A href=www.51cto.comlocation="http://www.51cto.com"/ > < ITEM title=Sina name="Sina" ? location=" < A href=' http: www.sina.com.cn?>http://www.sina.com.cn"/ A>> |
5:将第四步的示例程序中的struts-menu应用目录下的images、scripts、styles三个目录中的内容复制到web目录下
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 |
六、实战Struts-Menu
1:使用配置文件实现静态菜单
新建JSP文件web/static-menu.jsp,内容如下:
<%@ page contentType="text/html; charset=GBK" %>< %@ taglib uri="struts-menu" prefix="menu" %>< ?xml:namespace prefix = menu />< menu:useMenuDisplayer bundle="org.apache.struts.action.MESSAGE"> < menu:displayMenu name="DoorSite">< /menu:displayMenu> |
运行Tomcat,在IE地址栏输入http://localhost:8080/mymenu/static-menu.jsp查看
>
【编辑推荐】
- 在Eclipse中开发struts应用程序
- 手把手教你在Eclipse中配置开发Struts
- Eclipse下开发struts完整解决乱码问题
- Struts相关背景介绍
- 使用Easy Struts for Eclipse开发Struts