需求
分析
总结
{
"data": [
{
"u": "/products/1.html",
"n": "<a href='/products/1.html'>图书、音像、电子书刊</a>",
"i": [
{
"u": "/products/2.html",
"n": "电子书刊",
"i": [
"/products/3.html|电子书",
"/products/4.html|网络原创",
"/products/5.html|数字杂志",
"/products/6.html|多媒体图书"
]
},
{
"u": "/products/7.html",
"n": "音像",
"i": [
"/products/8.html|音乐",
"/products/9.html|影视",
"/products/10.html|教育音像"
]
},
{
"u": "/products/11.html",
"n": "英文原版",
"i": [
"/products/12.html|少儿",
"/products/13.html|商务投资",
"/products/14.html|英语学习与考试",
"/products/15.html|小说",
"/products/16.html|传记",
"/products/17.html|励志"
]
},
......
Service层
- taotao-rest的com.taotao.rest.pojo包下创建商品分类的节点类CatNode,和商品类目查询结果CatResult。
- CatNode中可以使用 @JsonProperty 注解注释用jackson转化为json后变量的名称。
- 子节点可能为String也可能为CatNode,所以items类型为?,data的类型也为?。
public class CatNode {
@JsonProperty("u")
private String url;
@JsonProperty("n")
private String name;
/**
* 子节点。子节点若为叶节点则为String,若非叶节点则为CatNode
*/
@JsonProperty("i")
private List<?> items;
public CatNode(String url, String name, List<?> items) {
this.url = url;
this.name = name;
this.items = items;
}
public String getUrl() {
return url;
}
public String getName() {
return name;
}
public List<?> getItems() {
return items;
}
}
public class CatResult {
private List<?> data;
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
- 创建ItemCatService和实现类,查询所有商品分类列表。
public interface ItemCatService {
CatResult getItemCatList();
}
@Service
public class ItemCatServiceImpl implements ItemCatService {
@Autowired
private TbItemCatMapper itemCatMapper;
@Override
public CatResult getItemCatList() {
CatResult result = new CatResult();
List<?> list = getListByParentId(0);
result.setData(list);
return result;
}
/**
* 递归获得商品类目
*/
private List<?> getListByParentId(long parentId) {
List list = new ArrayList<>();
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
List<TbItemCat> itemCatList = itemCatMapper.selectByExample(example);
for (TbItemCat itemCat : itemCatList) {
if (itemCat.getIsParent()) {
String url = "/products/" + itemCat.getId() + ".html";
String name = null;
if (parentId == 0) {
name = "<a href='/products/" + itemCat.getId() + ".html'>" + itemCat.getName() + "</a>";
} else {
name = itemCat.getName();
}
List<?> items = getListByParentId(itemCat.getId());
list.add(new CatNode(url, name, items));
} else {
String s = "/products/" + itemCat.getId() + ".html|" + itemCat.getName();
list.add(s);
}
}
return list;
}
}
Controller层
- 接受回调参数名,返回商品类目信息的json作为参数的js字符串。
- 返回类型为 “application/json;charset=UTF-8”, 否则会乱码。
- 受前端界面限制,只选取前14类商品(也可修改前端页面显示全部商品,但是逻辑较复杂,省略)。
@Controller
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;
/**
* @param callback 回调函数名称
*/
@RequestMapping(value = "itemcat/all", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String getItemCatList(String callback) {
CatResult catResult = itemCatService.getItemCatList();
List newData = new ArrayList<>(14);
List<?> data = catResult.getData();
for (int i = 0; i < 14; i++) {
newData.add(data.get(i));
}
catResult.setData(newData);
return callback + "(" + JsonUtils.objectToJson(catResult) + ")";
}
}