在上一编的基础上做如下工作即可:
一、新建一个类,用于获取允许显示的菜单列表
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>
好了,就现到这里吧,学习中......