url:'ResourceServlet?method=loadTree' ,
//第一次不带参数,以后异步加载的时候发送id,以此获取孩子
dnd:true ,//拖拽onDrop:function(target , source , point){
//target表哪个、source表谁被拖了、point表 方式(append、top、bottom)
var tar = $('#t1').tree('getNode' , target);</html>
后台代码
/**
* 加载tree的数据方法
* @param request
* @param response
*/
private void loadTree(HttpServletRequest request,
HttpServletResponse response) {
try {
//获取当前展开的节点id
String id = request.getParameter("id");
List<TreeDTO> treelist = this.rdao.getChildrenByParentId(id);
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(JSONArray.fromObject(treelist).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据父id获取子节点们
*/
public List<TreeDTO> getChildrenByParentId(String id) throws Exception {
Connection conn = DBUtils.createConn();
String sql = "";
if("".equals(id) || id == null){
sql = "select * from resource where parent_id = 999999";
} else {
sql = "select * from resource where parent_id = " + id ;
}
PreparedStatement ps = DBUtils.getPs(conn, sql);
ResultSet rs = ps.executeQuery();
List<Resource> rlist = new ArrayList<Resource>();
while(rs.next()){
Resource r = new Resource();
r.setId(rs.getInt("id"));
r.setIcon(rs.getString("icon"));
r.setChecked(rs.getInt("checked"));
r.setName(rs.getString("name"));
r.setUrl(rs.getString("url"));
r.setParent_id(rs.getInt("parent_id"));
rlist.add(r);
}
List<TreeDTO> tlist = new ArrayList<TreeDTO>();
for (Iterator iterator = rlist.iterator(); iterator.hasNext();) {
Resource resource = (Resource) iterator.next();
TreeDTO tree = new TreeDTO();
tree.setId(resource.getId());
tree.setText(resource.getName());
tree.setChecked(resource.getChecked());
tree.setIconCls(resource.getIcon());
tree.setParent_id(resource.getParent_id());
if(getChildren(resource.getId()).size() > 0){
tree.setState("closed");
} else {
tree.setState("open");
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", resource.getUrl());
tree.setAttributes(map);
tlist.add(tree);
}
return tlist;
}
/**
* 根据pid 获取孩子
* @param pid
* @return
* @throws Exception
*/
public List<Resource> getChildren(int pid) throws Exception{
Connection conn = DBUtils.createConn();
String sql = "select * from resource where parent_id = " + pid;
PreparedStatement ps = DBUtils.getPs(conn, sql);
ResultSet rs = ps.executeQuery();
List<Resource> children = new ArrayList<Resource>();
while(rs.next()){
Resource r = new Resource();
r.setId(rs.getInt("id"));
r.setIcon(rs.getString("icon"));
r.setChecked(rs.getInt("checked"));
r.setName(rs.getString("name"));
r.setUrl(rs.getString("url"));
r.setParent_id(rs.getInt("parent_id"));
children.add(r);
}
return children;
}
/**
* 改变节点的层次结构方法
* @param request
* @param response
*/
private void changeLevel(HttpServletRequest request,
HttpServletResponse response) {
try {
String targetId = request.getParameter("targetId");
String sourceId = request.getParameter("sourceId");
String point = request.getParameter("point");
//得到目标对象
Resource target = this.rdao.findById(Integer.parseInt(targetId));
//得到操作的对象(源对象)
Resource source = this.rdao.findById(Integer.parseInt(sourceId));
// append top bottom
if("append".equals(point)){
source.setParent_id(target.getId());
} else {
source.setParent_id(target.getParent_id());
}
this.rdao.update(source);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 递归删除节点们
* @param id
*/
private void deletenodes(int id) throws Exception{
//获取当前要删除的节点下面所有的孩子
List<Resource> rlist = this.rdao.getChildren(id);
for(int i = 0 ; i < rlist.size();i++){
int cid = rlist.get(i).getId();
this.rdao.delete(cid); //删除操作
deletenodes(cid);
}
this.rdao.delete(id);
}