ztree异步加载

时间:2021-07-27 04:09:27

Ztree异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载。

直接贴代码(SpringMvc+Mybatis):

ztree异步加载ztree异步加载

前台页面ztreeList.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ztree</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css"
    href="<%=request.getContextPath()%>/css/zTreeStyle.css">
<script type="text/javascript"
    src="<%=request.getContextPath()%>/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript"
    src="<%=request.getContextPath()%>/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
    src="<%=request.getContextPath()%>/js/jquery.ztree.excheck.min.js"></script>
<script type="text/javascript">
   /**
    *异步加载的意思就是: 当点击展开树节点时,
    *才去请求后台action返回点击节点的子节点
    *数据并加载。
    */
    var setting = {
        data : {
            key:{
                name:"catalogName"
            },
            simpleData : {
                enable : true,
                idKey:"catalogId",
                pIdKey:"parentId",
            }
        },
        async : {
            enable : true,
            url : "ztree/list.ht",
            autoParam : [ "catalogId", "catalogName" ],
            dataFilter : filter
        //异步返回后经过Filter
        },
        callback : {
            // beforeAsync: zTreeBeforeAsync, // 异步加载事件之前得到相应信息
            OnAsyncSuccess : zTreeOnAsyncSuccess,//异步加载成功的fun
            aOnAsyncError : zTreeOnAsyncError, //加载错误的fun
            beforeClick : beforeClick,//捕获单击节点之前的事件回调函数
            onRightClick: zTreeOnRightClick
        }
    };
    //treeId是treeDemo,异步加载完之后走这个方法
    function filter(treeId, parentNode, childNodes) {
        if (!childNodes)
            return null;
        childNodes = eval(childNodes);
        return childNodes;
    }
    //点击节点触发事件
    function beforeClick(treeId, treeNode) {
        if (!treeNode.isParent) {
            alert("请选择父节点");
            return false;
        } else {
            return true;
        }
    }
    //异步加载失败走该方法
    function zTreeOnAsyncError(event, treeId, treeNode) {
        alert("异步加载失败!");
    }
    //异步加载成功走该方法
    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {

    }
    //右击事件
    function zTreeOnRightClick(){
        alert("asdas");
    }
    /**********当你点击父节点是,会异步访问controller,把id传过去*************/
    var zNodes = [];
    $(document).ready(function() {
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
</script>
</head>
<body>
    <div style="height:600px; width:98%">
        <div style="height:600px; width:20%; border:1px solid #999999; float:left">
            <ul id="treeDemo" class="ztree"></ul>
        </div>
        <div style="height:600px; width:79.5%; border:1px solid #999999; float:left; overflow:auto">
            <iframe id="testIframe" name="testIframe" frameborder=0 scrolling=auto width=90%  height=580px src="ztree/welcome.ht"></iframe>
        </div>
    </div>
</body>
</html>

后台逻辑处理ZtreeController.java:

package com.henu.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.henu.model.Ztree;
import com.henu.service.ZtreeService;

@Controller
@RequestMapping("/ztree/")
public class ZtreeController {

    @Resource
    private ZtreeService ztreeService;

    @RequestMapping("page")
    public String page(){
        return "ztree/ztreeList";
    }

    @RequestMapping("welcome")
    public String welcome(){
        return "ztree/welcome";
    }

    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String catalogIdParam = request.getParameter("catalogId");
        Long catalogId = null;
        if (catalogIdParam != null) {
            catalogId = Long.parseLong(catalogIdParam);
        }
        String catalogName = request.getParameter("catalogName");
        System.out.println(catalogId + "|" + catalogName + "|");
        List<Ztree> ztreeList = ztreeService.getZtree(catalogId);
        PrintWriter out = response.getWriter();
        String str = JSONArray.fromObject(ztreeList).toString();
        System.out.println(str);
        out.print(str);
    }
}

Service层ZtreeService.java:

package com.henu.service;

import java.util.List;

import com.henu.model.Ztree;

public interface ZtreeService {

    List<Ztree> getZtree(Long catalogId);

}

ServiceImpl层ZtreeServiceImpl.java:

package com.henu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.henu.dao.ZtreeDAO;
import com.henu.model.Ztree;
import com.henu.service.ZtreeService;

@Service
public class ZtreeServiceImpl implements ZtreeService{

    @Resource
    private ZtreeDAO ztreeDAO;

    public List<Ztree> getZtree(Long catalogId) {
        return ztreeDAO.getZtree(catalogId);
    }
}

持久层ZtreeDAO:

package com.henu.dao;

import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

import com.henu.model.Ztree;

@Component
public class ZtreeDAO {

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    public List<Ztree> getZtree(Long catalogId) {
        List<Ztree> ztreeList = sqlSessionTemplate.selectList("com.henu.model.Ztree.getZtree",catalogId);
        return ztreeList;
    }

}

Ztree.map.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="com.henu.mapping.userMapper"就是com.henu.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
 -->
<mapper namespace="com.henu.model.Ztree">
    <select id="getZtree" resultType="com.henu.model.Ztree">
        select * from henu_catalog where 1=1
        <if test="_parameter!=null and _parameter!='' "> 
            and parentId = #{_parameter,jdbcType=NUMERIC}
        </if>
        <if test="_parameter == null "> 
            and parentId = 1
        </if>
        order by catalogId
    </select>
</mapper>

application-context.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:property-placeholder location="classpath:*.properties"/>

    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.henu" />
    <!-- 开启注解 -->
    <mvc:annotation-driven />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driverClassName}"/>
       <property name="url" value="${jdbc.url}" />
       <property name="username" value="${jdbc.username}" />
       <property name="password" value="${jdbc.password}" />

       <property name="maxActive" value="20" />
       <property name="initialSize" value="5" />
       <property name="maxWait" value="60000" />
       <property name="maxIdle" value="14"/>
       <property name="minIdle" value="5" />
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/henu/mapping/*.xml"></property>
    </bean>  

    <!-- 配置sqlsession 产生这个实例就是通过 sqlsessionTemplate来实现的 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>  

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
        <aop:pointcut id="allServiceMethod" expression="(execution(* com.henu.*.service.impl.*.*(..))) or (execution (* com.henu.*.*.service.impl.*.*(..)))"/>
        <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
    </aop:config>
</beans>

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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.henu" />

    <!-- 开启注解 -->
    <mvc:annotation-driven />

    <!-- 静态资源(js/image)的访问 -->
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>

    <!-- 定义视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射   处理ajax请求加入-->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0"/>
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
            </list>
        </property>
    </bean>

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="10485760000" />
        <property name="maxInMemorySize" value="40960" />
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
        <welcome-file>ztree/page</welcome-file>
    </welcome-file-list>

    <!-- 加载所有的配置文件,如果配置文件放在src下面则用classpath:application-context.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/application-context.xml</param-value>
    </context-param>

    <!-- 配置Spring监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置SpringMVC -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.ht</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/ztree/page</url-pattern>
    </servlet-mapping>

    <!-- 配置字符集 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置Session -->
<!--     <filter>
        <filter-name>openSession</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSession</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->
</web-app>

ztree异步加载的更多相关文章

  1. zTree 异步加载

    zTree异步加载数据的简单实现,更为详细的Api请参考 zTree官网   http://www.treejs.cn/ <link href="~/Content/ztree/css ...

  2. jquery zTree异步加载的例子

    下面是使用zTree异步加载的一个例子: 1)初始化树的时候是ajax请求,返回nodes列表来初始化树的:如果一开始就异步的话,$.fn.zTree.init($("#zTree&quot ...

  3. Ztree异步加载自动展开节点

    在Ztree的官网Demo中,有自动展开的例子,是通过设置节点属性open:true来实现自动展开的,但是在异步加载中,这个属性设置为true也不会自动展开,因为open:true是指在有子节点的情况 ...

  4. JQuery ztree 异步加载实践

    本来要做一个文件目录浏览界面,需要遍历所有的文件和目录,很显然一次性读取时很费时费力的一件事情. 因此就需要做异步加载.... 不过网上的几篇帖子还挺坑的!原始参考:JQuery异步加载实例,相对来说 ...

  5. 关于ztree异步加载的问题(二)

    本来以为这个异步加载会很难控制,因为考虑到ztree节点图标的控制,结果并不是那么困难,ztree自己控制图标,你只要在json中设置isParent:true,它自己会识别父节点并控制图标,以下是核 ...

  6. zTree异步加载并初始化树时全部展开(贴主要代码)

    <%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...

  7. Jquery树控件ZTree异步加载

    异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 这里面主要设计ztree的setting变量的async属性设置: var setting = { ...

  8. 插件使用一树形插件---zTree一zTree异步加载

    zTree 可以实现异步加载.异步加载可以让初次加载速度快,带来好的用户体验. 异步加载 官方源码中的demo提示了例子.例子是采用php语言. 在java语言中,zTree如何与Servlet结合呢 ...

  9. ztree异步加载树节点

    参考文档:https://www.cnblogs.com/tenWood/p/8620708.html ztree api地址:http://www.treejs.cn/v3/api.php 说明:j ...

随机推荐

  1. C&num;通过ODBC查询HANA数据库数据

    创建HANA的ODBC数据库连接. 默认在控制面板——>管理工具——>数据源(ODBC) 提示:如果系统是64位的,要运行 C:\Windows\SysWOW64\odbcad32.exe ...

  2. 基于ant的jmeter自动化性能测试

    准备工作: 1.java的运行环境正常,及运行java -version.javac -version能正常输出java版本: 2.ant的运行环境正常,使用ant需要配置环境变量,编辑/etc/pr ...

  3. android四种更新UI的方法

    笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...

  4. Array 原型扩展(快速排序,搅乱顺序)

    /// 快速快速排序算法Array.prototype.quickSort = function (left, right) { // left = left || 0; // right = rig ...

  5. UML示例图

  6. 10天学会phpWeChat——第八天:Form类,丰富表单提交的字段类型

    通过前面七讲的系列教程,我们完成了一个包含后台并自适应PC+h5移动端的文章管理模块. 在实际的生产环境中,文章投稿.商品上传等操作并不会简单局限于一个text和textarea组成的表单.在实际中, ...

  7. Entity Framework查询注意

    首先我们看下where的方法,直接查看定义(定义如下),其实一种是对IEnumerable的扩展,一种是对IQueryable的扩展,直接看最常用的,其实区别就在IEnumerable的扩展的参数是系 ...

  8. Hive内置数据类型

    Hive的内置数据类型可以分为两大类:(1).基础数据类型:(2).复杂数据类型.其中,基础数据类型包括:TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBL ...

  9. 描述性统计的matlab实现

    理论讲的再多不会做也白弄 直接上手 一.针对接近正态分布的(均值,方差,标准差,极差,变异系数,偏度,峰度) 这里我必须提前说明一点就是,你在写好函数后,函数的名是dts,你保存的文件名也必须是dts ...

  10. spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序)

    转载:spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序) 众所周知,spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执 ...