壹立方商城-Day05

前台系统搭建

商城架构

e3-portal-web工程搭建

e3-portal-web工程的pom.xml配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.e3mall</groupId>
		<artifactId>e3-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.e3mall</groupId>
	<artifactId>e3-portal-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<!-- 未必要用,暂时注释 -->
		<!-- <dependency>
			<groupId>cn.e3mall</groupId>
			<artifactId>e3-content-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency> -->
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<!-- JSP相关 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.jboss.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
	</dependencies>
	<!-- 配置tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<!-- 后台的端口是8081,这里需要改一下 -->
					<port>8082</port>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

复制e3-manager-web工程的配置文件



修改配置文件

因为在前台商城首页不做图片上传的功能,所以要把服务器地址的配置文件删除

删除属性文件的内容

修改配置springmvc.xml

需要删除的地方

我们的商城首页的拦截器配置的是.action所以不需要配置资源映射

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:conf/resource.properties"/>
	<!-- 配置controller扫描 -->
	<context:component-scan base-package="cn.e3mall.portal.controller" />
	<!-- 开启注解驱动 -->
	<mvc:annotation-driven />
    <!--视图解析器-->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 引用dubbo服务 -->
	<dubbo:application name="e3-portal-web"/>
	<dubbo:registry protocol="zookeeper" address="192.168.25.129:2181"/>	
	<!-- <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" /> -->
</beans>

生成web.xml配置文件

e3-portal-web工程的web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>e3-portal-web</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

	<!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- springmvc的前端控制器 -->
	<servlet>
		<servlet-name>e3-portal-web</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>e3-portal-web</servlet-name>
		<!-- 伪静态化 --> 
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
</web-app>

至此,项目e3-portal-web工程搭建结束!!!!

商城首页展示

默认首页

如何实现只输入localhost:8082就跳转到首页

原理解析

当浏览器输入localhost:8082,请求先找欢迎页

欢迎页默认的找的页面是webapps下的index.html

可以看出webapps下没有index.html

此时请求就会进入到Servlet拦截器中,拦截器中配置的是拦截.html

此时请求就会进入到Controller中

根据springmvc.xml里的视图解析器配置去找index.jsp

启动项目

项目上右键----Run As--Maven Build----输入clean tomcat7:run

启动之后查看页面,输入localhost:8082,页面效果如下

至此,首页首页展示结束!!!

CMS内容管理系统分析


首页广告位开发实现


当我们打开商城首页,会发现很多广告位,这些广告位的广告都是动态从数据库中取出的,我们可以设计一张表存放大广告位的广告,其他部分同样可以设计表,进行增删改查。

 存在的问题:

如果每一个前端展示内容(大广告位、小广告位等等),单独建立表,进行CRUD操作,会有以下问题:

1.首页页面信息大量堆积,发布显的异常繁琐沉重;

2.内容繁杂,管理效率低下;

3.许多工作都需要其他技术人员配合完成;

4.改版工作量大,维护,扩展性差;

使用内容管理系统解决以上问题。

内容管理系统介绍


内容管理系统(content management system,CMS)是一种位于WEB 前端(Web 服务器)和后端办公系统或流程(内容创作、编辑)之间的软件系统。内容的创作人员、编辑人员、发布人员使用内容管理系统来提交、修改、审批、发布内容。这里指的“内容”可能包括文件、表格、图片、数据库中的数据甚至视频等一切你想要发布到Internet网站的信息。

就是后台管理维护前台的页面和页面中的内容可以动态展示。

首页动态展示分析


对首页展示功能进行分析,抽取,发现应当有以下的字段属性:图片、链接、标题、价格、图片提示等

把首页的每个展示功能(大广告位,中广告,小广告等),看作是一个分类

每个展示功能里面展示的多条信息,看作是分类下的内容

例如:首页大广告,对应的是大广告分类,而大广告位展示的多张图片,就是大广告分类下的内容

前台需要获取大广告的图片,只需要根据大广告的id查询对应的内容即可

需要一个内容分类表和一个内容表。内容分类和内容表是一对多的关系。

内容分类表对应tb_content_category表

内容表对应tb_content表

需要有后台来维护内容信息CMS系统。

需要创建一个内容服务系统

CMS内容管理系统搭建


可以参考e3-manager创建。

e3-content:聚合工程打包方式pom

          |--e3-content-interface  jar

          |--e3-content-service  war

          //直接依赖e3-manager-pojo

          //直接依赖e3-manager-dao

e3-content

创建结果如下

配置pom.xml文件

