参考:http://blog.csdn.net/liangbinny/article/details/42291037
例子下载地址:
http://download.csdn.net/detail/liangbinny/8321459
项目结构图
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 启用注解 -->
<context:annotation-config />
<context:component-scan base-package="com.ztree" />
<!--开启了之后它给AnnotationMethodHandlerAdapter初始化7个转换器,
可以通过调用AnnotationMethodHandlerAdapter类的
@ResponseBody @RequestBody的使用需要打开这个
-->
<mvc:annotation-driven/>
<context:component-scan base-package="com.ztree.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/ztree/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注意:springmvc传递和返回对象,除了
<mvc:annotation-driven/>
要打开这个以外,项目还需要引入jackson-all-1.7.6.jar,jackson-core-asl-1.9.13.jar,jackson-mapper-asl-1.9.13.jar这三个jar包,不引入的话,项目不会报错,但传递不了对象,有点坑。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>600</session-timeout>
</session-config>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
//out.print(path);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ztree</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="<%=path %>/zTreedemo/css/demo.css" type="text/css">
<link rel="stylesheet" href="<%=path %>/zTreedemo/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="<%=path %>/zTreedemo/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="<%=path %>/zTreedemo/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=path %>/zTreedemo/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript">
var setting = {
check: {
enable: true,
nocheckInherit: true
},
data: {
simpleData: {
enable: true
}
}
};
/**
var zNodes =[
{ id:1, pId:0, name:"随意勾选 1", open:true},
{ id:11, pId:1, name:"随意勾选 1-1", open:true},
{ id:111, pId:11, name:"无 checkbox 1-1-1", nocheck:true},
{ id:112, pId:11, name:"随意勾选 1-1-2"},
{ id:12, pId:1, name:"无 checkbox 1-2", nocheck:true, open:true},
{ id:121, pId:12, name:"无 checkbox 1-2-1"},
{ id:122, pId:12, name:"无 checkbox 1-2-2"},
{ id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
{ id:21, pId:2, name:"随意勾选 2-1"},
{ id:22, pId:2, name:"随意勾选 2-2", open:true},
{ id:221, pId:22, name:"随意勾选 2-2-1", checked:true},
{ id:222, pId:22, name:"随意勾选 2-2-2"},
{ id:23, pId:2, name:"随意勾选 2-3"}
];
*/
var zNodes =[
{id:1,pId:0, nocheck:false, name:"随意勾选 1", open:true},
{id:11,pId:1, nocheck:false, name:"随意勾选 1-1", open:true},
{id:111,pId:11, nocheck:true, name:"无 checkbox 1-1-1", open:false}
];
function nocheckNode(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nocheck = e.data.nocheck,
nodes = zTree.getSelectedNodes();
if (nodes.length == 0) {
alert("请先选择一个节点");
}
for (var i=0, l=nodes.length; i<l; i++) {
nodes[i].nocheck = nocheck;
zTree.updateNode(nodes[i]);
}
}
$(document).ready(function(){
$.ajax({
type: "post",
url: "showData.do",
data: {},
dataType: "json",
success: function(data){
$.fn.zTree.init($("#treeDemo"), setting, data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
});
$("#nocheckTrue").bind("click", {nocheck: true}, nocheckNode);
$("#nocheckFalse").bind("click", {nocheck: false}, nocheckNode);
});
$(function(){
});
</script>
</head>
<body>
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
<div>
<p>试试看:<br/>
[ <a id="nocheckTrue" href="#" title="set checkbox to be hidden." οnclick="return false;">隐藏</a> ]
[ <a id="nocheckFalse" href="#" title="set checkbox to be show." οnclick="return false;">显示</a> ]
</div>
</body>
</html>
package com.ztree.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ztree.vo.TreeNode;
@Controller
@RequestMapping(value="/ztree")
public class ShowController {
@RequestMapping(value="/showData", method = RequestMethod.POST)
@ResponseBody
public List<TreeNode> showData(HttpSession session)throws Exception{
System.err.println(".........++++++++");
return getList();
}
public List<TreeNode> getList(){
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(new TreeNode("1","0","随意勾选 1",false,false,false));
list.add(new TreeNode("11","1","随意勾选 1-1",true,false,false));
list.add(new TreeNode("111","11","无 checkbox 1-1-1",false,true,false));
list.add(new TreeNode("112","11","随意勾选 1-1-2",false,false,true));
list.add(new TreeNode("12","1","无 checkbox 1-2",true,true,false));
list.add(new TreeNode("121","12","无 checkbox 1-2-1",false,true,false));
list.add(new TreeNode("122","12","无 checkbox 1-2-2",false,true,false));
//上一层都不能nocheck,open的话,那子层肯定也不能open和check了,所以父层要设置false,如果设置true,那子层设置就没意义
list.add(new TreeNode("2","0","随意勾选 2",true,false,false));
list.add(new TreeNode("21","2","随意勾选 2-1",false,false,false));
list.add(new TreeNode("22","2","随意勾选 2-2",false,false,false));
list.add(new TreeNode("221","22","随意勾选 2-2-1",false,false,false));
list.add(new TreeNode("222","22","随意勾选 2-2-2",false,false,false));
list.add(new TreeNode("23","2","随意勾选 2-3",false,false,false));
return list;
}
}
TreeNode.java
package com.ztree.vo;
import java.io.Serializable;
public class TreeNode implements Serializable{
private static final long serialVersionUID = -766759489880382042L;
private String id;
private String pId;
private String name;
private boolean open; //是否默认打开
private boolean nocheck; //是否可以选
private boolean checked; //是否默认选上
public TreeNode() {
super();
}
public TreeNode(String id, String pId, String name, boolean open, boolean nocheck, boolean checked) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.open = open;
this.nocheck = nocheck;
this.checked = checked;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean isNocheck() {
return nocheck;
}
public void setNocheck(boolean nocheck) {
this.nocheck = nocheck;
}
}
运行图