在Struts項目中使用Struts-menu- -
在Struts作為表示層框架的應用中,我們可以使用其眾多的Struts plug-ins來豐富其功能。大家使用比較多的是使用Struts-Tiles、Struts-Validator,這里我給大家介紹一下Struts-menu,很好用,在沒有權限控制時最簡單,只需配置文件就可以了;在要菜單權限控制時,可以和Tomcat(或其他J2EE容器)的認證機制結合實現權限控制,也可以利用后臺數據庫的方式實現權限控制,這里主要介紹數據庫方式,比較靈活,本人一直以為容器驗證的方式太死板,不易擴充。
Struts-menu是sourceforge的一個項目,可以直接到sourceforge.net下載。下面介紹一下在Struts項目中如何集成Struts-menu。
方式一:先介紹沒有權限控制的方式使用:
1. 將 /lib/struts-menu.jar 拷貝到 WEB-INF/lib/目錄下;
2. 將 /lib/struts-menu.tld拷貝到WEB-INF/lib/目錄下;
3. 將struts-menu.war中的scripts、styles、images目錄拷貝到你的應用"/"目錄下
4. 修改 web.xml 加入 taglib 的調用
/WEB-INF/struts-menu.tld /WEB-INF/struts-menu.tld |
5. 修改 struts-config.xml 加入 plugin描述
6. 配置menu-config.xml,如:
type="net.sf.navigator.displayer.DropDownMenuDisplayer"/> type="net.sf.navigator.displayer.SimpleMenuDisplayer"/> type="net.sf.navigator.displayer.CoolMenuDisplayer"/> type="net.sf.navigator.displayer.CoolMenuDisplayer4"/> type="net.sf.navigator.example.PermissionsFormMenuDisplayer"/> type="net.sf.navigator.displayer.ListMenuDisplayer"/> type="net.sf.navigator.displayer.TabbedMenuDisplayer"/> type="net.sf.navigator.displayer.VelocityMenuDisplayer"/> toolTip="Shows usage of the menu displayers using defaults." page="/menutest1.jsp"/> toolTip="Shows customized menu displays." page="/menutest2.jsp"/> toolTip="Shows menu using CoolMenu3" page="/coolmenu1.jsp"/> toolTip="Shows menu using CoolMenu3 with forms." page="/coolmenu2.jsp"/> toolTip="Example use of menu disable and permission checking with CoolMenus." page="/permissionsForm.jsp"/> toolTip="Shows menu using CoolMenu4" page="/coolmenu4.jsp"/> toolTip="Shows Expandable DHTML list" page="/dhtmlExpandable.jsp"/> toolTip="Shows Dropdown DHTML list" page="/dhtmlDropdown.jsp"/> |
說明:斷描述了Strut-menu將采用的顯示方式,可以在jsp文件中選擇Displayer; 進行了相關菜單項目的配置,可以在jsp文件中選擇顯示的菜單。
7. 在jsp文件(如Struts-tiles中使用了menu.jsp)中加入
可以通過對useMenuDisplayer來設置菜單的顯示風格。
8. 可以在瀏覽其中show啦!
方式二:數據庫驗證方式
基本配置與方式一一致,只是在JSP中動態構建Menus。
采用數據庫驗證的方式,后臺數據與你的其他數據庫驗證菜單類似。下面的示例代碼中假設從后臺數據中得到權限列表ArrayList:res,我下面的示例中的res是在Action中傳入的SQLExecutor的結果SQLResults(其實就是ArrayList); Import Struts 的類庫:net.sf.navigator.menu.*。
菜單顯示部分如下:
MenuRepository repository = new MenuRepository(); for (int i=0; i < res.getRowCount(); i++) { |
這樣的調用,詳細用法可以參考Struts-menu,當然有興趣的朋友可以把以上代碼包裝成特定的Javabeans,方便靈活調用。
玩了一陣struts-menu,總體感覺他的xml配置和權限處理,是我最喜歡的部分。但是他在頁面展示上不夠靈活,這是他的缺點。
這里推薦一個簡單配置權限的方式。就是在樓主介紹的方法一的基礎上,利用struts-menu adpater的概念,只要在jsp上加上如下代碼:
<% <menu:useMenuDisplayer name="TestDropDownDisplayer" |
其中MenuBO取出的是權限允許的菜單名字。
DropDownPermissionAdapter 實現了如下:
public class DropDownPermissionAdapter implements PermissionsAdapter{ /** this.menuTree =ts; public boolean isAllowed(MenuComponent menu) { if (menuTree == null || menuTree.contains(menu.getName())) { |
文章來源于領測軟件測試網 http://www.kjueaiud.com/