pom文件需要依赖common工程,在Tomcat插件配置中将端口号改为8083,因为8081,8082端口已经被用过

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.e3mall</groupId>
		<artifactId>e3-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.e3mall</groupId>
	<artifactId>e3-content</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<!-- 依赖 -->
	<dependencies>
		<dependency>
			<groupId>cn.e3mall</groupId>
			<artifactId>e3-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<!-- 配置tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<port>8083</port>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- 子工程 -->
	<modules>
		<module>e3-content-interface</module>
		<module>e3-content-service</module>
	</modules>
</project>

e3-content-interface

创建结果如下

配置pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>cn.e3mall</groupId>
    <artifactId>e3-content</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>e3-content-interface</artifactId>
  <!-- 依赖 -->
  <dependencies>
	<dependency>
		<groupId>cn.e3mall</groupId>
		<artifactId>e3-manager-pojo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</dependency>
  </dependencies>
</project>

content-service

service层的打包方式是war,因为一开始创建的时候没有注意到,所以在创建后做了修改

创建结果如下

配置pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.e3mall</groupId>
		<artifactId>e3-content</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>e3-content-service</artifactId>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>cn.e3mall</groupId>
			<artifactId>e3-manager-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>cn.e3mall</groupId>
			<artifactId>e3-content-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.jboss.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
	</dependencies>
</project>

此时e3-content-service项目会报错,因为打包方式为war,所以需要在src/main/webapp下创建WEB-INF/web.xml

配置web.xml文件

web.xml文件配置,主要是为了加载一个spring容器,将spring文件夹里的配置文件都加载进来

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>e3-content</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

框架整合

将e3-manager-service的src/main/resources下的mybatis、数据库连接、spring等配置复制到e3-content-service的src/main/resources里面。

SqlMapConfig.xml

不用修改

db.properties

不用修改

applicationContext-dao.xml

直接使用的是e3-manager-dao,所以也不用修改

applicationContext-service.xml

需要修spring注解扫描的包和dubbo协议暴露服务的端口,因为taotao-manager-service已经占用20880端口
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 配置包扫描器 -->
	<context:component-scan base-package="cn.e3mall.content.service" />
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-content" />
	<dubbo:registry protocol="zookeeper" address="192.168.25.129:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 一个端口对应一个服务,服务变了,端口也要变20881 -->
	<dubbo:protocol name="dubbo" port="20881" />
	<!-- 声明需要暴露的服务接口 发布服务 -->
	<!-- <dubbo:service interface="cn.e3mall.service.ItemService"
		ref="itemServiceImpl" timeout="600000" /> -->
</beans>

applicationContext-trans.xml

修改切面即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.e3mall.content.service..*.*(..))" />
	</aop:config>
</beans>

内容管理系统启动

安装e3-content、e3-content-interface,启动e3-content-service

Cms系统实现

完成后效果展示

主界面

新增内容分类界面

右键添加

添加界面

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

重命名内容分类界面

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

内容分类管理----显示页面

前端分析(可以简单看一下)

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

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

这张图是主图,上面的分析感兴趣的话可以看,对理解页面很有帮助

请求的url:/content/category/list

请求的参数:id,当前节点的id。第一次请求是没有参数,需要给默认值“0”

响应数据:List<EasyUITreeNode>(@ResponseBody)

Json数据。

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

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

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

业务逻辑:

1、取查询参数id,parentId

2、根据parentId查询tb_content_category,查询子节点列表。

3、得到List<TbContentCategory>

4、把列表转换成List<EasyUITreeNode>

代码实现

服务层e3-content-interface

在e3-content-interface创建cn.e3mall.content.service包,编写接口

ContentCategoryService.java

package cn.e3mall.content.service;

import java.util.List;

import cn.e3mall.common.pojo.EasyUITreeNode;

public interface ContentCategoryService {

	/**
	 * 查询内容列表
	 * 
	 * @param parentId
	 * @return List
	 */
	List<EasyUITreeNode> getContentCatList(long parentId);
}

服务层e3-content-service

在e3-content-service创建cn.e3.content.service.impl包编写实现类

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

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

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

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.content.service.ContentCategoryService;
import cn.e3mall.mapper.TbContentCategoryMapper;
import cn.e3mall.pojo.TbContentCategory;
import cn.e3mall.pojo.TbContentCategoryExample;
import cn.e3mall.pojo.TbContentCategoryExample.Criteria;

