新增商品——商品类目选择
使用的是 EasyUI 的 tree 控件
原型
点击新增商品。最终目标如下:
点击发送的请求为:
POST http://localhost:8081/item/cat/list 404 (Not Found)
功能分析
点击,触发一个事件
搜索一下selectItemCat
,该jsp
文件没有,那就是一个外部的 js
展示商品分类列表,使用EasyUI
的tree控件
展示。
初始化tree
请求的url
:/item/cat/list
参数:
初始化tree
时只需要把第一级节点展示,子节点异步加载。
long id(父节点id)
返回值:json
。数据格式
[{
"id": 1,
"text": "Node 1",
"state": "closed"
},{
"id": 2,
"text": "Node 2",
"state": "closed"
}]
state
:如果节点下有子节点closed
,如果没有子节点open
。
步骤
创建EasyUITreeNode
创建一个pojo
来描述tree
的节点信息,包含三个属性id
、text
、state
。放到e3-common
工程中。
package cn.ynx.e3mall.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;
}
}
查询的表:
tb_item_cat
查询列:
Id
、name
、isparent
查询条件parentId
Dao
层
tb_item_cat
可以使用逆向工程生成的代码
Service
层
参数:long parentId
业务逻辑:
1、根据parentId
查询节点列表
2、转换成EasyUITreeNode
列表。
3、返回。
返回值:List<EasyUITreeNode>
package cn.ynx.e3mall.service;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import java.util.List;
public interface ItemCatService {
List<EasyUITreeNode> getCatList(long parentId);
}
package cn.ynx.e3mall.service.impl;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.mapper.TbItemCatMapper;
import cn.ynx.e3mall.pojo.TbItemCat;
import cn.ynx.e3mall.pojo.TbItemCatExample;
import cn.ynx.e3mall.service.ItemCatService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class ItemCatServiceImpl implements ItemCatService {
@Resource
private TbItemCatMapper tbItemCatMapper;
@Override
public List<EasyUITreeNode> getCatList(long parentId) {
//根据parentId查询子节点列表
TbItemCatExample example = new TbItemCatExample();
TbItemCatExample.Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
//创建返回结果List
List<EasyUITreeNode> resultList = new ArrayList<>();
//把列表转换成EasyUITreeNode列表
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;
}
}
发布服务:
<dubbo:service interface="cn.ynx.e3mall.service.ItemCatService" ref="itemCatServiceImpl" timeout="300000" />
Controller
层
引用服务:
<dubbo:reference interface="cn.ynx.e3mall.service.ItemCatService" id="itemCatService" />
初始化tree
请求的url
:/item/cat/list
参数:
long id(父节点id)
返回值:json
。数据格式
List<EasyUITreeNode>
package cn.ynx.e3mall.controller;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.service.ItemCatService;
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 javax.annotation.Resource;
import java.util.List;
@Controller
public class ItemCatController {
@Resource
private ItemCatService itemCatService;
@RequestMapping("/item/cat/list")
@ResponseBody
public List<EasyUITreeNode> getItemCatList(
@RequestParam(name = "id" ,defaultValue = "0")Long parentId){
//调用服务查询节点列表
List<EasyUITreeNode> list = itemCatService.getCatList(parentId);
return list;
}
}
@RequestParam(value="id", defaultValue="0")Long parented
给id
一个默认值0