查看通过ajax访问controller的返回值_电子商城之通过树结构显示商品的类目种类

商品类目选择

611a3cc4146ed003d955f3595eb92801.png

点击选择类目就会弹出选择类目窗口

95f0e1790f843668d0906b935f31dd17.png
9dc1c5caa591ef3ca379946c6eea9bd5.png

这个就是选择类目的html,然后给这个超链接使用jquery设置一些操作:

70a4e0e3a49da67cf24f9b068d1469c1.png

事件绑定了selectItemCat这个class,然后给他绑定事件,初始化tree请求的url:/item/cat/list

参数:long id(父节点id)

返回值:json。数据格式

[{       "id": 1,       "text": "Node 1",       "state": "closed"},{       "id": 2,       "text": "Node 2",       "state": "closed"  }] 

state:如果节点下有子节点“closed”,如果没有子节点“open”

初始化tree时只需要把第一级节点展示,子节点异步加载,也就是说每次点击下一级的时候,它会带着父节点id去访问item/cat/list,所以controller每次只需要处理点击的当前父节点下面的所有子节点就可以了。

针对于上面的描述,也就是说我们controller部分需要接收父节点if,然后返回它的所有的孩子,并且以如上json的格式来返回

contorller

package com.huanfeng.mall.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.huanfeng.common.pojo.EasyUITreeNode;import com.huanfeng.mall.service.ItemCatService;@Controllerpublic class ItemCatContorller {    @Autowiredprivate ItemCatService itemCatService;        @RequestMapping("/item/cat/list")    @ResponseBody    public List getItemCatList(@RequestParam(name="id",defaultValue="0")Long parentId){    System.out.println(parentId);    List list = itemCatService.getItemCatlist(parentId);return list;        }}

在这个controller中需要接收参数id,但是最顶级目录的没有id,所以我们设置默认值为0,然后返回的类型应该是json,所以需要使用@ResponseBody,然后我们需要将返回的每一条数据封装到pojo中,那么我们需要在e3-common工程中创建这个pojo:

package com.huanfeng.common.pojo;import java.io.Serializable;public class EasyUITreeNode implements Serializable {private long id;private String text;private String state;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getState() {return state;}public void setState(String state) {this.state = state;}}

接下来在service中使用的逻辑是根据web层中传递过来的id,然后调用mapper中的查询方法,然后将查询结果转成EasyUITreeNode列表返回。

在manager-interface中定义这个接口:

package com.huanfeng.mall.service;import java.util.List;import com.huanfeng.common.pojo.EasyUITreeNode;public interface ItemCatService {List getItemCatlist(long parentId);}

然后在manager-service中具体实现

package com.huanfeng.mall.service;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.huanfeng.common.pojo.EasyUITreeNode;import com.huanfeng.mall.mapper.TbItemCatMapper;import com.huanfeng.mall.pojo.TbItemCat;import com.huanfeng.mall.pojo.TbItemCatExample;import com.huanfeng.mall.pojo.TbItemCatExample.Criteria;@Servicepublic class ItemCatServiceImpl implements ItemCatService { @Autowiredprivate TbItemCatMapper itemCatMapper;@Overridepublic List getItemCatlist(long parentId) {//根据parentId查询子节点列表TbItemCatExample example=new TbItemCatExample();Criteria criteria = example.createCriteria();criteria.andParentIdEqualTo(parentId);List list = itemCatMapper.selectByExample(example);//把列表转换成EasyUITreeNode列表List resultList=new ArrayList<>();for (TbItemCat tbItemCat :list) {EasyUITreeNode node=new EasyUITreeNode();node.setId(tbItemCat.getId());node.setText(tbItemCat.getName());node.setState(tbItemCat.getIsParent()?"closed":"open");resultList.add(node);}//返回结果return resultList;}}

如上所示,将查询的条件封装到example中,然后查询得到列表,然后此时的列表不符合我们的情况,我们需要遍历它,然后封装成我们想要的列表,其中我们需要判断它是不是父目录,如果是则closed,如果不是则open。然后将这个列表返回。

dao层

因为这个是单表查询,所以直接使用逆向工程生成的就可以了。

2a5ecbe79205027b1568e0ddd3dc54be.png

其中id表示当前目录的id,parent_id表示当前目录的父类id,is_parent表示这个类别是不是父目录。我们查找的时候就是使用Parent_id,比如Parent_id=0,那么表示查找类别为0的所有的子类别。

以上写好之后,我们需要将common和interface重新发布,然后将service发布服务,然后web引用服务

首先在manager-service中的applicationContent-service.xml中发布服务:

然后在manager-web中的springmvc.xml中引用服务:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值