1.接着上一篇来,上一篇我们写好了基本的要求,还需要加上的有回显数据的方法,我们需要知道其实在页面传递的都是id显示的是id对应实体的name,所以想要回显就是需要得到privilegeIds,如果像上一篇在jsp利用checkbox来显示的话,虽然能显示但是不方便不好换行,显示树状结构也很麻烦,所以我们改善一下利用自己在jsp中写一段代码来实现同样的功能,代码Jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>配置权限</title>
<%@ include file="/WEB-INF/jsp/public/common.jspf" %>
<script language="javascript" src="${pageContext.request.contextPath}script/jquery_treeview/jquery.treeview.js"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}style/blue/file.css" />
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}script/jquery_treeview/jquery.treeview.css" />
<script type="text/javascript">
$(function{
$("#root").treeview();
})
</script>
</head>
<body>
<!-- 标题显示 -->
<div id="Title_bar">
<div id="Title_bar_Head">
<div id="Title_Head"></div>
<div id="Title"><!--页面标题-->
<img border="0" width="13" height="13" src="${pageContext.request.contextPath}style/images/title_arrow.gif"/> 配置权限
</div>
<div id="Title_End"></div>
</div>
</div>
<!--显示表单内容-->
<div id=MainArea>
<s:form action="roleAction_setPrivilege">
<s:hidden name="id"></s:hidden>
<div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
<img border="0" width="4" height="7" src="${pageContext.request.contextPath}style/blue/images/item_point.gif" /> 正在为【${role.name}】配置权限 </div>
</div>
<!-- 表单内容显示 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<table cellpadding="0" cellspacing="0" class="mainForm">
<!--表头-->
<thead>
<tr align="LEFT" valign="MIDDLE" id="TableTitle">
<td width="300px" style="padding-left: 7px;">
<!-- 如果把全选元素的id指定为selectAll,并且有函数selectAll(),就会有错。因为有一种用法:可以直接用id引用元素 -->
<input type="CHECKBOX" id="cbSelectAll" onClick="selectAll(this.checked)"/>
<label for="cbSelectAll">全选</label>
</td>
</tr>
</thead>
<!--显示数据列表-->
<tbody id="TableData">
<tr class="TableDetail1">
<!-- 显示权限树 -->
<td>
<!--
<s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue="name"></s:checkboxlist>
-->
<span style="color:#ff0000;"><s:iterator value="#privilegeList">
<input type="checkbox" value="${id}" id="cb_${id}" name="privilegeIds"
<s:property value="id in privilegeIds ? 'checked' : '' " />
>
<label for="cb_${id}">${name}</label><br/>
</s:iterator></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="image" src="${pageContext.request.contextPath}style/images/save.png"/>
<a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}style/images/goBack.png"/></a>
</div>
</s:form>
</div>
<div class="Description">
说明:<br />
1,选中一个权限时:<br />
a,应该选中 他的所有直系上级。<br />
b,应该选中他的所有直系下级。<br />
2,取消选择一个权限时:<br />
a,应该取消选择 他的所有直系下级。<br />
b,如果同级的权限都是未选择状态,就应该取消选中他的直接上级,并递归向上做这个操作。<br />
3,全选/取消全选。<br />
4,默认选中当前岗位已有的权限。<br />
</div>
</body>
</html>
解释:红色是改善过的代码
① 首先是利用struts2的标签将PrivilegeList迭代出来,因为在后台我们是将它放在Map中所以在前面加一个#代表从map中取出
② 然后写常用的input checkbox标签,将需要显示的name动态显示出来,其中的label标签是为了完成点击名字也能选中的功能,为什么id写成cb_${id}是因为循环一次id必须变化,不能一样cb随便写在加一个动态的id就可以实现点击名字也能选中的效果
③ 再然后input标签中的value放的是id,跟checkboxlist的listkey对应的,而下面的name跟listValue对应的,上面的name跟checkboxlist的name对应最后在input中又加入的 struts2的标签因为他是在服务器端解释的,所以在页面我们只看到结果,这个属性的作用是判断当前这个id在所有的权限id中是不是存在如果存在就选中不存在返回空,所 以有的话在页面前面就会回显也就是有勾号,因为我们在设置权限的时候我们点一下他是传过去权限的id了,然后保存到数据库中,所以在回显的时候只需要比较下有么有然后利用checked属性选中就可以实现回显功能了。
④ 经过改进后效果如下
2.给出对应的后台改进过的最终代码:
RoleAction
package com.icss.oa.view.action;
import java.util.HashSet;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.icss.oa.base.BaseAction;
import com.icss.oa.domain.Privilege;
import com.icss.oa.domain.Role;
import com.icss.oa.service.PrivilegeService;
import com.icss.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {
private Long[] privilegeIds;
public Long[] getPrivilegeIds() {
return privilegeIds;
}
public void setPrivilegeIds(Long[] privilegeIds) {
this.privilegeIds = privilegeIds;
}
//列表方法
public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}
//删除方法
public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
}
//增加页面方法
public String addUI() throws Exception {
return "addUI";
}
//增加方法
public String add() throws Exception {
//为页面参数设值
// Role role=new Role();
//role.setName(role.getName());
//role.setDescription(role.getDescription());
//保存到数据库
roleService.save(model);
return "toList";
}
//修改页面方法
public String editUI() throws Exception {
//根据id得到role对象的一条信息并显示
Role role1 = roleService.getById(model.getId());
//在edit页面显示数据
//this.name=role.getName();
//this.description=role.getDescription();
ActionContext.getContext().getValueStack().push(role1);
return "editUI";
}
//修改方法
public String edit() throws Exception {
//设置需要修改的值
Role role2= roleService.getById(model.getId());
role2.setName(model.getName());
role2.setDescription(model.getDescription());
//update到数据库中
roleService.update(role2);
return "toList";
}
//设置权限页面方法
public String setPrivilegeUI() throws Exception {
//准备显示的数据
Role role=roleService.getById(model.getId());
ActionContext.getContext().put("role", role);
List<Privilege> privilegeList=privilegeService.findAll();
ActionContext.getContext().put("privilegeList", privilegeList);
// 准备回显的数据
privilegeIds = new Long[role.getPrivileges().size()];
int index = 0;
for (Privilege privilege : role.getPrivileges()) {
privilegeIds[index++] = privilege.getId();
}
return "setPrivilegeUI";
}
//设置权限方法
public String setPrivilege() throws Exception {
//从数据库中取出源对象
Role role=roleService.getById(model.getId());
//设置需要修改的属性
List<Privilege> privileges= privilegeService.getByIds(privilegeIds);
role.setPrivileges(new HashSet<Privilege>(privileges));
//更新到数据库中
roleService.update(role);
return "toList";
}
}
3.明天给出树状结构的笔记,具体是利用了Jquery官方的一个做好的通用的TreeView来实现的。