• 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;
        }
    
    }

    前台需要的数据结构:

    后台数据结构:

    最终返回的数据结构:

  • 相关阅读:
    Core Data入门
    web前端开发与iOS终端开发的异同
    Blocks编程
    ARC下循环引用的问题
    小项目
    error: /Users/**/Documents/workspace/***/clean_right_normal.png: No such file or directory
    修复NSTextAlignmentCenter引起的警告
    Implicit conversion loses integer precision: 'long long' to 'NSUInteger' (aka 'unsigned int')
    手势相关函数
    implicit declaration of function setxattr is invalid in c99
  • 原文地址:https://www.cnblogs.com/libin6505/p/9712734.html
Copyright © 2020-2023  润新知