Java Tree的几种开源组件

一、最近需要使用树形展示权限,所以研究了一下Java Tree,其实java中有不少Tree展示,具体有如下几种:

1.dtree

2.ztree(国内的,api全中文,我用这个)

3.JTree

4.dhtmlxTree(国外的,种类齐全),网址为:http://dhtmlx.com/docs/products/dhtmlxTree/index.shtml

##以后是我找的还不错的Tree组件,还有很多!

 

二、我的代码(互联交易平台一版代码,2级树simpleData形式)

1.Action中的代码

	/**
	 * 打开角色授权页面
	 * @return
	 * @author mengxianjun
	 * @throws Exception 
	 * @date 2011-4-12 下午01:30:19
	 */
	public String openImpower() throws Exception {
		try
		{
			employeeRole = employeeRoleService.queryTEmployeeRole(roleNum);
			
			userHavePrivSet = employeeRole.getTEmployeePriv();
			
			search = new ArrayList();
			if(userHavePrivSet != null){
				Iterator it = userHavePrivSet.iterator();
				for(;it.hasNext();){
					TEmployeePriv e = (TEmployeePriv) it.next();
					search.add(e.getPrivNum());
				}
			}
			
			//TEmployeePrivList = privService.queryAllPriv_noPage();//查询出系统中所有的权限
			TEmployeePrivList = privService.queryUnauthorizedPriv_noPage(search);//查询出系统中所有的权限
			
			successPath = "/jsp/priv/role/impower.jsp";
			
			
			/**合成树--开始**/
			List<TMenu> myMenuList = menuDao.queryAll();
			
			List<PrivJSON> jsonList = new ArrayList<PrivJSON>();
			
			for( int i=0; i<myMenuList.size(); i++ )
			{
				PrivJSON json = new PrivJSON();
				
				TMenu menu = myMenuList.get(i);
				
				json.setId(menu.getId().getMenuId());
				json.setpId("0");
				json.setName(menu.getMenuName());
				json.setOpen(true);
				
				jsonList.add(json);
			}
			
			List<TEmployeePriv> myPrivList = privService.queryAllPriv_noPage();
			for( int j=0; j<myPrivList.size();j++ )
			{
				TEmployeePriv priv = myPrivList.get(j);
				PrivJSON json2 = new PrivJSON();
				json2.setId(priv.getPrivNum());
				json2.setpId(priv.getMenuId1());
				json2.setName(priv.getPrivName());
				json2.setPrivNum(priv.getPrivNum());
				
				for(int i=0;i<search.size();i++)
				{
					if( priv.getPrivNum().equals(search.get(i)) )
					{
						json2.setOpen(true);
						json2.setChecked(true);
					}
				}
				
				jsonList.add(json2);
			}
			
			JSONArray myjsonObj = JSONArray.fromObject(jsonList);//将权限集合,转成JSON数组
			this.getRequest().setAttribute("myjsonObj",myjsonObj);
			
			return SUCCESS;
		}
		catch( Exception e )
		{
			log.error("RoleAction's openImpower() a error: "+ e.fillInStackTrace());
			errorPath = "/jsp/error/error.jsp";
			return "error";
		}
	}
	
	/**
	 * 角色授权
	 * @return
	 * @author mengxianjun
	 * @throws Exception
	 * @date 2011-4-12 下午01:27:41
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	public String impower() throws Exception {
		try
		{
			String[] privList = privSelect.split(";"); //获得前台传来的权限Num
			
			employeeRole = employeeRoleService.queryTEmployeeRole(roleNum);
			
			Set<TEmployeePriv> mySet = employeeRole.getTEmployeePriv();
			
			forOldTRprRecord( mySet );//日志前得到旧权限(修改前的权限集合)--授权
			
			mySet.clear();//清空权限
			
			if( null != privList && privList.length>0 )
			{
				for( int i=1; i<privList.length; i++ )
				{
					mySet.add( privService.queryPrivByNum(privList[i]) );
				}
			}
				
			/**
			 * 修改角色的权限
			 */
			employeeRoleService.updateTEmployeeRoleForImpower(employeeRole);
			
			employeeRoleCtrl.loadEmployeeRole();
			rolePrivCtrl.loadRolePriv();
			
			doRolePrivRecord();//记录日志1(修改授权时间、原因)--授权
			
			doRprRecord();//记录日志2(修改前的权限集合)--授权
			
			return queryAllRole();
		}
		catch( Exception e )
		{
			log.error("RoleAction's impower() a error: "+ e.fillInStackTrace());
			errorPath = "/jsp/error/error.jsp";
			return "error";
		}
	}

 

2.js

  <SCRIPT LANGUAGE="JavaScript">
	var zTreeObj;
	var setting = {checkable:true,treeNodeKey:"id",treeNodeParentKey:"pId",isSimpleData:true}; //zTree的参数配置,深入使用请参考 API 文档(参数说明 -- setting 详解)
	/*var zNodes = [
	{name:"test1", open:true, nodes:[
		{name:"test1_1"}, {name:"test1_2"}]},
	{name:"test2", open:true, nodes:[
		{name:"test2_1"}, {name:"test2_2"}]}
	];*/ //zTree的数据属性,深入使用请参考 API 文档(参数说明 -- zTreeNodes 详解)

	var myjsonObj = ${requestScope.myjsonObj};
	var zNodes = myjsonObj;
	                	
	$(document).ready(function(){
		zTreeObj = $("#treeDemo").zTree(setting, zNodes);
	});

	function expandAll(expandSign) {
		zTreeObj.expandAll(expandSign);
	}

	function checkAllTreeNode(checked) {
		var oldNodes = zTreeObj.getNodes();
		var oldcheckNum = zTreeObj.getCheckedNodes(checked).length;
		zTreeObj.checkAllNodes(checked);
		var newcheckNum = zTreeObj.getCheckedNodes(checked).length;
		if (newcheckNum != oldcheckNum) zTreeObj.checkAllNodes(checked);
	}

	/*测试用树授权*/
	function testImpower()
	{
		var temp ="";
		var nodes = zTreeObj.getCheckedNodes();
		for( var i=0; i<nodes.length; i++ ){
			if(nodes[i].privNum != ''){
				temp = temp+";"+nodes[i].privNum;
			}
		}
		alert(temp);
		document.getElementById('privSelect').value=temp;
	}

	/*分配运维权限*/
	function allotYunwei()
	{
		var privNum = ["sys_config","sys_config_a","sys_config_b"];
		for(var i=0;i<privNum.length;i++){
			var node = zTreeObj.getNodeByParam("privNum", privNum[i]);
			node.checked=true;
			zTreeObj.updateNode(node,true);
			//zTreeObj.selectNode(node);//选中指定的节点
		}
	}
  </SCRIPT>

 

3.jsp页面调用

</div>
<div class="zTreeDemoBackground">
<button onclick="expandAll(true);">全展开</button>|<button onclick="expandAll(false);">全关闭</button>
<button onclick="checkAllTreeNode(true);">全选</button>|<button onclick="checkAllTreeNode(false);">全不选</button>
<button onclick="testImpower();">测试</button>
<button onclick="allotYunwei();">运维授权</button><br/><br/>
	<ul id="treeDemo" class="tree"></ul>
</div>

 

 

####具体参见附件,在此留作备忘!!

你可能感兴趣的:(Java Tree)