`

[译]Struts Menu用户指南

阅读更多
官方英文版用户指南(http://struts-menu.sourceforge.net/userguide.html)
改编自 Robin http://www.blogjava.net/fastzch/archive/2005/11/28/21615.html

[译]Struts Menu用户指南

官方英文版用户指南(http://struts-menu.sourceforge.net/userguide.html)
Robin http://www.blogjava.net/fastzch/archive/2005/11/28/21615.html


一、从1.X升级
从Struts Menu 1.x升级到2.X需要做以下事情:
• 改变你的taglib声明中的URI。
<%@ taglib uri="http://struts-menu.sf.net/tag" prefix="menu" %>
• 2、改变<plug-in>,使用新的包名"net.sf.navigator." 。
<plug-in className="net.sf.navigator.menu.MenuPlugIn">
• 3、改变你的menu-config.xml文件,使用新的包名"net.sf.navigator." 。
<Displayer name="Simple"
  type="net.sf.navigator.displayer.SimpleMenuDisplayer"/>

二、快速开始:
• 下载最新的struts-menu版本;
• 解包到本地目录;
• 下载并安装Tomcat或者其它Java  Servlet容器;
• 把struts-menu.war包放入Tomcat安装目录的wabapps目录下,并启动Tomcat服务。
• 打开http://localhost:8080/struts-menu/

三、把Struts Menu整合到你的应用程序中:
Struts Menu能够被轻易的整合到你的Struts应用程序中。(它也可以整合到一个非Struts的应用程序中,但是我不喜欢这样做,所以在此没有提供相应的教程。)这里将一步步的带你整合这个标签库。

把struts-menu.jar拷贝到你的WEB-INF/lib目录下。然后在你的struts-config.xml中设置plug-in settings。
<plug-in className="net.sf.navigator.menu.MenuPlugIn">
  <set-property property="menuConfig" value="/WEB-INF/menu-config.xml"/>
<!-- Default settings -->
</plug-in>
对于Struts Menu 2.1,如果你希望在menu-config.xml中使用EL expressions,你还需要将 Jakarta's Standard Tag Library JAR包放入你的WEB-INF/lib目录下。(http://ibiblio.org/maven/taglibs/jars/standard-1.0.4.jar)
1. 在WEB-INF/menu-config.xml中声名你的菜单属性,下面是一个小片断。
<Menu name="contactMenu" title="Contact" location="?Contact">
  <Item name="email" title="E-Mail" location="?EMail"/>
  <Item name="phone" title="Phone" location="?Phone"/>
</Menu>
更多深入的例子,请参阅 样本程序web/WEB-INF目录下的menu-config文件, 你可以修整它以适应你的需要。完整的属性列表请参见MenuBase class's javadocs(
http://struts-menu.sourceforge.net/apidocs/net/sf/navigator/menu/MenuBase.html)
2. 在你的JSP文件 顶部添加一个 taglib 声明:
<%@ taglib uri="http://struts-menu.sf.net/tag" prefix="menu" %>

3. 在你的JSP文件添加 taglib 代码来实施( render)你的菜单:
<menu:useMenuDisplayer name="TabbedMenu"
  bundle="org.apache.struts.action.MESSAGE">
  <menu:displayMenu name="Home"/>
  <menu:displayMenu name="About"/>
</menu:useMenuDisplayer>
属性name="TabbedMenu"被定义在menu-config.xml文件的顶部:
<Displayer name="TabbedMenu"
   type="net.sf.navigator.displayer.TabbedMenuDisplayer"/>

(译注:其实到此步即可在你的JSP文件中添加相应的菜单了,只不过是没有结合Velocity。在menu-config.xml文件的头部可以定义多个Displayer,每个 Displayer都有name和type属性,name属性与JSP文件中menu:useMenuDisplayer标签的name属性相对应,即表明使用何种样式,具体的样式定义便在type属性中定义,type属性中是一个class。在menu-config.xml文件中的菜单定义中的 name属性则与JSP文件中的menu:displayMenu标签的name属性相对应。)

使用定制的Velocity模版实施你的菜单,你需要整合Velocity到你的WEB应用程序中。如果需要这样做的话,请完成下面的步骤:
1、确定你的menu-config.xml文件有“Velocity”的displayer定义:
<Displayer name="Velocity"
     type="net.sf.navigator.displayer.VelocityMenuDisplayer"/>
2、加入Velocity的JARs包到你的WEB-INF/lib目录中,下载velocity-1.4-rc1.jar(http://www.ibiblio.org/maven/velocity/jars/velocity-1.4.jar)和velocity-tools-view-1.0.jar(http://www.ibiblio.org/maven/velocity-tools/jars/velocity-tools-view-1.0.jar)。
3、将JSP文件中displayer的值改为“Velocity”, “config”属性应当指向一个文件(如config="/templates/tabs.html")或者 可设config="tabs.html"(如果tabs.htm在你的WEB- INF/classes目录中的话)。

这里提供了一些使用Velocity的displayer例子,请参阅sample application(http://demo.raibledesigns.com/struts-menu/index.jsp),AppFuse(http://appfuse.org)。这些例子向你展示了CSS,JavaScript和图像文件,你的菜单总是会用到的。下面有一些在当前的Struts Menu中用到的Velocity模版的例子的链接:

CoolMenus Demo http://demo.raibledesigns.com/struts-menu/velocity-coolmenu4.jsp
Template http://struts-menu.sourceforge.net/templates/coolmenus.html
CSS Menu Demo http://demo.raibledesigns.com/struts-menu/cssHorizontal.jsp
Template http://struts-menu.sourceforge.net/templates/cssMenu.html
NiceTabs Demo http://demo.raibledesigns.com/struts-menu/velocity-nicetabs.jsp?Home
Template http://struts-menu.sourceforge.net/templates/nicetabs.html
Tabs Demo http://demo.raibledesigns.com/struts-menu/velocity-tabs.jsp?Home
Template http://struts-menu.sourceforge.net/templates/tabs.html
XTree Demo http://demo.raibledesigns.com/struts-menu/velocity-xtree.jsp
Template http://struts-menu.sourceforge.net/templates/xtree.html


所有相关的有用的文件如果你需要的话都可以在下面的站点上下载:
Images (http://struts-menu.sourceforge.net/menu-images/)
Stylesheets (http://struts-menu.sourceforge.net/styles/)
Scripts (http://struts-menu.sourceforge.net/scripts/)
Templates (http://struts-menu.sourceforge.net/templates/)

更多的基于roles的显示/隐藏菜单的信息,请查看FAQs(http://struts-menu.sourceforge.net/faq.html)。

四、在Struts之外使用Struts Menu:
在2.2+ 版中,Menu Repository能够使用MenuContextListener载入:

<!--
   - Loads the menu-config.xml for struts-menu at startup,
   - by default from "/WEB-INF/menu-config.xml".
   - To override this, add a context-param named "menuConfigLocation"
   - web.xml file.
   -->
<listener>
    <listener-class>net.sf.navigator.menu.MenuContextListener</listener-class>
</listener>
或者如果你使用Spring,甚至更容易。仅仅需要加入下面的部分到你的applicationContext.xml文件中:
<bean id="menu" class="net.sf.navigator.menu.MenuLoader">
  <property name="menuConfig" value="/WEB-INF/menu-config.xml"/> <-- default and optional -->
</bean>

感谢Dan Luputan提供MenuLoader类的源代码。

五、从源文件编译:
要从源文件编译这个项目,执行下面的步骤:
1、下载并安装Maven(http://maven.apache.org/);
2、创建一个环境变量MAVEN_HOME指出你的Maven的安装目录,然后添加$MAVEN_HOME/bin到你的PATH变量中;
3、操纵这个目录你可以扩展源代码,执行“maven.jar”创建target/struts-menu.jar。

要deploy struts-menu例程,需要下面的步骤:
1、下载和安装Tomcat;
2、创建一个环境变量CATALINA_HOME指出你的Tomcat的安装目录;
3、执行“maven deploy”把应用程序展开到Tomcat中;
4、打开http://localhost:8080/struts-menu在你喜爱的浏览器中。

如果你喜欢使用Eclipse开发项目,请参考开发向导(http://struts-menu.sourceforge.net/devguide.html)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics