java接口代码
/**
* 权限分配
*
* @param roleId
* [角色ID]
* @param clientId
* [客户ID]
* @return 菜单的权限列表
*/
@SuppressWarnings("unchecked")
public List privilegList(Integer roleId, Integer clientId);
/**
* 菜单的下一层
*
* @param parentId
* [父节点的ID]
* @return 列表
*/
@SuppressWarnings("unchecked")
public List nextList(Integer parentId);
java实现代码
@SuppressWarnings("unchecked")
public List privilegList() {
String hql = null;
try {
hql = "from Privilege t where t.parentId is null or t.parentId = 0 order by t.id";
} catch (Exception e) {
e.printStackTrace();
logger.error("出错位置[RolesImpl.privilegList]", e);
}
return find(hql);
}
@SuppressWarnings("unchecked")
public List nextList(Integer parentId) {
return find("from Privilege t where t.parentId = ?",
new Object[] { parentId });
}
model代码
public class Privilege implements Serializable {
/**
* Privilege
*/
private static final long serialVersionUID = 6991113246687701165L;
private Integer id;
private String name_en;
private String name_tw;
private String name_cn;
private String name_jp;
private Integer parentId;
private boolean selected;
private boolean selected2;
@SuppressWarnings("unchecked")
private Set privilegeSet;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName_en() {
return name_en;
}
public void setName_en(String name_en) {
this.name_en = name_en;
}
public String getName_tw() {
return name_tw;
}
public void setName_tw(String name_tw) {
this.name_tw = name_tw;
}
public String getName_cn() {
return name_cn;
}
public void setName_cn(String name_cn) {
this.name_cn = name_cn;
}
public String getName_jp() {
return name_jp;
}
public void setName_jp(String name_jp) {
this.name_jp = name_jp;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
@SuppressWarnings("unchecked")
public Set getPrivilegeSet() {
return privilegeSet;
}
@SuppressWarnings("unchecked")
public void setPrivilegeSet(Set privilegeSet) {
this.privilegeSet = privilegeSet;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public boolean isSelected2() {
return selected2;
}
public void setSelected2(boolean selected2) {
this.selected2 = selected2;
}
}
Managed Bean代码:
package com.aexperm.bean.system;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
import com.aexperm.dao.system.RolesIfc;
import com.aexperm.model.Client;
import com.aexperm.model.Privilege;
import com.aexperm.model.RolePrivilege;
import com.aexperm.model.Roles;
import com.aexperm.model.Users;
import com.aexperm.system.AppBean;
import com.aexperm.system.Constants;
import com.aexperm.utils.common.FacesUtil;
public class RolesBean {
private static Logger logger = Logger.getLogger(RolesBean.class);
private RolesIfc roleService;
private Roles roles;
@SuppressWarnings("unchecked")
private TreeNode privilegeTree;
public RolesIfc getRoleService() {
return roleService;
}
public void setRoleService(RolesIfc roleService) {
this.roleService = roleService;
}
public Roles getRoles() {
try {
if (roles == null) {
init();
}
} catch (Exception e) {
e.printStackTrace();
}
return roles;
}
public void setRoles(Roles roles) {
this.roles = roles;
}
@SuppressWarnings("unchecked")
public TreeNode getPrivilegeTree() {
try {
if (privilegeTree == null) {
privilegeTree = new TreeNodeImpl();
List<Privilege> pri = roleService.privilegList(); // 主菜单
Iterator iter = pri.iterator();
while (iter.hasNext()) {
Privilege pri_v = (Privilege) iter.next();
TreeNode node = createTreeNode(pri_v);
node.setData(pri_v);
privilegeTree.addChild(pri_v.getId(), node);
}
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("生成权限树出错", e);
}
return privilegeTree;
}
@SuppressWarnings("unchecked")
public void setPrivilegeTree(TreeNode privilegeTree) {
this.privilegeTree = privilegeTree;
}
@SuppressWarnings("unchecked")
public TreeNode createTreeNode(Privilege pri_v) {
TreeNode parNode = null;
try {
List<Privilege> list = roleService.nextList(pri_v.getId());
parNode = new TreeNodeImpl();
parNode.setData(pri_v);
if (!list.isEmpty() && list.size() > 0) {
for (Privilege pri : list) {
TreeNode node = createTreeNode(pri);
if (node != null) {
parNode.addChild(pri.getId(), node);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("递归权限树出错[RolesBean.createTreeNode].", e);
}
return parNode;
}
//复选框迭代选中像
@SuppressWarnings( { "unchecked" })
public void addSelect(Integer rolesId, TreeNode rootNode) {
try {
if (rootNode != null) {
Iterator itr = rootNode.getChildren();
while (itr.hasNext()) {
TreeNode node = (TreeNode) ((Entry) itr.next()).getValue();
Privilege childPri = (Privilege) node.getData();
if (childPri.isSelected()) {// 选中
logger.info(childPri.getName_cn() + ": "+ childPri.isSelected());
}
addSelect(rolesId, node);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
页面代码
<script language="javascript" type="text/javascript">
jQuery.noConflict();//与JSF搭配需要这句话,不然会有冲突
jQuery(document).ready(function() {
// Add click event listener to each checkbox in the tree page
// Note! Using this simple selector assumes that there are no other
// checkboxes on the page, if there are other checkboxes then
// selector should be changed
jQuery(":checkbox").click(function(){
updateChildren(this);
});
});
function updateChildren(currentCheckBox)
{
// Get state of current checkbox (true or false)
var state = currentCheckBox.checked;
// Get parent TABLE, where current checkbox is places
var parentTables = jQuery(currentCheckBox).parents("table");
var parentTable = parentTables[0];
// Get DIV where child nodes with checkboxes are situated
// See http://docs.jquery.com/Traversing/ to get better uderstanding of
// parents() and next()
var childDivs = jQuery(parentTable).next("div");
var childDiv = childDivs[0];
// Iterate over all child nodes checkboxes and set same state as the
// current checkbox state
jQuery(childDiv).contents().find(":checkbox").each(function() {
this.checked = state;
});
}
</script>
<rich:panel id="prilivigePanel" style="width:270px;">
<f:facet name="header">
<t:outputText value="#{rolesVar['roles.privilege']}"/>
</f:facet>
<rich:tree id="privilegetree" switchType="client" value="#{rolesbean.privilegeTree}" var="item" style="width:120px">
<rich:treeNode id="tree">
<h:selectBooleanCheckbox id="pri" value="#{item.selected}"/>
<h:outputText value="#{item.name}"/>
</rich:treeNode>
</rich:tree>
</rich:panel>
注:不明白地方请加群73624154,谢谢!