千家信息网

基于zTree中Jsp通用动态级联树结构是怎么样的

发表于:2024-11-30 作者:千家信息网编辑
千家信息网最后更新 2024年11月30日,这篇文章将为大家详细讲解有关基于zTree中Jsp通用动态级联树结构是怎么样的,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1、categoryTree.html
千家信息网最后更新 2024年11月30日基于zTree中Jsp通用动态级联树结构是怎么样的

这篇文章将为大家详细讲解有关基于zTree中Jsp通用动态级联树结构是怎么样的,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1、categoryTree.html 通用页面

    2、调用方法

    3.controller

    import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;/** * @author  liujun * @since 2018/1/18. * 产品分类 */@Controller@RequestMapping("/category")public class CategoryController {    Logger logger = LoggerFactory.getLogger(CategoryController.class);    @Autowired    CategoryTreeService categoryService;    @RequestMapping(value = "/getCategoryTree")    @ResponseBody    public List getCategoryTree(){        logger.info("处理请求:/category/getCategoryTree");        return this.categoryService.getCategoryTree();    }}

    4、service接口

    import java.util.List;/** * @author  liujun * @since 2018/1/18. * 产品类别服务接口 */public interface CategoryTreeService extends CommonService{    /**     * 获取类别树     * @return List商品类别{@link CategoryTree}     */    public List getCategoryTree();}import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.jpa.domain.Specification;import java.io.Serializable;import java.util.Collection;import java.util.List;/** * Created by liujun on 2018/1/22. */public interface CommonService  {    public E get(ID id);    public E find(ID id);    public List getAll();    public Long getTotalCount();    public E save(E entity);    public E update(E entity);    public void delete(E entity);    public void delete(ID id);    public void delete(Collection entities);    public void flush();    public List findAll(Specification spec);    public Page findAll(Pageable pageable);    public Page findAll(Specification spec, Pageable pageable);    public List findAll(Specification spec, Sort sort);    public long count(Specification spec);}

    5、service实现

    import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * @author liujun * @since 2018/1/18. * @see CategoryTreeServiceImpl * 产品类别业务类{@link Service} */@Servicepublic class CategoryTreeServiceImpl extends CommonServiceImpl implements CategoryTreeService {    @Resource(name = "baseSqlDaoImpl")    CustomBaseSqlDao baseSqlDao;    @Autowired    CategoryTreeDao categoryTreeDao;    @Override    public List getCategoryTree() {        String hql = " select o from CategoryTree o where o.parent_id = 0 order by o.sort_order";        return this.baseSqlDao.queryForList(hql);    }    @Autowired    public void setCategoryDao(CategoryTreeDao categoryTreeDao) {        super.setCommonDao(categoryTreeDao);        this.categoryTreeDao = categoryTreeDao;    }}/** * 基础Service的定义 * @author Jeff Xu * @since 2015-12-09 * @param  * @param  */public abstract class CommonServiceImpl  implements CommonService  {   protected CommonDao commonDao;   public void setCommonDao(CommonDao commonDao) {      this.commonDao = commonDao;   }   public CommonDao getCommonDao() {      return commonDao;   }      /**    * 根据ID获取某个Entity    * @param id    * @return    */   public E get(ID id) {      return commonDao.getOne(id);   }   /**    * 根据ID查找某个Entity(建议使用)    * @param id    * @return    */   public E find(ID id) {      return commonDao.findOne(id);   }   /**    * 获取所有的Entity列表    * @return    */   public List getAll() {      return commonDao.findAll();   }      /**    * 获取Entity的总数    * @return    */   public Long getTotalCount() {      return commonDao.count();   }   /**    * 保存Entity    * @param entity    * @return    */   public E save(E entity) {      return commonDao.save(entity);   }   /**    * 修改Entity    * @param entity    * @return    */   public E update(E entity) {      return commonDao.save(entity);   }      /**    * 删除Entity    * @param entity    */   public void delete(E entity) {      commonDao.delete(entity);   }   /**    * 根据Id删除某个Entity    * @param id    */   public void delete(ID id) {      commonDao.delete(id);   }   /**    * 删除Entity的集合类    * @param entities    */   public void delete(Collection entities) {      commonDao.delete(entities);   }   /**    * 清空缓存,提交持久化    */   public void flush() {      commonDao.flush();   }      /**    * 根据查询信息获取某个Entity的列表    * @param spec    * @return    */   public List findAll(Specification spec) {      return commonDao.findAll(spec);   }      /**    * 获取Entity的分页信息    * @param pageable    * @return    */   public Page findAll(Pageable pageable){      return commonDao.findAll(pageable);   }      /**    * 根据查询条件和分页信息获取某个结果的分页信息    * @param spec    * @param pageable    * @return    */   public Page findAll(Specification spec, Pageable pageable) {      return commonDao.findAll(spec, pageable);   }      /**    * 根据查询条件和排序条件获取某个结果集列表    * @param spec    * @param sort    * @return    */   public List findAll(Specification spec, Sort sort) {      return commonDao.findAll(spec);   }      /**    * 查询某个条件的结果数集    * @param spec    * @return    */   public long count(Specification spec) {      return commonDao.count(spec);   }}

    6、dao接口

    /** * Created by liujun on 2018/1/22. */public interface CustomBaseSqlDao {    public List> querySqlObjects(String sql, Integer currentPage, Integer rowsInPage);    public List> querySqlObjects(String sql);    public List> querySqlObjects(String sql, List params);    public List> querySqlObjects(String sql, Object params, Integer currentPage,Integer rowsInPage);    public PageModel> querySqlObjects(String sql, String sbCount, Map params, Integer currentPage, Integer rowsInPage);    public int getCount(String sql);    public List queryForList(String hql, List params);    public List queryByMapParams(String hql, Map params, Integer currentPage,Integer pageSize);    public List queryByMapParams(String hql, Map params);    public List queryForList(String hql);    public PageModel queryForPage(String hql,int currentPage,int pageSize);    public PageModel queryForPageWithParams(String hql, String hqlCount, Map params, int currentPage,int pageSize);    public PageModel queryForPageWithParams(String hql,Map params, int currentPage,int pageSize);    public PageModel queryForPageBySql(String sql,Integer currentPage,Integer pageSize);    public PageModel queryForPageBySql(String sql,Map params,Integer currentPage,Integer pageSize);    public Long queryCount(String hql, Map params);    public Integer queryCountBySql(String sql, Map params);    public int executeSql(String sql, List params);}/** * Created by liujun on 2018/1/19. */public interface CategoryTreeDao extends CommonDao{}/** * 基础Dao接口定义 * @since 2015-12-09 */@NoRepositoryBeanpublic interface CommonDao  extends JpaRepository, JpaSpecificationExecutor {}

    7、dao实现

    @Component(value="baseSqlDaoImpl")public class CustomBaseSqlDaoImpl implements CustomBaseSqlDao {      @Autowired   private EntityManager em;      public List> querySqlObjects(String sql, Integer currentPage,Integer rowsInPage){      return this.querySqlObjects(sql, null, currentPage, rowsInPage);   }      public List> querySqlObjects(String sql){      return this.querySqlObjects(sql, null, null, null);   }      public List> querySqlObjects(String sql, List params){      return this.querySqlObjects(sql, params, null, null);   }      @SuppressWarnings("unchecked")   public List> querySqlObjects(String sql, Object params, Integer currentPage,Integer rowsInPage){      Query qry = em.createNativeQuery(sql);      SQLQuery s = qry.unwrap(SQLQuery.class);            //设置参数      if(params != null){         if(params instanceof List){            List paramList = (List) params;            for(int i = 0, size = paramList.size(); i < size; i++){               qry.setParameter(i+1, paramList.get(i));            }         }else if(params instanceof Map){            Map paramMap = (Map) params;            for(String key : paramMap.keySet()){               qry.setParameter(key, paramMap.get(key));            }         }      }            if (currentPage != null && rowsInPage != null) {//判断是否有分页         // 起始对象位置         qry.setFirstResult(rowsInPage * (currentPage - 1));         // 查询对象个数         qry.setMaxResults(rowsInPage);      }      s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);      List> resultList=new ArrayList>();      try {         resultList=s.list();      } catch (Exception e) {      }finally{         em.close();      }      return resultList;   }      public PageModel> querySqlObjects(String sql,         String sbCount, Map params, Integer currentPage,Integer rowsInPage){      PageModel> pageModel = new PageModel>();            List> list = this.querySqlObjects(sql, params, currentPage, rowsInPage);      pageModel.setList(list);            if(currentPage == null || rowsInPage == null){         pageModel.setTotalCount(list == null ? 0 : list.size());      }else{         Integer count = this.queryCountBySql(sbCount, params);                  pageModel.setCurrentPage(currentPage);         pageModel.setTotalCount(count);         pageModel.setPageSize(rowsInPage);                  int totalPage = 0;         if(count%rowsInPage == 0){            totalPage = count / rowsInPage;         }else{            totalPage = count / rowsInPage + 1;         }                  pageModel.setTotalPage(totalPage);      }                  return pageModel;   }         public int getCount(String sql){      String sqlCount="select count(0) count_num from ("+sql+") as total";      List> list = this.querySqlObjects(sqlCount);      if(list.size() > 0){         int countNum=((BigInteger) list.get(0).get("count_num")).intValue();         return countNum;      }else{         return 0;      }   }      /**    * 处理sql语句    *     * @param _strSql    * @return    */   public String toSql(String _strSql) {      String strNewSql = _strSql;      if (strNewSql != null) {         strNewSql = regReplace("'", "''", strNewSql);      } else {         strNewSql = "";      }      return strNewSql;   }   private String regReplace(String strFind, String strReplacement, String strOld) {      String strNew = strOld;      Pattern p = null;      Matcher m = null;      try {         p = Pattern.compile(strFind);         m = p.matcher(strOld);         strNew = m.replaceAll(strReplacement);      } catch (Exception e) {      }      return strNew;   }      /**    * 根据hql语句查询数据    * @param hql    * @return    */   @SuppressWarnings("rawtypes")   public List queryForList(String hql, List params){      Query query = em.createQuery(hql);      List list = null;      try {         if(params != null && !params.isEmpty()){            for(int i=0,size=params.size();i params, Integer currentPage,Integer pageSize){      //EntityManager em = this.emf.createEntityManager();      Query query = em.createQuery(hql);      List list = null;      try {         if(params != null && !params.isEmpty()){            for(Map.Entry entry: params.entrySet()){               query.setParameter(entry.getKey(), entry.getValue());            }         }                  if(currentPage != null && pageSize != null){            query.setFirstResult((currentPage-1)*pageSize);            query.setMaxResults(pageSize);         }         list = query.getResultList();      } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }            return list;   }      @SuppressWarnings("rawtypes")   public List queryByMapParams(String hql, Map params){      return queryByMapParams(hql, params, null, null);   }      @SuppressWarnings("rawtypes")   public List queryForList(String hql){      return queryForList(hql, null);   }      /**    * 根据hql语句和分页条件查找分页数据    * @param hql    * @param currentPage    * @param pageSize    * @return    */   @SuppressWarnings({ "rawtypes", "unchecked" })   public PageModel queryForPage(String hql,int currentPage,int pageSize){      PageModel page = new PageModel();      List list = null;      Integer totalCount = 0;      Integer totalPage = 0; //总页数      try {         int firstResult = (currentPage-1)*pageSize;         Query query = em.createQuery(hql);         query.setMaxResults(pageSize);         query.setFirstResult(firstResult);         list = query.getResultList();                  Query query2 = em.createQuery(hql);         List list2 = query2.getResultList();         totalCount = (list2 == null) ? 0 : list2.size();         if(totalCount % pageSize == 0){            totalPage = totalCount/pageSize;         }else{            totalPage = totalCount/pageSize + 1;         }                  page.setCurrentPage(currentPage);         page.setList(list);         page.setPageSize(pageSize);         page.setTotalCount(totalCount);         page.setTotalPage(totalPage);      } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }           return page;   }      /**    *     * @param hql    * @param hqlCount  查询所有数据的hql    * @param params    * @param currentPage    * @param pageSize    * @return    */   @SuppressWarnings({ "unchecked", "rawtypes" })   public PageModel queryForPageWithParams(String hql, String hqlCount, Map params, int currentPage,int pageSize){      PageModel page = new PageModel();      List list = null;      Integer totalCount = 0;      Integer totalPage = 0;            //EntityManager em = this.emf.createEntityManager();      Query query = em.createQuery(hql);            try {                  if(params != null){            for(Map.Entry entry: params.entrySet()){               query.setParameter(entry.getKey(), entry.getValue());            }         }                  query.setMaxResults(pageSize);         query.setFirstResult((currentPage-1)*pageSize);         list = query.getResultList();                           totalCount = this.queryCount(hqlCount, params).intValue();         if(totalCount % pageSize == 0){            totalPage = totalCount/pageSize;         }else{            totalPage = totalCount/pageSize + 1;         }                  page.setCurrentPage(currentPage);         page.setPageSize(pageSize);         page.setList(list);         page.setTotalCount(totalCount);         page.setTotalPage(totalPage);               } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }              return page;   }      @SuppressWarnings({ "unchecked", "rawtypes" })   public PageModel queryForPageWithParams(String hql,Map params, int currentPage,int pageSize){      PageModel page = new PageModel();      List list = null;      Integer totalCount = 0;      Integer totalPage = 0;            //EntityManager em = this.emf.createEntityManager();      Query query = em.createQuery(hql);            try {                  if(params != null){            for(Map.Entry entry: params.entrySet()){               query.setParameter(entry.getKey(), entry.getValue());            }         }                  query.setMaxResults(pageSize);         query.setFirstResult((currentPage-1)*pageSize);         list = query.getResultList();                  Query queryTotal = em.createQuery(hql);                  if(params != null){            for(Map.Entry entry: params.entrySet()){               queryTotal.setParameter(entry.getKey(), entry.getValue());            }         }                  List totlaList = queryTotal.getResultList();          totalCount = (totlaList == null) ? 0 : totlaList.size();         if(totalCount % pageSize == 0){            totalPage = totalCount/pageSize;         }else{            totalPage = totalCount/pageSize + 1;         }                  page.setCurrentPage(currentPage);         page.setPageSize(pageSize);         page.setList(list);         page.setTotalCount(totalCount);         page.setTotalPage(totalPage);               } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }              return page;   }        /**    * 根据SQL语句查询分页    * @param sql    * @param currentPage    * @param pageSize    * @return    */   @SuppressWarnings({ "rawtypes", "unchecked" })   public PageModel queryForPageBySql(String sql,Integer currentPage,Integer pageSize){      PageModel page = new PageModel();      Integer totalCount = 0;      Integer totalPage = 0;            //EntityManager em = this.emf.createEntityManager();      Query qry = em.createNativeQuery(sql);      Query qry2 = em.createNativeQuery(sql);      SQLQuery s = qry.unwrap(SQLQuery.class);      if (currentPage != null && pageSize != null) {//判断是否有分页         // 起始对象位置         qry.setFirstResult(pageSize * (currentPage - 1));         // 查询对象个数         qry.setMaxResults(pageSize);      }      s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);      List resultList=new ArrayList();      List list = qry2.getResultList();      totalCount = (list == null ? 0 : list.size());      if(totalCount % pageSize == 0){         totalPage = totalCount/pageSize;      }else{         totalPage = totalCount/pageSize + 1;      }      try {         resultList=s.list();         page.setCurrentPage(currentPage);         page.setPageSize(pageSize);         page.setList(resultList);         page.setTotalCount(totalCount);         page.setTotalPage(totalPage);      } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }           return page;   }      @SuppressWarnings({ "rawtypes", "unchecked" })   public PageModel queryForPageBySql(String sql,Map params,Integer currentPage,Integer pageSize){      PageModel page = new PageModel();      Integer totalCount = 0;      Integer totalPage = 0;            Query qry = em.createNativeQuery(sql);      Query qry2 = em.createNativeQuery(sql);            for(Map.Entry entry: params.entrySet()){         qry.setParameter(entry.getKey(), entry.getValue());         qry2.setParameter(entry.getKey(), entry.getValue());      }            SQLQuery s = qry.unwrap(SQLQuery.class);      if (currentPage != null && pageSize != null) {//判断是否有分页         // 起始对象位置         qry.setFirstResult(pageSize * (currentPage - 1));         // 查询对象个数         qry.setMaxResults(pageSize);      }      s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);      List resultList=new ArrayList();      List list = qry2.getResultList();      totalCount = (list == null ? 0 : list.size());      if(totalCount % pageSize == 0){         totalPage = totalCount/pageSize;      }else{         totalPage = totalCount/pageSize + 1;      }      try {         resultList=s.list();         page.setCurrentPage(currentPage);         page.setPageSize(pageSize);         page.setList(resultList);         page.setTotalCount(totalCount);         page.setTotalPage(totalPage);      } catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }           return page;   }      /**    * 查询总数    * @param hql    * @param params    * @return    */   public Long queryCount(String hql, Map params){      //EntityManager em = this.emf.createEntityManager();      Query query = em.createQuery(hql);      Long count = null;      try{         if(params != null && !params.isEmpty()){            for(Map.Entry entry: params.entrySet()){               query.setParameter(entry.getKey(), entry.getValue());            }         }         count = (Long) query.getSingleResult();      }catch (Exception e) {         e.printStackTrace();      }finally{         em.close();      }            return count;   }      /**    * 查询总数    * @param hql    * @param params    * @return    */   public Integer queryCountBySql(String sql, Map params){      Integer count = null;      try {         Query query = em.createNativeQuery(sql);         if(params != null && !params.isEmpty()){            for(Map.Entry entry: params.entrySet()){               query.setParameter(entry.getKey(), entry.getValue());            }         }                  Object obj = query.getSingleResult();         if(obj instanceof BigInteger){            count = ((BigInteger) obj).intValue();         }else{            count = (Integer) obj;         }                } finally {         if(em != null){            em.close();         }      }      return count;   }      /**    * select count(*) from table     * @param sql    * @param params    * @return    */   public int executeSql(String sql, List params){      try {         Query query = em.createNativeQuery(sql);         if(params != null && !params.isEmpty()){            for(int i = 0, size = params.size(); i < size; i++){               query.setParameter(i+1, params.get(i));            }         }         return query.executeUpdate();      } finally {         if(em != null){            em.close();         }      }   }   }

    8、entity

    /** * Created by liujun on 2018/1/18. */@Entity@Table(name="ecs_category")public class CategoryTree implements Serializable{    private Long cat_id;//id主键    private String cat_name; //名称    private Long parent_id;//父    private String keywords;//关键字    private String cat_desc;//描述    private Long sort_order;//排序    private String measure_unit;//单位    private Integer show_in_nav;//    private String style;    private Integer is_show;//是否显示    private Integer grade;//级别    private String filter_attr;    private Long category_index;    private Long show_in_index;//    private String path_name;    private Integer is_virtual;    private Long show_goods_num;//    CategoryTree partent = new CategoryTree();//父    List nodes = new ArrayList();//子    @Id   @Column(name="cat_id", length = 8, nullable = false)    @GeneratedValue(strategy = GenerationType.AUTO)    public Long getCat_id() {        return cat_id;    }    public void setCat_id(Long cat_id) {        this.cat_id = cat_id;    }   @Column(name="cat_name")    public String getCat_name() {        return cat_name;    }    public void setCat_name(String cat_name) {        this.cat_name = cat_name;    }    @Column(name="parent_id")    public Long getParent_id() {        return parent_id;    }    public void setParent_id(Long parent_id) {        this.parent_id = parent_id;    }    @Column(name="keywords")    public String getKeywords() {        return keywords;    }    public void setKeywords(String keywords) {        this.keywords = keywords;    }   @Column(name="cat_desc")    public String getCat_desc() {        return cat_desc;    }    public void setCat_desc(String cat_desc) {        this.cat_desc = cat_desc;    }   @Column(name="sort_order")    public Long getSort_order() {        return sort_order;    }    public void setSort_order(Long sort_order) {        this.sort_order = sort_order;    }   @Column(name="measure_unit")    public String getMeasure_unit() {        return measure_unit;    }    public void setMeasure_unit(String measure_unit) {        this.measure_unit = measure_unit;    }   @Column(name="show_in_nav")    public Integer getShow_in_nav() {        return show_in_nav;    }    public void setShow_in_nav(Integer show_in_nav) {        this.show_in_nav = show_in_nav;    }   @Column(name="style")    public String getStyle() {        return style;    }    public void setStyle(String style) {        this.style = style;    }   @Column(name="is_show")    public Integer getIs_show() {        return is_show;    }    public void setIs_show(Integer is_show) {        this.is_show = is_show;    }   @Column(name="grade")    public Integer getGrade() {        return grade;    }    public void setGrade(Integer grade) {        this.grade = grade;    }   @Column(name="filter_attr")    public String getFilter_attr() {        return filter_attr;    }    public void setFilter_attr(String filter_attr) {        this.filter_attr = filter_attr;    }   @Column(name="category_index")    public Long getCategory_index() {        return category_index;    }    public void setCategory_index(Long category_index) {        this.category_index = category_index;    }   @Column(name="show_in_index")    public Long getShow_in_index() {        return show_in_index;    }    public void setShow_in_index(Long show_in_index) {        this.show_in_index = show_in_index;    }// @Column(name="path_name")//    public String getPath_name() {//        return path_name;//    }////    public void setPath_name(String path_name) {//        this.path_name = path_name;//    }   @Column(name="is_virtual")    public Integer getIs_virtual() {        return is_virtual;    }    public void setIs_virtual(Integer is_virtual) {        this.is_virtual = is_virtual;    }   @Column(name="show_goods_num")    public Long getShow_goods_num() {        return show_goods_num;    }    public void setShow_goods_num(Long show_goods_num) {        this.show_goods_num = show_goods_num;    }    @OneToMany(cascade = CascadeType.REFRESH,fetch = FetchType.LAZY)    @JoinColumn(name="parent_id")    @Where(clause = "is_virtual=0")    @OrderBy(value = "sort_order asc")    public List getNodes() {        return nodes;    }    public void setNodes(List nodes) {        this.nodes = nodes;    }}

    9、配置

                                         com.jeff.tianti         com.youlin                                    org.hibernate.cfg.ImprovedNamingStrategy                                                                                                                                

    10、效果图

    11、dom结构

    关于"基于zTree中Jsp通用动态级联树结构是怎么样的"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

    0