页面代码:
<!-- 这个是显示的多选框页面 已有的角色要打勾,可以重新选择角色-->
<form action="<c:url value="/sys/userRolesEdit.htm"/>" method="post" >
<table class="grid" width="100%" cellspacing="1">
<tr>
<td colspan="6" class="title">
角色列表
</td>
</tr>
<tr>
<td class="column"></td>
<td class="column">角色ID</td>
<td class="column">角色名</td>
<td class="column">角色代码</td>
<td class="column">角色描述</td>
</tr>
<!-- 循环显示列表userRoles的每一项,并判断如果数据库中已有的打勾<c:if test标签正确才执行中间的代码-->
<c:forEach items="${roles}" var="item">
<tr>
<td>
<input type="checkbox" name="rolesId" <c:forEach items="${userRoles}" var = "selectedRoles">
<c:if test="${item.srolId == selectedRoles.srolId}">checked="true"</c:if></c:forEach>
value="<c:out value="${item.srolId}"/>" />
</td>
<td><c:out value="${item.srolId}"/></td>
<td><c:out value="${item.srolName}"/> </td>
<td><c:out value="${item.srolCode}"/></td>
<td><c:out value="${item.srolDesc}"/></td>
<tr>
</c:forEach>
<tr>
<td colspan="6" class="nav">
<input type="hidden" name="userID"
value=<c:out value="${userInfo.suinId}"/>>
<input type="submit" class="button3" value="提交"/>
<INPUT type="button" class="button2" value="返回" onclick="history.back()" />
</td>
</tr>
</table>
</form>
控制器代码:
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Map result = new HashMap();
SysUserinfo userInfo = null;
String id = request.getParameter("userID");
System.out.println(id);
if (id != null && !id.equals(""))
{
userInfo = (SysUserinfo) sysService.get(SysUserinfo.class,
new Long(id));
}
else
{
}
result.put("userInfo", userInfo);
result.put("roles", sysService.getSupportData("SysRole", null));
if (userInfo != null)
{
result.put("userRoles", sysService.getUserRoles(userInfo));
}
SysUserRole sysUserRole = null;
SysRole sysRole = null;
String roleId = null;
// 记录父列表ID
result.put("parentlistID", request.getParameter("parentlistID"));
result.put("userInfo", userInfo);
//遍历所有map里的key
Iterator it = result.keySet().iterator();
while (it.hasNext())
{
String key = (String) it.next();
request.setAttribute(key, result.get(key));
}
//这里就是从列表多选框传过来的数值。根据name属性获得所有的value
String[] rolesId = null;
rolesId = request.getParameterValues("rolesId");
System.out.println("------------------------" + rolesId + "-------------------");
if (rolesId == null || rolesId.equals(null))
{
return new ModelAndView(formView);
}
else
{
List userRoles = (List)sysService
.getUsersUserRoles(userInfo.getSuinId());
//先删除数据库中所有的,再插入所选的值
for (int k = 0; k < userRoles.size(); k++)
{
SysUserRole userRole = (SysUserRole)userRoles.get(k);
sysService.delete(userRole);
}
for (int i = 0; i < rolesId.length; i++)
{
roleId = rolesId[i];
sysRole = (SysRole)sysService.get(SysRole.class, new Long(roleId));
sysUserRole = new SysUserRole();
sysUserRole.setUser(userInfo);
sysUserRole.setRole(sysRole);
sysService.save(sysUserRole);
}
//如果在###-servlet.xml配置文件中,successView不能传递参数,可在这里直接有response导向别的页面
response.sendRedirect(request.getContextPath()
+ "/sys/userRolesList.htm?userID=" + userInfo.getSuinId().toString());
return null;
}
}
posted on 2006-10-25 09:27
lovetq 阅读(1777)
评论(0) 编辑 收藏