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. 近中期3D编程研究目标

    近几年一直在用业余时间研究3D编程,研究的中期目标是建立一个实用的开源3D编程框架.3D编程技术最直接的应用是开发游戏,所以3D编程框架也就是3D游戏开发框架.在我看来,游戏是否好玩的关键是能否为玩家 ...

  2. 【jpeg_Class 类】使用说明

    jpeg_Class类是针对图片操作类,可以获取图片属性.等比例缩略图片.裁切图片.图片上打印文字及打印水印等功能. 目录 原型 参数 返回 说明 Sub load(byVal path) path ...

  3. 2.0 flume、sqoop、oozie/Azkaban

    在一个完整的大数据处理系统中,除了hdfs+mapreduce+hive组成分析系统的核心之外,还需要数据采集.结果数据导出.任务调度等不可或缺的辅助系统,而这些辅助工具在hadoop生态体系中都有便 ...

  4. Windows操作系统C盘占用空间过多

    Windows操作系统C盘占用空间过多 大部分的windows电脑用户在长时间使用PC时都会遇到一个问题,就是C盘占用的空间会越来越多,乃至占满整个C盘. 后来在百度了一波,发现各种方法都试过了,也不 ...

  5. ObjectAnimator实现菜单的弹出(扇形)

    用ObjectAnimator 实现菜单的弹出 首先是菜单的图片资源和布局 布局中使用FrameLaout 将菜单唤出对应的imageView放在布局的最后面来隐藏菜单详细内容. <?xml v ...

  6. 梯度下降算法以及其Python实现

    一.梯度下降算法理论知识 我们给出一组房子面积,卧室数目以及对应房价数据,如何从数据中找到房价y与面积x1和卧室数目x2的关系?   为了实现监督学习,我们选择采用自变量x1.x2的线性函数来评估因变 ...

  7. Python:迭代器的简单理解

    一.什么是迭代器 迭代,顾名思义就是重复做一些事很多次(就现在循环中做的那样).迭代器是实现了__next__()方法的对象(这个方法在调用时不需要任何参数),它是访问可迭代序列的一种方式,通常其从序 ...

  8. Python:模块学习——os模块

    os模块提供了多个访问操作系统服务的功能 os模块中一些重要的函数和变量 os.name 显示当前使用平台 os.getcwd() 显示当前Python脚本工作路径 os.listdir('dirna ...

  9. Android连接SQLServer详细教程(数据库+服务器+客户端)

    摘星 标签: android连接sql http://blog.csdn.net/haoxingfeng/article/details/9111105

  10. 元素相加交换另解&puts的一个用法

    #include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; a^=b; b^=a ...