jsonP 后台写法 及 层级树型数据递归查询
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 后台写法 及 层级树型数据递归查询的更多相关文章
- CSS z-index 属性的使用方法和层级树的概念
之前有一篇文章提到过z-index,我们知道只有在元素设置了position部位static时才生效,而且z-index也跟父元素有关系,今天就在ie7遇到类似问题,在网上查了一些资料,发现一篇好文章 ...
- 【转】CSS z-index 属性的使用方法和层级树的概念
文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...
- jquery的ajax和jsonp的写法
交互 ajax jsonp ajax跟之前一模一样 $(document).ready(function(){ $.ajax({ url:'get.php', ...
- mschart asp chart 用法,包括前台写法与后台写法,还有click事件,如何触发。
纯后台动态生成aspchart ,这种方式没办法实现chart中click事件.click事件点击没有反应,用第二种可以实现点击事件. 两种方式实现同一种效果图: 第一种写法:后台动态生成aspcha ...
- noip2017D2T3的几种写法...(BIT/线段树/平衡树)
题意各大oj上都有啦..想必来搜题解的都看过题面了...Qw Solution1: 首先观察n=1的情况,显然就是中间删掉一个数后面加上一个数,并查询那个删掉的数(以后把这样一个过程称为一个操作啦(( ...
- jsonp 后台返回注意事项
前端代码 <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script ...
- PHP递归实现层级树状展现数据
树状数据展现很常用,今天学习了PHP的递归,也来总结总结! PHP代码: function _getTreeList_custom1($data,$parent_id,$depth) { $retur ...
- 微信获取用户数据后台写法,author2.0认证
/* 微信授权接口 */ //1.设置路由 router.get('/wechat/userinfo', function(req, res) { var cb = req.query.cb; //设 ...
- vue中组件之间的相互调用,及通用后台管理系统左侧菜单树的迭代生成
由于本人近期开始学习使用vue搭建一个后端管理系统的前端项目,在左侧生成菜单树的时候遇到了一些问题.在这里记录下 分析:由于本人设定的菜单可以使多级结构,直接使用vue的v-for 遍历并不是很方便. ...
随机推荐
- MATLAB 笔记
MATLAB的学习 Matlab 主要有5大部分构成,分别是MATLAB语言,桌面工具与开发环境,数学函数库 ,图形系统和应用程序接口.以及众多的专业工具.
- Python数据挖掘——基础知识
Python数据挖掘——基础知识 数据挖掘又称从数据中 挖掘知识.知识提取.数据/模式分析 即为:从数据中发现知识的过程 1.数据清理 (消除噪声,删除不一致数据) 2.数据集成 (多种数据源 组合在 ...
- 【RL系列】On-Policy与Off-Policy
强化学习大致上可分为两类,一类是Markov Decision Learning,另一类是与之相对的Model Free Learning 分为这两类是站在问题描述的角度上考虑的.同样在解决方案上存在 ...
- sqlserver-查阻塞
模拟阻塞: 打开两个窗口: 窗口一: BEGIN TRANSACTION--开始事务 --等待1分钟 WAITFOR DELAY '00:1'; 窗口二: 查询阻塞:(当前被阻塞的进程id,不 ...
- Scrum立会报告+燃尽图(十月二十五日总第十六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- Python学习之路7 - 生成器&迭代器
本章内容: 列表生成式 生成器 yield 迭代器 列表生成式 当我们要定义一个列表的时候,我们通常用这种方式a = [1,2,3],但是如果我们定义了一个比较长的列表的时候,手动定义列表就会比较麻烦 ...
- HDU 5869 Different GCD Subarray Query rmq+离线+数状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...
- HDU 5646 DZY Loves Partition
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- Iterable,Iterator和forEach
Iterable Interface Iterable<T> 方法: Iterator<T> iterator() Returns an iterator over a set ...
- 【第二周】【作业五】Scrum 每日站会
1.首先来看一下什么是Scrum: Scrum是一种敏捷软件开发的方法学,用于迭代式增量软件开发过程.Scrum在英语是橄榄球运动中争球的意思. 虽然Scrum是为管理软件开发项目而开发的,它同样可以 ...