/**
 * 内容分类管理Service
 * <p>
 * Title: ContentCategoryServiceImpl
 * </p>
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;

	@Override
	public List<EasyUITreeNode> getContentCatList(long parentId) {
		// 根据parentid查询子节点列表
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		// 设置查询条件
		criteria.andParentIdEqualTo(parentId);
		// 执行查询
		List<TbContentCategory> catList = contentCategoryMapper.selectByExample(example);
		// 转换成EasyUITreeNode的列表
		List<EasyUITreeNode> nodeList = new ArrayList<>();
		for (TbContentCategory tbContentCategory : catList) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent() ? "closed" : "open");
			// 添加到列表
			nodeList.add(node);
		}
		return nodeList;
	}
}

发布服务

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

<!-- 声明需要暴露的服务接口 发布服务  服务层暴露出来以后,表现层(web层)才能引用-->
<dubbo:service interface="cn.e3mall.content.service.ContentCategoryService"
    ref="contentCategoryServiceImpl" timeout="600000" />

表现层e3-manager-web

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

依赖添加在e3-manager-web/pom.xml文件中的dependencies标签内

<dependency>
	<groupId>cn.e3mall</groupId>
	<artifactId>e3-content-interface</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

引用服务,在springmvc.xml文件中引用service层暴露的服务

<dubbo:reference interface="cn.e3mall.content.service.ContentCategoryService" id="contentCategoryService" />

在e3-manager-web创建controller

ContentCatController.java

package cn.e3mall.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 cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.content.service.ContentCategoryService;

/**
 * 内容分类管理Controller
 * <p>Title: ContentCatController</p>
 * @version 1.0
 */
@Controller
public class ContentCatController {

	@Autowired
	private ContentCategoryService contentCategoryService;
	
	@RequestMapping("/content/category/list")
	@ResponseBody
	public List<EasyUITreeNode> getContentCatList(
			@RequestParam(name="id", defaultValue="0")Long parentId) {
		List<EasyUITreeNode> list = contentCategoryService.getContentCatList(parentId);
		return list;
	}
	
}

安装(maven install)e3-content。启动e3-content(相当于创建了一个spring容器,利用tomcat)、e3-manager、e3-manager-web即可

内容分类管理----添加

前端分析

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

当用户点击菜单栏上的添加、重命名等按钮时会触发onClick:menuHandler函数。下面menuHandler函数注释写的很明确

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

新增节点时发起的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与重命名。

代码实现

添加内容分类业务逻辑

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

添加----服务层

dao层

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

TbContentCategoryMapper.xml

<!-- keyProperty:TbContentCategory主键的属性名
 resultType:主键的数据类型 写的是long但是这是Long类型数据的别名
 order:在插入数据之后才能取得主键的值,所以配置为after
 SELECT LAST_INSERT_ID():取到最后生成的主键
 -->
<selectKey keyProperty="id" resultType="long" order="AFTER">
	SELECT LAST_INSERT_ID()
</selectKey>

修改之后 maven install一下e3-manager-dao工程

service层

在e3-content-interface编写接口ContentCategoryService.java

/**
 * 添加内容分类
 * @param parentId
 * @param name
 * @return
 */
E3Result addContentCategory(long parentId, String name);

在e3-content-service中编写实现类ContentCategoryServiceImpl.java

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

@Override
	public E3Result addContentCategory(long parentId, String name) {
		//创建一个tb_content_category表对应的pojo对象
		TbContentCategory contentCategory = new TbContentCategory();
		//设置pojo的属性
		contentCategory.setParentId(parentId);
		contentCategory.setName(name);
		//1(正常),2(删除)
		contentCategory.setStatus(1);
		//默认排序就是1
		contentCategory.setSortOrder(1);
		//新添加的节点一定是叶子节点
		contentCategory.setIsParent(false);
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		//插入到数据库
		contentCategoryMapper.insert(contentCategory);
		//判断父节点的isparent属性。如果不是true改为true
		//根据parentid查询父节点
		TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(parentId);
		if (!parent.getIsParent()) {
			parent.setIsParent(true);
			//更新到数数据库
			contentCategoryMapper.updateByPrimaryKey(parent);
		}
		//返回结果,返回E3Result,包含pojo
		return E3Result.ok(contentCategory);
	}

添加----表现层

在e3-manager-web编写controller

请求的url:/content/category/create

请求的参数:

Long parentId

String name

响应的结果:

json数据,E3Result

ContentCatController.java

        /**
	 * 添加分类节点
	 */
	@RequestMapping(value="/content/category/create", method=RequestMethod.POST)
	@ResponseBody
	public E3Result createContentCategory(Long parentId, String name) {
		//调用服务添加节点
		E3Result e3Result = contentCategoryService.addContentCategory(parentId, name);
		return e3Result;
	}

启动

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

内容分类管理----删除

前端分析

代码实现

删除----服务层

删除----表现层

内容分类管理----修改

前端分析

代码实现

修改----服务层

修改----表现层

内容管理

完成效果展示

加载内容列表

点击叶子节点,会根据内容分类加载内容列表

