最近写个项目,要弄选择用户的JS目录树,用到三个表,来动态生成json。以下是源代码。
public class Tree{
Hashtable<Integer, Integer> departTable=new Hashtable<Integer, Integer>();
public void sendUserListJson() throws IOException {
HttpServletResponse res=ServletActionContext.getResponse();
HttpServletRequest req=ServletActionContext.getRequest();
res.setCharacterEncoding("utf-8");
String id=req.getParameter("id");
System.out.println("============"+id+"====================");
List branchList=branchService.getBranchs();
PrintWriter pwt=res.getWriter();
String json="[";
/**
* 以下是加载机构信息目录
*/
if(Integer.parseInt(id)==0){
for( i=1;i<=branchList.size();++i){
branchInfo=(BranchInfo)branchList.get(i-1);
if(i==branchList.size()){
json+="{id:'"+i+"',fristId:'0',text:'"+branchInfo.getBranchName()+"',cls:'folder'}";
}
else{
json+="{id:'"+i+"',fristId:'0',text:'"+branchInfo.getBranchName()+"',cls:'folder'},";
}
}
json+="]";
pwt.print(json);
}
/**
* 结构信息加载完毕
*/
/**
* 加载部门信息
*/
if(Integer.parseInt(id)<=branchList.size()&&Integer.parseInt(id)!=0){
branchInfo= (BranchInfo) branchService.getBranchs().get(Integer.parseInt(id)-1);
System.out.println(branchInfo.getBranchName());
Set departSet=branchInfo.getDepartinfos();
List departList=new ArrayList<Object>(departSet);
for(int j=1;j<=departList.size();j++){
departInfo=(DepartInfo)departList.get(j-1);
departTable.put(i, departInfo.getDepartId());
System.out.println("哈希表保存 id="+i+"值为"+departInfo.getDepartId());
if(j==departList.size()){
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+departInfo.getDepartName()+"',cls:'folder'}";
}
else{
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+departInfo.getDepartName()+"',cls:'folder'},";
}
i++;
}
json+="]";
pwt.print(json);
}
/**
* 加载部门信息加载完毕
*/
/**
* 加载员工信息
*/
if(Integer.parseInt(id)>branchList.size()){
departInfo=departService.getDepart(departTable.get(Integer.parseInt(id)));
Set userSet=departInfo.getUserinfos();
List userList=new ArrayList(userSet);
for(int k=1;k<=userList.size();++k){
userInfo=(UserInfo) userList.get(k-1);
if(k==userList.size()){
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+userInfo.getUserName()+"',cls:'file',leaf:'"+true+"'}";
}
else{
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+userInfo.getUserName()+"',cls:'file',leaf:'"+true+"'},";
}
i++;
}
json+="]";
pwt.print(json);
}
/**
* 员工信息加载完毕
*
*/
System.out.println(json);
}
}
tree.js
Ext.onReady(function(){
//定义树的跟节点
var root=new Ext.tree.AsyncTreeNode({
id:"0",
text:"选择用户"
});
var treePanel=new Ext.tree.TreePanel({
border:true,
width:185.5,
height:400,
root:root,
rootVisible:true,
containerScroll:true,
renderTo:"usertree",
autoScroll:true,//显示滚动条
animate : true,//true表示使用动画展开/折叠
enableDD : true,//允许拖放
containerScroll : true,//登记本容器ScrollManager
listeners:{//在加载之前的一个监听事件
'beforeload':function(node){
alert(node.id);
node.loader=new Ext.tree.TreeLoader({//树节点的数据来源
url:"/myOffice/tree!sendUserListJson.action",//请求的路径
baseParams:{//请求参数
id:node.id
}
});
}
}
});
treePanel.on('click', function(node){
alert(node.id);
});
})
public class Tree{
Hashtable<Integer, Integer> departTable=new Hashtable<Integer, Integer>();
public void sendUserListJson() throws IOException {
HttpServletResponse res=ServletActionContext.getResponse();
HttpServletRequest req=ServletActionContext.getRequest();
res.setCharacterEncoding("utf-8");
String id=req.getParameter("id");
System.out.println("============"+id+"====================");
List branchList=branchService.getBranchs();
PrintWriter pwt=res.getWriter();
String json="[";
/**
* 以下是加载机构信息目录
*/
if(Integer.parseInt(id)==0){
for( i=1;i<=branchList.size();++i){
branchInfo=(BranchInfo)branchList.get(i-1);
if(i==branchList.size()){
json+="{id:'"+i+"',fristId:'0',text:'"+branchInfo.getBranchName()+"',cls:'folder'}";
}
else{
json+="{id:'"+i+"',fristId:'0',text:'"+branchInfo.getBranchName()+"',cls:'folder'},";
}
}
json+="]";
pwt.print(json);
}
/**
* 结构信息加载完毕
*/
/**
* 加载部门信息
*/
if(Integer.parseInt(id)<=branchList.size()&&Integer.parseInt(id)!=0){
branchInfo= (BranchInfo) branchService.getBranchs().get(Integer.parseInt(id)-1);
System.out.println(branchInfo.getBranchName());
Set departSet=branchInfo.getDepartinfos();
List departList=new ArrayList<Object>(departSet);
for(int j=1;j<=departList.size();j++){
departInfo=(DepartInfo)departList.get(j-1);
departTable.put(i, departInfo.getDepartId());
System.out.println("哈希表保存 id="+i+"值为"+departInfo.getDepartId());
if(j==departList.size()){
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+departInfo.getDepartName()+"',cls:'folder'}";
}
else{
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+departInfo.getDepartName()+"',cls:'folder'},";
}
i++;
}
json+="]";
pwt.print(json);
}
/**
* 加载部门信息加载完毕
*/
/**
* 加载员工信息
*/
if(Integer.parseInt(id)>branchList.size()){
departInfo=departService.getDepart(departTable.get(Integer.parseInt(id)));
Set userSet=departInfo.getUserinfos();
List userList=new ArrayList(userSet);
for(int k=1;k<=userList.size();++k){
userInfo=(UserInfo) userList.get(k-1);
if(k==userList.size()){
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+userInfo.getUserName()+"',cls:'file',leaf:'"+true+"'}";
}
else{
json+="{id:'"+i+"',fristId:'"+id+"',text:'"+userInfo.getUserName()+"',cls:'file',leaf:'"+true+"'},";
}
i++;
}
json+="]";
pwt.print(json);
}
/**
* 员工信息加载完毕
*
*/
System.out.println(json);
}
}
tree.js
Ext.onReady(function(){
//定义树的跟节点
var root=new Ext.tree.AsyncTreeNode({
id:"0",
text:"选择用户"
});
var treePanel=new Ext.tree.TreePanel({
border:true,
width:185.5,
height:400,
root:root,
rootVisible:true,
containerScroll:true,
renderTo:"usertree",
autoScroll:true,//显示滚动条
animate : true,//true表示使用动画展开/折叠
enableDD : true,//允许拖放
containerScroll : true,//登记本容器ScrollManager
listeners:{//在加载之前的一个监听事件
'beforeload':function(node){
alert(node.id);
node.loader=new Ext.tree.TreeLoader({//树节点的数据来源
url:"/myOffice/tree!sendUserListJson.action",//请求的路径
baseParams:{//请求参数
id:node.id
}
});
}
}
});
treePanel.on('click', function(node){
alert(node.id);
});
})