一、簡(jiǎn)介
Struts-Menu是一組從基于XML的配置文件中生成多種樣式的菜單的JSP Tags,并且可以結(jié)合API開(kāi)發(fā)通過(guò)數(shù)據(jù)庫(kù)生成的動(dòng)態(tài)菜單。Struts-Menu支持國(guó)際化和多種權(quán)限控制。 二、運(yùn)行環(huán)境 三、下載與安裝 四、運(yùn)行示例程序 五、安裝與配置 <?xml version="1.0" encoding="ISO-8859-1"?>
3:解壓縮struts-1.2.7.zip,將壓縮包中的lib目錄下所有的8個(gè)jar釋放到lib目錄中,將5個(gè)tld文件釋放到web\WEB-INF目錄中,在web\WEB-INF目錄中新建struts-config.xml,內(nèi)容如下:<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java./dtd/web-app_2_3.dtd"> <web-app> <display-name>My Example Application -- Vinton Lee</display-name> <!-- ============= 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> <load-on-startup>1</load-on-startup> </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-uri> <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-uri> <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-location> </taglib> <!-- ================================================================== --> </web-app> <?xml version="1.0" encoding="ISO-8859-1" ?>
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的文檔中沒(méi)有看到需要這個(gè)包,但是沒(méi)有這個(gè)包卻無(wú)法成功加載。在web\WEB-INF目錄中新建menu-config.xml,內(nèi)容如下:<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts./dtds/struts-config_1_2.dtd"> <struts-config> <!-- ========== 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 property="menuConfig" value="/WEB-INF/menu-config.xml"/> </plug-in> </struts-config> <?xml version="1.0" encoding="UTF-8" ?>
5:將第四步的示例程序中的struts-menu應(yīng)用目錄下的images、scripts、styles三個(gè)目錄中的內(nèi)容復(fù)制到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."/> <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:你的目錄結(jié)構(gòu)應(yīng)該類似如下: %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 六、實(shí)戰(zhàn)Struts-Menu <%@ page contentType="text/html; charset=GBK" %>
運(yùn)行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:實(shí)現(xiàn)中文化 > 在src\application_zh_CN.properties中增加下面的內(nèi)容,Unicode可以通過(guò)JDK自帶的native2ascii工具得到: #門(mén)戶網(wǎng)站
> 在src\application.properties中增加下面的內(nèi)容:menu.DoorSite=\u95e8\u6237\u7f51\u7ad9 #雅虎 menu.Yahoo=\u96c5\u864e #雅虎首頁(yè) menu.YahooIndex=\u96c5\u864e\u9996\u9875 #雅虎郵件 menu.YahooMail=\u96c5\u864e\u90ae\u4ef6 #搜狐 menu.Sohu=\u641c\u72d0 #新浪 menu.Sina=\u65b0\u6d6a #門(mén)戶網(wǎng)站
> 修改menu-config.xml文件<Menus></Menus>部分:menu.DoorSite=DoorSite #雅虎 menu.Yahoo=Yahoo #雅虎首頁(yè) menu.YahooIndex=Yahoo Index #雅虎郵件 menu.YahooMail=Yahoo Mail #搜狐 menu.Sohu=Sohu #新浪 menu.Sina=Sina <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."/> <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:結(jié)合數(shù)據(jù)庫(kù)實(shí)現(xiàn)動(dòng)態(tài)菜單 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() > 插入測(cè)試數(shù)據(jù): ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() > 新建一個(gè)Struts的Action,代碼如下: package cn.appex.menu;
> 將struts-config.xml文件的<action-mappings />部分修改為:import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.ListResourceBundle; import java.util.Map; import com.mysql.jdbc.Driver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.navigator.menu.MenuComponent; import net.sf.navigator.menu.MenuRepository; import net.sf.navigator.menu.PermissionsAdapter; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class MenuAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { buildMenuRepository(request); buildMenuResourceBundle(request); buildMenuPermissions(request); return (mapping.findForward("success")); } /** * 創(chuàng)建菜單數(shù)據(jù)結(jié)構(gòu) * * @param request */ private void buildMenuRepository(HttpServletRequest request) { MenuRepository repository = new MenuRepository(); // Get the repository from the application scope - and copy the // DisplayerMappings from it. MenuRepository defaultRepository = (MenuRepository) request .getSession().getServletContext().getAttribute( MenuRepository.MENU_REPOSITORY_KEY); repository.setDisplayers(defaultRepository.getDisplayers()); Map[] menus = getMenuComponents(); for (int i=0; i < menus.length; i++) { MenuComponent mc = new MenuComponent(); Map row = menus[i]; String name = (String) row.get("name"); mc.setName(name); String parent = (String) row.get("parent_name"); 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.get("title"); mc.setTitle(title); String location = (String) row.get("location"); mc.setLocation(location); String description = (String) row.get("description"); mc.setDescription(description); repository.addMenu(mc); } request.setAttribute("examplesRepository", repository); } /** * 從數(shù)據(jù)庫(kù)中讀取菜單配置信息 * * @return */ private Map[] getMenuComponents() { ArrayList list = new ArrayList(); Connection conn = null; PreparedStatement pstmt = null; ResultSet rest = null; String sql = "select name,parent_name,title,location,description from menu_item order by id"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8"); pstmt = conn.prepareStatement(sql); rest = pstmt.executeQuery(); while (rest.next()) { int i = 1; HashMap map = new HashMap(); map.put("name", rest.getString(i++)); map.put("parent_name", rest.getString(i++)); map.put("title", rest.getString(i++)); map.put("location", rest.getString(i++)); map.put("description", rest.getString(i++)); list.add(map); } } catch (SQLException ex) { ex.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (null!=rest) rest.close(); if (null!=pstmt) pstmt.close(); if (null!=conn) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return (Map[]) list.toArray(new HashMap[0]); } /** * 構(gòu)造菜單權(quán)限 * * @param request */ private void buildMenuPermissions(HttpServletRequest request) { PermissionsAdapter permession = new PermissionsAdapter() { public boolean isAllowed(MenuComponent menu) { // 名稱等于StandaloneMenu的菜單不顯示 return !"StandaloneMenu".equalsIgnoreCase(menu.getName()); } }; request.setAttribute("examplesPermession", permession); } /** * 構(gòu)造菜單顯示標(biāo)題 * * @param request */ private void buildMenuResourceBundle(HttpServletRequest request) { MenuResourceBundle resourceBundle = new MenuResourceBundle(); request.setAttribute("examplesBundle", resourceBundle); } /** * MenuResourceBundle樹(shù)狀菜單國(guó)際語(yǔ)言顯示 * * @author wenbin.zhang * */ class MenuResourceBundle extends ListResourceBundle { private ArrayList list = new ArrayList(); public MenuResourceBundle() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rest = null; String sql = "select title,titleCN from menu_item order by id"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8"); pstmt = conn.prepareStatement(sql); rest = pstmt.executeQuery(); while (rest.next()) { int i = 1; String[] message = new String[2]; message[0] = rest.getString(i++); try { message[1] = new String(rest.getString(i++).getBytes("latin1"), "gbk"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (message[0] != null && message[1] != null) { list.add(message); } } } catch (SQLException ex) { ex.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (null!=rest) rest.close(); if (null!=pstmt) pstmt.close(); if (null!=conn) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public Object[][] getContents() { return (String[][]) list.toArray(new String[0][0]); } } } <action-mappings>
> 新建JSP文件web/dynamic-menu.jsp,內(nèi)容如下:<action path="/menuAction" type="cn.appex.menu.MenuAction" > <forward name="success" path="/struts-menu/dynamic-menu.jsp" /> </action> </action-mappings> ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 七、關(guān)于Struts-Menu配置文件和API的使用方法,請(qǐng)參考附錄中的官方網(wǎng)站
|
|
來(lái)自: funronglei > 《我的圖書(shū)館》