jsp頁面的代碼以下所示: 數組
<td class="label" valign="top">
<fmt:message key="authority.cname.title"/>:
</td>
<c:forEach items="${authoritys}" var="current" varStatus="i">
<td>
<form:checkbox path="userName" value="${current.id}" checked="checked"/>
${current.CName}
</td>
</c:forEach>
<c:forEach items="${authorities}" var="cur" varStatus="j">
<td>
<form:checkbox path="userName" value="${cur.id}"/>
${cur.CName}
</td>
</c:forEach> app
紅色的項表示用戶已有的權限,利用循環和checked="checked"在每行加上使這些權限項的前面畫勾,綠色的代碼表示用戶沒有的權限,利用循環將這些項顯示出來。 jsp
control層的代碼以下所示: this
/*
* edit an existing Authority entity
*/
@RequestMapping("/updateAuthority")
public ModelAndView updateAuthority(@RequestParam Integer idKey)
{
ModelAndView mav=new ModelAndView();
User user=systemManageService.findUserById(idKey);
Set<Authority> sets=systemManageService.findAllAuthorities();
mav.addObject("user", user);
mav.addObject("users", shareService.getUsersMap());
mav.addObject("authoritys", user.getAuthorities());
mav.addObject("authorities",systemManageService.findOtherAuthorities(idKey));
mav.addObject("size", sets.size());
mav.setViewName("_operate/updateAuthority.jsp");
return mav;
} spa
service層以下所示: orm
/*
* 查找全部的權限信息
*/
@Transactional
public Set<Authority> findAllAuthorities()
{
return authorityDAO.findAllAuthoritys();
} rem
/*
* 查找用戶沒有的權限
*/
public List<Authority> findOtherAuthorities(Integer idKey)
{
Set<Authority> sets=this.findAllAuthorities();//獲得全部的權限數據;
User user=userDAO.findUserByPrimaryKey(idKey);
List<Authority> list=new ArrayList<Authority>();//建立一個空的權限的數組;
list.addAll(sets);
Set<Authority> userauth=user.getAuthorities();//獲得用戶現有的權限;
List<Authority> list2=new ArrayList<Authority>();//建立一個空的權限的數組;
list2.addAll(userauth);//將一個set的authority數組轉換成一個list型的;
for(int i=0;i<list2.size();i++)
{
Authority auth=list2.get(i);
int id=auth.getId();
for(Authority authority:sets)
{
if(authority.getId()==id)
{
list.remove(authority);
}
}
}
return list;
} get
頁面顯示以下圖所示: it