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>
红色的项表示用户已有的权限,利用循环和checked="checked"在每行加上使这些权限项的前面画勾,绿色的代码表示用户没有的权限,利用循环将这些项显示出来。
control层的代码如下所示:
/*
* 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;
}
service层如下所示:
/*
* 查找所有的权限信息
*/
@Transactional
public Set<Authority> findAllAuthorities()
{
return authorityDAO.findAllAuthoritys();
}
/*
* 查找用户没有的权限
*/
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;
}
页面显示如下图所示:
![](http://static.oschina.net/uploads/space/2012/1217/161608_O4QQ_659230.png)