1. 程式人生 > >044 根據權限動態展示菜單 - bos

044 根據權限動態展示菜單 - bos

ide urn equal distinct one hql err find etl


1.修改index.jsp頁面中ajax請求的url為functionAction_findMenu.action,請求菜單數據

2.實現FunctionAction.findMenu方法

/**
* 根據當前登錄的用戶查詢對應的菜單數據,返回json
* @return
*/
public String findMenu(){
List<Function> functionList = functionService.findMenu();
list2JsonAndWriteResponse(functionList, "parentFunction" , "roles", "children");
return NONE;
}

3.實現FunctionService.findMenu

@Override
public List<Function> findMenu() {
List<Function> functionList = null;
User user = BOSUtils.getLoginUser();
if(user.getUsername().equals("admin")){
//如果是內置用戶,則查詢所有菜單
functionList = functionDao.findAllMenu();
}else{
//其他用戶,根據用戶ID查詢菜單
functionList = functionDao.findMenuByUserId(user.getId());
}
return functionList;
}

4.實現FunctionDao.findAllMenu();

/**
* 查詢所有菜單
*/
@Override
public List<Function> findAllMenu() {
String hql = "from Function f where f.generatemenu = ‘1‘ ORDER BY f.zindex";
@SuppressWarnings("unchecked")
List<Function> list = (List<Function>) getHibernateTemplate().find(hql);
return list;
}

5.實現FunctionDao.findMenuByUserId

@Override
public List<Function> findMenuByUserId(String id) {
//涉及5張表的關聯查詢,使用hql由於省略的中間表,剩下3個類的關聯查詢
String hql = "SELECT DISTINCT f FROM Function f LEFT OUTER JOIN f.roles r LEFT OUTER JOIN r.users u "
+ " WHERE u.id=? AND f.generatemenu = ‘1‘ "
+ "ORDER BY f.zindex";
@SuppressWarnings("unchecked")
List<Function> find = (List<Function>) getHibernateTemplate().find(hql, id);
return find;
}

044 根據權限動態展示菜單 - bos