淘淘商城20-CMS内容分类管理之内容分类展示、添加内容分类、重命名内容分类

目录

目录

1.完成后效果展示

2.内容分类展示分析与代码编写

2.1前端分析

2.2后端代码编写

2.2.1封装返回值工具类

2.2.2服务层

2.2.3表现层

3.添加内容分类与重命名内容分类

3.1前端分析

3.2后端代码编写

3.2.1添加内容分类服务层

3.2.2添加内容分类表现层

3.2.3重命名内容分类服务层

3.2.4重命名内容分类表现层


1.完成后效果展示

内容分类展示

新增内容分类

右键添加

可以添加一个内容分类节点 

重命名内容分类

右键重命名,即可重命名。

2.内容分类展示分析与代码编写

2.1前端分析

点击内容分类管理时,会加载easyui异步树控件,发送GET请求 url : '/content/category/list',用户返回id,text,state这样的json格式,即可将数据渲染到异步树控件中。

这里与后台商品类目选择类似(分析链接)。

2.2后端代码编写

2.2.1封装返回值工具类

在taotao-common中,封装easyui异步树节点 EasyUITreeNode

id是树节点的id,text是节点的名称,state是指树形是闭合的还是打开的,如果当前节点还有子节点,那么state的值是"closed",如果当前节点已经是叶子节点了,那么state的值是"open"。

Json数据。

[{id:1,text:节点名称,state:open(closed)},

{id:2,text:节点名称2,state:open(closed)},

{id:3,text:节点名称3,state:open(closed)}

package com.taotao.common.pojo;

import java.io.Serializable;

public class EasyUITreeNode implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;//父节点id
	private String text;//类目名称
	private String state;//如果节点下有子节点“closed”,如果没有子节点“open”
	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;
	}
	
}

2.2.2服务层

在taotao-content-interface创建com.taotao.content.service包,编写接口

package com.taotao.content.service;

import java.util.List;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;

public interface ContentCategoryService {
	/**
	 * 根据parentId查询子节点列表
	 * @param parentId
	 * @return
	 */
	List<EasyUITreeNode> getContentCategoryList(Long parentId);
	
}

在taotao-content-service创建com.taotao.content.service.impl包编写实现类

  1. 取查询参数id,parentId
  2. 根据parentId查询tb_content_category,查询子节点列表。
  3. 得到List<TbContentCategory>
  4. 把列表转换成List<EasyUITreeNode>

package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCategoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	/**
	 * 根据parentId查询正常的子节点列表
	 */
	@Override
	public List<EasyUITreeNode> getContentCategoryList(Long parentId) {
		//1.注入mapper
		//2.设置查询条件, 根据parentId查询正常的子节点列表
		TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
		Criteria criteria = tbContentCategoryExample.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		//分类的状态。可选值:1(正常),2(删除)
		criteria.andStatusEqualTo(1);
		//3.查询parentId下的子节点列表
		List<TbContentCategory> tbContentCategoryList = contentCategoryMapper.selectByExample(tbContentCategoryExample);
		//4.封装List<EasyUITreeNode>
		List<EasyUITreeNode> nodeList= new ArrayList<EasyUITreeNode>();
		for (TbContentCategory tbContentCategory : tbContentCategoryList) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent()?"closed":"open");
			nodeList.add(node);
		}
		return nodeList;
	}
}

发布服务

下面我们发布一下该服务的这个接口,我们在taotao-content-service工程的spring目录下的applicationContext-service.xml文件中发布

<dubbo:service interface="com.taotao.content.service.ContentCategoryService" ref="ContentCategoryServiceImpl" timeout="300000"/>

2.2.3表现层

下面我们需要在taotao-manager-web工程添加Controller类来实现内容分类展示,前提是我们需要先添加对taotao-content-interface的依赖

<dependency>
	<groupId>com.taotao</groupId>
	<artifactId>taotao-content-interface</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

在taotao-manager-web创建controller

