本文執行個體為大家分享了zTree樹形插件異步加載,Struts2架構,供大家參考,具體内容如下
異步加載
上面呢,是jsp頁面的代碼,因為是純插件測試,可以直接複制過去使用的,
action層代碼,可以直接複制使用
list=null;
try {
list=ztreeService.getMenusById(ztree);
} catch (Exception e) {
e.printStackTrace();
}
super.writeJson(list);
}
getMenusById(Ztree ztree) throws Exception;
//接口
//service代碼,幾本都可以複制使用
package com.jk.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.jk.dao.ZtreeDao;
import com.jk.model.Ztree;
import com.jk.service.ZtreeService;
@Repository("ztreeService")
public class ZtreeServiceImpl implements ZtreeService {
@Autowired
private ZtreeDao ztreeDao;
@Override
public List queryList(Ztree ztree) throws Exception {
String hql=" from Ztree where 1=1 ";
Map params = new HashMap();
//傳的對象是否為空,拼接sql+id
if(ztree.getId()!=null){
hql+=" and id = :id ";
params.put("id",ztree.getId());
}
return ztreeDao.queryList(hql,params);
}
//遞歸查詢
@Override
public ListgetMenusById(Ztree ztree) throws Exception {
String hql=" from Ztree where 1=1 ";
Map params=new HashMap();
if(ztree.getId()==null||ztree.getId().equals("")){
hql+=" and pid is null ";
}else{
hql+= " and pid = :id ";
params.put("id",ztree.getId());
}
ListqueryZtree = ztreeDao.queryList(hql,params);
Listlist=new ArrayList();
for(Ztree ztree2:queryZtree){
if(queryBoolean(ztree2)){
ztree2.setIsParent("true");
}
list.add(ztree2);
}
return list;
}
//熟悉遞歸的朋友應該可以看懂,我就不解釋了,我也聊不清楚這個遞歸,不誤導了,代碼在這,可以複制進去直接運作,
public boolean queryBoolean(Ztree ztree) throws Exception{
boolean flag=false;
String hql= " from Ztree where pid = :id ";
Map();
params.put("id",ztree.getId());
List queryZtree = ztreeDao.queryList(hql,params);
if(queryZtree!=null&&queryZtree.size()>0){
flag=true;
}
return flag;
}
}