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 遍历并不是很方便. ...
随机推荐
- sqoop安装与简单实用
一,sqoop安装 1.解压源码包 2.配置环境变量 3.在bin目录下的 /bin/configsqoop 注释掉check报错信息 4.配置conf目录下 /conf/sqoop-env.sh 配 ...
- JAVA学习笔记--组合与继承
JAVA一个很重要的功能就是代码的可复用性,代码复用可以大大提升编程效率.这里主要介绍两种代码复用方式:组合和继承. 一.组合 组合比较直观,只需在新的类中产生现有类的对象,新的类由现有类的对象组成, ...
- C#判断字符串中是否有数字
// <summary> /// 提取字符串中的数字字符串 /// </summary> /// <param name="str"></ ...
- spring-boot-mybatis搭建
写在开始 mybatis是一个持久化框架,支持手动sql.存储过程.高级映射.mybatis支持XML方式或注解方式将POJO与数据库表间建立映射. maven依赖 spring-boot.mysql ...
- 7.hdfs工作流程及机制
1. hdfs基本工作流程 1. hdfs初始化目录结构 hdfs namenode -format 只是初始化了namenode的工作目录 而datanode的工作目录是在datanode启动后自己 ...
- springboot 集成 swagger
1. 首先配置swaggerConfigpackage com.lixcx.lismservice.config; import com.lixcx.lismservice.format.Custom ...
- Java中的断言assert
Java陷阱之assert关键字 一.概述 在C和C++语言中都有assert关键,表示断言. 在Java中,同样也有assert关键字,表示断言,用法和含义都差不多. 二.语法 在J ...
- lintcode-496-玩具工厂
496-玩具工厂 工厂模式是一种常见的设计模式.请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类.可以假设只有猫和狗两种玩具. 您在真实的面试中是否遇到过这个题? Yes 样例 ToyF ...
- 解释Spring中IOC, DI, AOP
oc就是控制翻转或是依赖注入.通俗的讲就是如果在什么地方需要一个对象,你自己不用去通过new 生成你需要的对象,而是通过spring的bean工厂为你长生这样一个对象.aop就是面向切面的编程.比如说 ...
- PAT L1-034 点赞
https://pintia.cn/problem-sets/994805046380707840/problems/994805098188750848 微博上有个“点赞”功能,你可以为你喜欢的博文 ...