package com.taotao.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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCategoryService;

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
	@Autowired
	private ContentCategoryService contentCategoryService;
	/**
	 * 加载内容分类列表
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public List<EasyUITreeNode> getContentCategoryList(@RequestParam(value="id", defaultValue="0") Long parentId){
		List<EasyUITreeNode> list = contentCategoryService.getContentCategoryList(parentId);
		return list;
	}
}

引用dubbo服务

<dubbo:reference interface="com.taotao.content.service.ContentCategoryService" id="contentCategoryService" timeout="300000" />

安装taotao-content。启动taotao-content、taotao-manager、taotao-manager-web即可

3.添加内容分类与重命名内容分类

这个两个功能相似就放在一起写了。

3.1前端分析

在content-category.jsp页面。首先用户右击鼠标,触发onContextMenu函数,关闭原来鼠标的默认事件(防止触发浏览器自带的鼠标右击事件),选中鼠标右击的节点位置,展示id=contentCategoryMenu的菜单栏。

当用户点击菜单栏上的添加、重命名等按钮时会触发相应事件。下面注释写的很明确

当点击添加或者重命名时,触发编辑事件。在编辑事件中,有几处比较重要的地方。

新增节点时发起的post请求url:"/content/category/create",携带{parentId:node.parentId,name:node.text}数据,新增节点的父节点的id,name新增节点的文本。新增成功后,刷新异步树上的节点。

重命名节点时发起的post请求url:"/content/category/update",携带{id:node.id,name:node.text}数据,重命名节点id与重命名。

前端分析结束

3.2后端代码编写

添加内容分类业务逻辑

  1. 接收两个参数:parentId、name
  2. 向tb_content_category表中插入数据。
    1. 创建一个TbContentCategory对象
    2. 补全TbContentCategory对象的属性
    3. 向tb_content_category表中插入数据
  3. 判断父节点的isparent是否为true,不是true需要改为true。
  4. 需要主键返回。
  5. 返回TaotaoResult,其中包装TbContentCategory对象

3.2.1添加内容分类服务层

dao层

使用逆向工程。因为新添加完节点后,可能新节点的父节点以前没有子节点,现在新添加子节点了,需要将新添加子节点的父节点isparent字段改为true。需要在mapper中添加主键返回。

:

<selectKey keyProperty="id" resultType="long" order="AFTER">
  		SELECT LAST_INSERT_ID()
  	</selectKey>

service层

在taotao-content-interface编写接口

在taotao-content-service中编写实现类

这里需要注意查询条件为:status=1,正常的节点。status=2,表示删除的节点

@Override
	public TaotaoResult createContentCategory(Long parentId, String name) {
		//1.创建一个内容分类对象,并补全属性
		TbContentCategory tbContentCategory = new TbContentCategory();
		tbContentCategory.setCreated(new Date());
		tbContentCategory.setIsParent(false);
		tbContentCategory.setName(name);
		tbContentCategory.setParentId(parentId);
		//排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数
		tbContentCategory.setSortOrder(1);
		//状态。可选值:1(正常),2(删除)
		tbContentCategory.setStatus(1);
		tbContentCategory.setUpdated(tbContentCategory.getCreated());
		//2.将内容分类对象插入内容分类表中。插入的同时主键返回到tbContentCategory中
		contentCategoryMapper.insert(tbContentCategory);
		//3.判断父节点的isparent是否为true,不是true需要改为true。
		TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId);
		if(!parentNode.getIsParent()) {
			parentNode.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(parentNode);
		}
		//将新添加的节点封装到TaotaoResult中,用于前端data.data.id
		return TaotaoResult.ok(tbContentCategory);
	}

3.2.2添加内容分类表现层

在taotao-manager-web编写controller

请求的url:/content/category/create

请求的参数:

Long parentId

String name

响应的结果:

json数据,TaotaoResult

/**
	 * 新增内容分类
	 * @param parentId
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult createContentCategory(Long parentId,String name){
		TaotaoResult result = contentCategoryService.createContentCategory(parentId,name);
		return result;
	}

安装taotao-content、taotao-manager、taotao-manager-interface、taotao-manager-dao,启动taotao-manager、taotao-content、taotao-manager-web即可

3.2.3重命名内容分类服务层

请求的url:/content/category/update

参数:id,当前节点id。name,重命名后的名称。

业务逻辑:根据id更新记录。

返回值:返回TaotaoResult.ok()

dao层

直接使用逆向工程

service层

在taotao-content-interface编写接口

/**
	 * 更新内容分类的name
	 * @param parentId
	 * @param name
	 * @return
	 */
	TaotaoResult updateContentCategory(Long id,String name);

在taotao-content-service编写实现类

/**
	 * 更新内容分类的name
	 */
	@Override
	public TaotaoResult updateContentCategory(Long id, String name) {
		//1.封装更新的name
		TbContentCategory tbContentCategory = new TbContentCategory();
		tbContentCategory.setId(id);
		tbContentCategory.setName(name);
		//2.更新内容分类表
		contentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
		return TaotaoResult.ok();
	}

3.2.4重命名内容分类表现层

在taotao-manager-web表写controller

/**
	 * 重命名内容分类
	 * @param id
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult updateContentCategory(Long id,String name){
		TaotaoResult result = contentCategoryService.updateContentCategory(id,name);
		return result;
	}

安装taotao-content、taotao-manager

启动taotao-manager、taotao-content、taotao-manager-web即可

猜你喜欢

转载自blog.csdn.net/pdsu161530247/article/details/81841382