原创作者: melin
阅读:2919次
评论:0条
更新时间:2011-06-01
在前一个项目当中要根据登入用户的权限进行菜单显示:我把其中相关的代码提炼出来.
在系统启动时把菜单信息加载近来.通过扩展ActionServlet进行加载.
数据库表中一级菜单的roles值按2.4.8.16....增加 这样与用户的roles值进行位运算.只要值为1,就可以显示该菜单.
显示菜单:
在系统启动时把菜单信息加载近来.通过扩展ActionServlet进行加载.
/** *加载菜单静态信息 */ private void loadMenuRepository() { MenuConstructor menuConstructor=new MenuConstructor(); menuConstructor.createMenuRepository(); }
数据库表中一级菜单的roles值按2.4.8.16....增加 这样与用户的roles值进行位运算.只要值为1,就可以显示该菜单.
/** * 加载系统菜单入菜单库 * @param defaultRepository :缺省库 */ public void createMenuRepository() { ServletContext app=ApplicationContext.getContext(); MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY); MenuRepository repository = new MenuRepository(); repository.setDisplayers(defaultRepository.getDisplayers()); List list = getMenuComponents(); for (int i=0; i < list.size(); i++) { MenuComponent mc = new MenuComponent(); MenuInfo menu=(MenuInfo) list.get(i); String menuName = menu.getName(); mc.setName(menuName); String parentName = (String) menu.getParentName(); if (parentName != null) { MenuComponent parentMenu = repository.getMenu(parentName); if (parentMenu == null) { parentMenu = new MenuComponent(); parentMenu.setName(parentName); repository.addMenu(parentMenu); } mc.setParent(parentMenu); } String title = (String)menu.getTitle(); String location = (String) menu.getLocation(); String target = (String) menu.getTarget(); String description = (String) menu.getDescription(); String action=(String)menu.getAction(); String roles=(String)menu.getRoles(); mc.setTitle(title); mc.setLocation(location); mc.setAction(action); mc.setTarget(target); mc.setDescription(description); mc.setRoles(roles); repository.addMenu(mc); } constant.setMenuRepository(repository); //因为加载出来的菜单信息被打乱了. //按照库表菜单顺序保存一级菜单信息,在struts-menu显示时要用 app.setAttribute("OrderMenuList",getOrderMenuList()); } /** * 从数据库中读取菜单信息 * @return List :菜单信息 */ private static List getMenuComponents() { PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService"); String hql=QlConstructor.getSqlContent("getMenuInfo"); List menuInfoList = service.list(hql); return menuInfoList; } /** * 将菜单表中的一级菜单按照显示顺序取出来 * @return List:一级菜单信息 */ private static List getOrderMenuList() { List orderMenuList=new ArrayList(); PersistenceService service = (PersistenceService)ApplicationContextProvider.getBean("persistenceService"); String hql=QlConstructor.getSqlContent("getParentmenuSql"); orderMenuList = service.list(hql); return orderMenuList; }
/** * 根据userId获取用户所能看到的菜单信息 * @param authSet :用户权限集 * @return MenuRepository : 用户所能看到的菜单库 */ public MenuRepository getUserMenuRepository(long authSet) { Constant constant=Constant.getInstance(); //获取系统所有菜单 ServletContext app=ApplicationContext.getContext(); MenuRepository defaultRepository = (MenuRepository)app.getAttribute(MenuRepository.MENU_REPOSITORY_KEY); MenuRepository menuRepository=constant.getMenuRepository(); MenuRepository userMenuRepository=new MenuRepository(); userMenuRepository.setDisplayers(defaultRepository.getDisplayers()); Set menuSet=menuRepository.getMenuNames(); Iterator itor=menuSet.iterator(); MenuComponent menuComponent =null; //查找满足用户权限的菜单项 while(itor.hasNext()) { String menuName=(String)itor.next(); menuComponent = menuRepository.getMenu(menuName); long roles=Long.parseLong(menuComponent.getRoles()); if((roles&authSet)!=0) { userMenuRepository.addMenu(menuComponent); } } //TODO:下面生成用户所能看到的菜单 userMenuRepository=menuRepository; return userMenuRepository; } /** * 获取用户所能看到的一级菜单列表,在画面展示时struts-menu标签需要用到 * @param userMenuRepository :用户自己的菜单库 * @return List: 用户能看到的一级菜单名称数组 */ public List getParentMenuNameList(MenuRepository userMenuRepository) throws Exception { ServletContext app=ApplicationContext.getContext(); List orderMenuList=(List)app.getAttribute("OrderMenuList"); List parentMenuNameList=new ArrayList(); for(int i=0;i<orderMenuList.size();i++) { String menuName=((MenuInfo)orderMenuList.get(i)).getName(); if(userMenuRepository.getMenu(menuName)!=null) parentMenuNameList.add(menuName); } return parentMenuNameList; }
显示菜单:
<menu:useMenuDisplayer name="Velocity" config="/framework/res/menucomp/templates/xtree.html" bundle="org.apache.struts.action.MESSAGE" repository="userMenuRepository" permissions="menuPermession"> <logic:iterate id="menuName" name="userParentMenuNameList" indexId="index"> <menu:displayMenu name='<%=menuName.toString()%>' /> </logic:iterate> </menu:useMenuDisplayer>
评论 共 0 条 请登录后发表评论