新增内容

选择叶子节点,点击新增

填写内容后。

点击提交后,内容列表会更新内容。

加载内容列表

前端分析

首先用户点击内容管理时,会请求url:'/content/category/list,加载内容分类的列表,这个在之前已经实现过了。

当用户点击内容分类列表节点时会触发onClick,if(tree.tree("isLeaf",node.target)){}判断用户点击的是否是叶子节点,如果是则会

datagrid.datagrid('reload', { categoryId :node.id});重新加载datagrid,也就是数据表格。

加载之前发送GET请求url:'/content/list',queryParams:{categoryId:0},并会默认加载"categoryId=0"的内容列表

后端代码实现

加载内容时要注意,内容列表使用了easyui的分页工具,会发送page:当前所在页与rows:分页大小。

关于easyui的分页分析(分析链接

内容管理----修改

前端分析

在content.jsp,加载完easyui-datagrid时,也就是内容列表。会加载toolbar:contentListToolbar,这个叫contentListToolbar的toolbar在content.jsp的JavaScript部分。

这个toolbar会生成新增、编辑、删除等按钮。

var node = $("#contentCategoryTree").tree("getSelected");获取用户选中的节点。

然后if(!node || !$("#contentCategoryTree").tree("isLeaf",node.target))判断用户是否选中节点或者选中的是叶子节点,如果不满足则弹窗提示。

TT.createWindow({ url : "/content-add"}); 是将url作为参数传入TT.createWindow()函数。

TT.createWindow()函数在common.js中。createWindow(params)函数从参数params中获取width、height等属性,如果参数没有就是要默认值。我们传入的参数为:{url : "/content-add"},href : params.url被赋值,然后请求url : "/content-add"。会被

PageController的@RequestMapping("/{page}")拦截,返回/WEB-INF/jsp/content-add.jsp视图。

createWindow函数会自动触发onLoad函数,如果参数params中包含onLoad函数,就会回调传入参数的函数,但是新增按钮没有onLoad没有。

点击关闭时会触发$(this).window("destroy");关闭加载的创建(其实是content-add.jsp)。

来到content-add.jsp后,会加载富文本编辑器,创建TT.initOnePicUpload();单图片上传组件,将categoryId放到

[name=categoryId]的隐藏域表单表单中。

当用户点击提交后contentAddEditor.sync();会同步富文本编辑器的内容到textarea中,然后$("#contentAddForm").serialize()

将表单中的数据以key-value的形式,post请求url:"/content/save",新增成功后,提示新增成功,并重新加载内容列表。

代码实现

后端只需要拦截url:/content/save,使用pojo接收TbContent接收,保存到数据库中即可。

修改----服务层

dao层

使用逆向工程

service层

在e3-content-interface创建接口

ContentService.java

package cn.e3mall.content.service;

import cn.e3mall.common.utils.E3Result;
import cn.e3mall.pojo.TbContent;

public interface ContentService {
	/**
	 * 内容管理----添加
	 * @param content
	 * @return
	 */
	E3Result addContent(TbContent content);
}

在e3-content-service实现接口

ContentServiceImpl.java

package cn.e3mall.content.service.impl;

import java.util.Date;

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

import cn.e3mall.common.utils.E3Result;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.mapper.TbContentMapper;
import cn.e3mall.pojo.TbContent;

/**
 * 内容管理Service
 * <p>
 * Title: ContentServiceImpl
 * </p>
 * 
 * @version 1.0
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;

	/**
	 * 内容管理----添加
	 */
	@Override
	public E3Result addContent(TbContent content) {
		// 将内容数据插入到内容表
		content.setCreated(new Date());
		content.setUpdated(new Date());
		// 插入到数据库
		contentMapper.insert(content);
		return E3Result.ok();
	}

}

发布服务

在e3-content-service中的applicationContext-service.xml文件中添加

<dubbo:service interface="cn.e3mall.content.service.ContentService"
	ref="contentServiceImpl" timeout="600000" />

修改----表现层

引入服务

在e3-manager-web工程中springmvc.xml文件中添加:

<dubbo:reference interface="cn.e3mall.content.service.ContentService" id="contentService" />

在e3-manager-web编写controller

内容分类管理----删除

前端分析

代码实现

删除----服务层

删除----表现层

内容分类管理----删除

前端分析

代码实现

删除----服务层

删除----表现层

参考博文

原文:https://blog.csdn.net/pdsu161530247/article/details/81835099?utm_source=copy 

原文:https://blog.csdn.net/pdsu161530247/article/details/81871988?utm_source=copy 

猜你喜欢

转载自blog.csdn.net/qq_30916377/article/details/83074735
今日推荐