Controller层:

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.utils.JsonUtils;
import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService; @Controller
public class ItemCatController { @Autowired
ItemCatService itemCatService; /*
* jsonp跨域请求的Controller(第二个参数是为了让返回的json串中的中文是utf-8编码,
* 当然也可以用添加参数 Response 对象,然后将结果封装到对象里,再设置Responsetype的方法)
*/
@RequestMapping(value="/itemcat/list",
produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
@ResponseBody
public String getItemCatList(String callback) {
CatResult catrtn = itemCatService.getItemCatList();
//把pojo转成json字符串
String json = JsonUtils.objectToJson(catrtn);
//拼装返回值
String result = callback+"("+json+");";
return result;
} //第二种返回jsonp串的方式(需要spring 4.1 以上版本)
@RequestMapping("/itemcat/list2")
@ResponseBody
public Object getItemCatList2(String callback) {
CatResult catrtn = itemCatService.getItemCatList();
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(catrtn);
mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
}
}

service层:

package com.taotao.rest.service.impl;

import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.mysql.fabric.xmlrpc.base.Array;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.rest.pojo.CatNode;
import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService; @Service
public class ItemCatServiceImpl implements ItemCatService { @Autowired
TbItemCatMapper itemCatMapper; @Override
public CatResult getItemCatList() {
CatResult catResult = new CatResult();
catResult.setData(getCatList(0));
return catResult;
} public List getCatList(long parentId) {
List rtnList = new ArrayList();
//只取前14个
int count = 0;
TbItemCatExample example = new TbItemCatExample();
example.createCriteria().andParentIdEqualTo(parentId);
List<TbItemCat> queryList = itemCatMapper.selectByExample(example);
if (queryList!=null && queryList.size()>0) {
for (TbItemCat item : queryList) {
//如果是父节点
if (item.getIsParent()) {
CatNode node = new CatNode();
node.setUrl("/products/"+item.getId()+".html");
if (parentId==0) {
node.setName("<a href='/products/"+item.getId()+".html'>"+item.getName()+"</a>");
}else{
node.setName(item.getName());
}
//递归调用
node.setItem(getCatList(item.getId()));
//将结果添加到返回集合中
rtnList.add(node);
count ++;
//第一层只取14条记录
if (parentId ==0 && count>=14) {
break;
}
//如果是叶子节点
}else{
rtnList.add("/products/"+item.getId()+".html|"+item.getName());
}
}
}
return rtnList;
} }

前台需要的数据结构:

后台数据结构:

最终返回的数据结构:

jsonP 后台写法 及 层级树型数据递归查询的更多相关文章

  1. CSS z-index 属性的使用方法和层级树的概念

    之前有一篇文章提到过z-index,我们知道只有在元素设置了position部位static时才生效,而且z-index也跟父元素有关系,今天就在ie7遇到类似问题,在网上查了一些资料,发现一篇好文章 ...

  2. 【转】CSS z-index 属性的使用方法和层级树的概念

    文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...

  3. jquery的ajax和jsonp的写法

    交互 ajax jsonp ajax跟之前一模一样 $(document).ready(function(){     $.ajax({         url:'get.php',         ...

  4. mschart asp chart 用法,包括前台写法与后台写法,还有click事件,如何触发。

    纯后台动态生成aspchart ,这种方式没办法实现chart中click事件.click事件点击没有反应,用第二种可以实现点击事件. 两种方式实现同一种效果图: 第一种写法:后台动态生成aspcha ...

  5. noip2017D2T3的几种写法...(BIT/线段树/平衡树)

    题意各大oj上都有啦..想必来搜题解的都看过题面了...Qw Solution1: 首先观察n=1的情况,显然就是中间删掉一个数后面加上一个数,并查询那个删掉的数(以后把这样一个过程称为一个操作啦(( ...

  6. jsonp 后台返回注意事项

    前端代码 <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script ...

  7. PHP递归实现层级树状展现数据

    树状数据展现很常用,今天学习了PHP的递归,也来总结总结! PHP代码: function _getTreeList_custom1($data,$parent_id,$depth) { $retur ...

  8. 微信获取用户数据后台写法,author2.0认证

    /* 微信授权接口 */ //1.设置路由 router.get('/wechat/userinfo', function(req, res) { var cb = req.query.cb; //设 ...

  9. vue中组件之间的相互调用,及通用后台管理系统左侧菜单树的迭代生成

    由于本人近期开始学习使用vue搭建一个后端管理系统的前端项目,在左侧生成菜单树的时候遇到了一些问题.在这里记录下 分析:由于本人设定的菜单可以使多级结构,直接使用vue的v-for 遍历并不是很方便. ...

随机推荐

  1. Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享

    collision-simple-demo Phaser 自带的Arcade虽然易用,但复杂的物理碰撞明显就不够用了,于是Matter等物理引擎还是不得不学的,以下是Matter物理体碰撞的一个插件, ...

  2. [python]序列的重复操作符

    当你需要需要一个序列的多份拷贝时,重复操作符非常有用,它的语法如下: sequence * copies_int In [1]: a = [1,2,3,4] In [2]: a * 5 Out[2]: ...

  3. Python常用模块之Pygame(手册篇:首页)

    Pygame手册官方网址:http://www.pygame.org/docs/ Pygame首页 说明文档: 自述 关于Pygame的基本信息,它是什么,谁参与了以及在哪里找到它. 安装 在几个平台 ...

  4. C++:构造函数1——普通构造函数

    前言:构造函数是C+中很重要的一个概念,这里对其知识进行一个简单的总结 一.构造函数的定义 1.类中的构造函数名与类名必须相同 2.构造函数没有函数的返回类值型说明符 [特别注意]: a.构造函数的返 ...

  5. inside、outside和dmz之间的访问

    现有条件:100M宽带接入,分配一个合法的IP(222.134.135.98)(只有1个静态IP是否够用?);Cisco防火墙PiX515e-r-DMZ-BUN1台(具有Inside.Outside. ...

  6. 【acm】杀人游戏(hdu2211)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others)    M ...

  7. 使用Log4在测试过程中打印执行日志 及配置log4j.properties!

    http://zengxiantao.iteye.com/blog/1881706 1.环境配置:到网上下载log4j-1.2.17.jar包!完后 添加到 项目的build path 中即可! 2. ...

  8. oracle 查询优化及sql改写

    ORACLE有个高速缓冲的概念,这个高速缓冲就是存放执行过的SQL语句,那oracle在执行sql语句的时候要做很多工作,例如解析sql语句,估算索引利用率,绑定变量,读取数据块等等这些操作.假设高速 ...

  9. [微软官网]One Windows Kernel

    One Windows Kernel https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/One-Windows-Kerne ...

  10. 我们为什么要使用Spring Cloud?

    我们为什么要使用Spring Cloud? 两个需要好好看看: Spring Boot Spring Clude Spring Cloud是一个集成了众多开源的框架,利用Spring Boot的开发便 ...