Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the general dynamic cascade tree structure of Jsp based on zTree?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the general dynamic cascading tree structure based on Jsp in zTree is like. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. CategoryTree.html general page

/ * display the style of the tree structure * / ul.ztree {position:relative;top: 32pxposition leftRelay 0pxbetwether: 1px solid # 617775: background: # f0f7fbten widthbank 220pxmith heightwitch 360pxoverflowflowyflowflowyrotrotscrollwindflowxdisplay: none} var node_id Var zTreeObj, setting = {view: {selectedMulti: false / / multiple options}, data: {/ / define the data specification simpleData: {whether enable:false,// uses simple data structures This example uses the standard json tree structure idKey:'cat_id',//nodeId field name pIdKey:'parent_id',//parentId field name rootPId:0 / / with the value of nodeId}, key: {/ / field name defines name: "cat_name" / / display name children: "nodes" / / Child Node object}, zTreeNodes = [] / / load dynamic data function loadTree (data) {if (data & & data.length > 0) {zTreeNodes = data; zTreeObj = $.fn.zTree.init ($("# tree"), setting, zTreeNodes);}} $(document) .ready (function () {$.post ("/ category/getCategoryTree", null,loadTree);})

2. Call method

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. * Product category * / @ Controller@RequestMapping ("/ category") public class CategoryController {Logger logger = LoggerFactory.getLogger (CategoryController.class) @ Autowired CategoryTreeService categoryService; @ RequestMapping (value = "/ getCategoryTree") @ ResponseBody public List getCategoryTree () {logger.info ("processing request: / category/getCategoryTree"); return this.categoryService.getCategoryTree ();}}

4. Service interface

Import java.util.List;/** * @ author liujun * @ since 2018-1-18. * Product category service interface * / public interface CategoryTreeService extends CommonService {/ * get category tree * @ return List commodity category {@ 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 implementation

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 * Product category Business Class {@ 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 The definition of basic 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 } / * get an Entity * @ param id * @ return * / public E get (ID id) {return commonDao.getOne (id) according to ID;} / * find an Entity based on ID (recommended) * @ param id * @ return * / public E find (ID id) {return commonDao.findOne (id) } / * get all Entity lists * @ return * / public List getAll () {return commonDao.findAll ();} / * get the total number of Entity * @ return * / public Long getTotalCount () {return commonDao.count () } / * Save Entity * @ param entity * @ return * / public E save (E entity) {return commonDao.save (entity);} / * modify Entity * @ param entity * @ return * / public E update (E entity) {return commonDao.save (entity) } / * delete Entity * @ param entity * / public void delete (E entity) {commonDao.delete (entity);} / * Delete an Entity * @ param id * / public void delete (ID id) {commonDao.delete (id) according to Id Delete the collection class of Entity * @ param entities * / public void delete (Collection entities) {commonDao.delete (entities);} / * clear the cache and submit persistence * / public void flush () {commonDao.flush () } / * get the list of an Entity according to the query information * @ param spec * @ return * / public List findAll (Specification spec) {return commonDao.findAll (spec);} / * get the paging information of Entity * @ param pageable * @ return * / public Page findAll (Pageable pageable) {return commonDao.findAll (pageable) } / * get the paging information of a result according to query conditions and paging information * @ param spec * @ param pageable * @ return * / public Page findAll (Specification spec, Pageable pageable) {return commonDao.findAll (spec, pageable) } / * get a list of result sets according to query and sort criteria * @ param spec * @ param sort * @ return * / public List findAll (Specification spec, Sort sort) {return commonDao.findAll (spec) } / * query the result set of a condition * @ param spec * @ return * / public long count (Specification spec) {return commonDao.count (spec);}}

6. Dao interface

/ * 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 {} / * basic Dao interface definition * @ since 2015-12-09 * / @ NoRepositoryBeanpublic interface CommonDao extends JpaRepository, JpaSpecificationExecutor {}

7. Dao implementation

@ 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); / / set the parameter 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;}} / * * handle the sql statement * * @ 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;} / * query data according to the hql statement * @ 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 ionome0); I Mobile phone, digital, communication notebook home, furniture, home decoration, kitchen care makeup, cleaning shoes, bags, watches, luxury cars, automotive products, food, wine, fresh, specialty books, audio and video, e-book finance, crowdfunding, white slips, insurance

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report