Struts-menu 权限控制

 

基本配置我就不多说了,网上多的很,我这里只说一下具体实现,呵呵
 
采用 Struts+Hibernate
一、 新建菜单表:表根据配置文件自己建吧,我这里就不写了
二、 建立表对应的 Hibernate 的配置文件及 JAVABEAN
<? xml version = "1.0" encoding = "utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
    < class name = "com.wz.hibernate.Menu" table = "menu" catalog = "gknews" >
        < id name = "id" type = "java.lang.Integer" >
            < column name = "id" />
            < generator class = "native" />
        </ id >
        < property name = "parent" type = "java.lang.String" >
            < column name = "parent" not-null = "true" />
        </ property >
        < property name = "name" type = "java.lang.String" >
             < column name = "name" length = "45" not-null = "true" />
        </ property >
        < property name = "target" type = "java.lang.String" >
            < column name = "target" length = "45" not-null = "true" />
        </ property >
    </ class >
</ hibernate-mapping >
package com.wz.hibernate;
 
public class Menu  implements java.io.Serializable {
 
     private Integer id ;
     private String parent ;
     private String name ;
     private String target ;
 
    /** default constructor */
    public Menu() {
    }
    /** full constructor */
    public Menu(String parent, String name, String target) {
        this . parent = parent;
        this . name = name;
        this . target = target;
    }  
    // Property accessors
    public Integer getId() {
        return this . id ;
    }   
    public void setId(Integer id) {
        this . id = id;
    }
    public String getParent() {
        return this . parent ;
    }   
    public void setParentId(String parent) {
        this . parent = parent;
    }
    public String getName() {
        return this . name ;
    }   
    public void setName(String name) {
        this . name = name;
    }
    public String getTarget() {
        return this . target ;
    }   
    public void setTarget(String target) {
        this . target = target;
}
}
三、 ActionForm
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.wz.struts_menu.struts.form;
 
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class TestMenuForm extends ActionForm {
  
   private String parent;
   private String title;
   private String target;
   private Integer id;
   public ActionErrors validate(ActionMapping mapping,
           HttpServletRequest request) {
       // TODO Auto-generated method stub
       return null;
   }
   public void reset(ActionMapping mapping, HttpServletRequest request) {
       // TODO Auto-generated method stub
   }
   public String getParent() {
       return parent;
   }
   public void setParentId(String parent) {
       this.parent = parent;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public String getTarget() {
       return target;
   }
   public void setTarget(String target) {
       this.target = target;
   }
   public Integer getId() {
       return id;
   }
   public void setId(Integer id) {
       this.id = id;
   }
}
四、 Action
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.wz.struts_menu.struts.action;
 
import java.util.List;
import javax.servlet.ServletContext;
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 org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import com.wz.hibernate.Menu;
import com.wz.hibernate.MenuItem;
import com.wz.hibernate.SessionFactory;
import com.wz.struts_menu.struts.form.TestMenuForm;
public class TestMenuAction extends Action {
  
   public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) {
       TestMenuForm testMenuForm = (TestMenuForm) form;// TODO Auto-generated method stub
      
       Session session=SessionFactory.getSession();
       // 创建事务
       Transaction tx=session.beginTransaction();
       // 创建对话
       Query query=session.createQuery("FROM Menu m order by id");
       List list=query.list();
       // 事务提交
       tx.commit();
      
       if(list.size()<0)
           return mapping.getInputForward();
      
       MenuRepository repository = new MenuRepository();
      
       HttpSession httpsession=(HttpSession)request.getSession();
      
       ServletContext application=(ServletContext)httpsession.getServletContext();
      
       MenuRepository defaultRepository = (MenuRepository)application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
      
       repository.setDisplayers(defaultRepository.getDisplayers());
      
        for (int i=0; i < list.size(); i++) {
           
            MenuComponent mc = new MenuComponent();
           
            Menu mi=(Menu) list.get(i);
           
            String name = mi.getName();    
            mc.setName(name);
           
            String parent = (String) mi.getParentId();
           
            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)mi.getName();
            mc.setTitle(title);
            String location = (String) mi.getTarget();
            mc.setLocation(location);
            repository.addMenu(mc);
       }
        request.setAttribute("repository", repository);
       return mapping.findForward("okGo");
   }  
}
 
