测试jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ZTreeTest</title>
<link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css"
type="text/css">
<script type="text/javascript" src="js/ztree/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/ztree/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript"
src="js/ztree/jquery.ztree.excheck-3.5.js"></script>
<SCRIPT type="text/javascript">
var setting = {
check: {
enable: true,
autoCheckTrigger: true,
},
data: {
simpleData: {
enable: true
}
}
};
var zNodes = ${obj};
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
});
//获取所有选择的节点
function submitCheckedNodes() {
var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo");
var nodes = new Array();
nodes = zTreeObj.getCheckedNodes(true); //取得选中的结点
var str = "";
for (i = 0; i < nodes.length; i++) {
if (str != "") {
str += ",";
}
str += nodes[i].id;
}
document.getElementById("ids").value = str;//将勾选项id给表单隐藏项
}
</SCRIPT>
</head>
<body>
<div>
<form action="getResult.do">
<input type="hidden" id="ids" name="ids" value="kevin">
<input type="submit" onclick="submitCheckedNodes()" value="submit">
</form>
</div>
<div>
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
</div>
</body>
</html>
controller:
package com.kevin.shiro.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ZTreeController {
@RequestMapping("/ztree")
public ModelAndView ztree() {
ModelAndView model = new ModelAndView();
String obj = "[{ id:11, pId:1, name:\"11\", open:true}," + "{ id:111, pId:11, name:\"111\"},"
+ "{ id:112, pId:11, name:\"112\"}," + "{ id:1111, pId:111, name:\"12\", open:true},"
+ "{ id:121, pId:1, name:\"121\",checked:true}," + "{ id:1, pId:0, name:\" 1\", open:true}" + "];";
model.addObject("obj", obj);
model.setViewName("ztree");
return model;
}
@RequestMapping("/getResult")
public ModelAndView getResult(String ids) {
ModelAndView model = new ModelAndView();
model.addObject("ids", ids);
model.setViewName("getResult");
return model;
}
}
获取的数据显示
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ include file="WEB-INF/jsp/base/base.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ZTreeTest</title>
</head>
<body>
<div>
<c:out value="${ids }"></c:out>
</div>
</body>
</html>