五、 JSP
<%@ page contentType = "text/html;charset=UTF-8" language = "java" %>
<%@ taglib uri = "/WEB-INF/struts-menu.tld" prefix = "menu" %>
<%@ taglib uri = "/WEB-INF/struts-menu-el.tld" prefix = "menu-el" %>
<%@ taglib uri = "/WEB-INF/c.tld" prefix = "c" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en" >
< head >
    < title > Dynamic, Database-driven Menu </ title >
    < link rel = "stylesheet" type = "text/css" media = "all"
        href = "<c:urlvalue="/styles/menuExpandable.css"/>"/>
    < script type = "text/javascript"
        src = "<c:urlvalue="/scripts/menuExpandable.js"/>"></script>
    < link rel = "stylesheet" type = "text/css" media = "all" href = "<c:urlvalue="/styles/xtree.css"/>"/>
    < script type = "text/javascript" src = "<c:urlalue="/scripts/xtree.js"/>"></script>
    < link rel = "stylesheet" type = "text/css" media = "all" href = "<c:urlvalue="/styles/global.css"/>"/>
    < script type = "text/javascript" >
         /* Function for showing and hiding elements that use 'display:none' to hide */
        function toggleDisplay(targetId) {
            if (document.getElementById) {
                target = document.getElementById(targetId);
                if (target.style.display == "none" ) {
                    target.style.display = "" ;
                } else {
                    target.style.display = "none" ;
                }
            }
        }
    </ script >
</ head >
< body >
    < div class = "dynamicMenu" >
        < menu:useMenuDisplayer name = "ListMenu" repository = "repository" >
            < menu:displayMenu name = " 新浪" />
            < menu:displayMenu name = " 网易" />
        </ menu:useMenuDisplayer >
    </ div >
</ body >
</ html >
 
六、Struts-config.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
 
< struts-config >
  < data-sources />
  < form-beans />
  < global-exceptions />
  < global-forwards >    
    < forward name = "okGo" path = "/ok.jsp" />  
  </ global-forwards >
  < action-mappings >
    < action path = "/testAction" type = "com.wz.struts_menu.struts.action.TestMenuAction" />
  </ action-mappings >
  < message-resources parameter = "com.wz.struts_menu.struts.ApplicationResources" />
 
  < plug-in className = "net.sf.navigator.menu.MenuPlugIn" >
    < set-property property = "menuConfig" value = "/WEB-INF/menu-config.xml" />
  </ plug-in >
</ struts-config >
 
这样就算完成了,呵呵;学习中 ……
 
上一编 的基础上做如下工作即可:

一、新建一个类,用于获取允许显示的菜单列表
package com.wz.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.wz.hibernate.Menu;
import com.wz.hibernate.SessionFactory;
public class TestMenu {
 
 
 public List getList(){
  
  List list = new ArrayList();
  Session session = SessionFactory.getSession();
  Transaction tx = session.beginTransaction();
  
  try {
   
   list = session.createQuery("from Menu order by id").list();
   tx.commit();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(null != session) session.close();
  }
  return list;
 }
 public static List getAllowed(String menuName){
    
  Session session = SessionFactory.getSession();
  Transaction tx = session.beginTransaction();
  List list = new ArrayList();
  try {
   
   Query query = session.createQuery("from Menu where name like ?");
   query.setParameter(0, "%"+menuName+"%");
   list = query.list();
   
  } catch (Exception e) {
   
   e.printStackTrace();
  }finally{
   if(null != session) session.close();
  }
  return list;
 }
 
 public static void main(String[] args) {
  
  TestMenu tm = new TestMenu();
  List ts = tm.getAllowed("
新浪 ");
  Iterator it = ts.iterator();
  while(it.hasNext()){
   
   Menu menu = (Menu)it.next();
   System.out.println(menu.getName());  
   
  }
 }
}

二、修改原 JSP
<body>
<%
 TestPermissionAdapter test = new TestPermissionAdapter();
 List list = TestMenu.getAllowed("
网易 ");
 test.initializeMenu(list);
 request.getSession().getServletContext().setAttribute("AtdMenuAdapter", test);
 %>
    <div class="dynamicMenu">
        <menu:useMenuDisplayer name="ListMenu" repository="repository" permissions="AtdMenuAdapter">
            <menu:displayMenu name="
新浪 "/>
            <menu:displayMenu name="
网易 "/>
        </menu:useMenuDisplayer>
    </div>
</body>

好了,就现到这里吧,学习中 ......

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值