前言:前面我们已经实现了文件上传和喝富文本编辑器的使用,下面主要是以下几个方面的知识:
1.前台系统的搭建和商城首页的展示
2.Cms系统的实现
a). 内容分类管理
b). 内容管理
1 前台系统搭建
先来看看效果
目前已经完成了后台管理系统和商品服务。淘淘商城首页展示需要用到商城门户工程和内容服务工程
下面是在idea中创建taotao-portal-web工程
首先创建一个maven工程
然后添加依赖
pom.xml
common-parent
com.lan
1.0-SNAPSHOT
../common-parent/pom.xml
4.0.0
taotao-portal-web
war
taotao-portal-web Maven Webapp
http://www.example.com
com.lan
common-utils
1.0-SNAPSHOT
org.springframework
spring-context
org.springframework
spring-beans
org.springframework
spring-webmvc
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.springframework
spring-jms
org.springframework
spring-context-support
jstl
jstl
javax.servlet
servlet-api
provided
javax.servlet
jsp-api
provided
com.alibaba
dubbo
spring
org.springframework
netty
org.jboss.netty
org.apache.zookeeper
zookeeper
com.github.sgroschupf
zkclient
junit
junit
org.apache.tomcat.maven
tomcat7-maven-plugin
/
8082
所需要的资源文件如下:
resource.properties为空
springmvc.xml配置
从springmvc.xml的配置中可以看到配置的controller的扫描包是com.taotao.portal.controller,我们在src/main/java目录下新建一个com.taotao.portal.controller包
web.xml
taotao-portal-web
index.html
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
taotao-portal-web
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
taotao-portal-web
*.html
科普:
我们可以看到我们拦截的请求都是以.html结尾的。这是一种伪静态;即我们请求的时候是以.html结尾。但是我们响应的是动态数据。但是由于请求是以.html结尾的,浏览器便认为要访问静态资源。SEO喜欢收录以.html结尾的请求,这样有利于搜索排名
导入静态资源
访问首页:
先修改web.xml
把index.jsp换成index.html。因为我们请求的时候输入的是index.html。由于这个界面是不存在的;DispatcherServlet就会处理请求,将请求传递到Controller中,执行对应的方法;返回的时候返回Index。由于我们配置了视图解析器。这样会自动跳到index.jsp的页面中
@Controller
public class PageController {
@RequestMapping("/index")
public String showIndex() {
return "index";
}
}
2. Cms内容管理系统
2.1 我们先来分析一下前台显示的内容的表设计。
我们来看看淘宝
可以看到淘宝首页展示的内容是分组的;轮播图是一组。下面的一个框也是一组;右边的公告,规则等是一组。我们点击每一个轮播图或者一些图片都会跳到新的链接。从上面我们可以看出。我们应该对内容进行分类,每一个分类对应多条内容:如轮播图类,下面可以包括多个要展示的信息。这样我们就能获得两张表,一个是专门保存分类的。一个是专门保存分类里面的具体内容的;表的设计如下:
2.2 Cms系统的搭建
Cms系统的搭建和商品管理系统的步骤差不多:
大家可以看第一篇的介绍。这里要注意的是dao层和pojo层我们不用创建。直接调用以前的。新建后的工程如下:
下面是关于每个工程的配置:
taotao-content:
pom.xml:
4.0.0
com.lan
common-parent
1.0-SNAPSHOT
../common-parent/pom.xml
taotao-content
1.0-SNAPSHOT
pom
../taotao-content-interface
../taotao-content-service
com.lan
common-utils
1.0-SNAPSHOT
org.apache.tomcat.maven
tomcat7-maven-plugin
/
8083
配置taotao-content-interface的pom.xml文件,添加对taotao-manager-pojo的依赖
4.0.0
com.lan
taotao-content
1.0-SNAPSHOT
../taotao-content/pom.xml
taotao-content-interface
com.lan
taotao-manager-pojo
1.0-SNAPSHOT
配置taotao-content-service的pom.xml文件,添加taotao-manager-dao、taotao-content-interface、spring、dubbo的依赖
4.0.0
com.lan
taotao-content
1.0-SNAPSHOT
../taotao-content/pom.xml
taotao-content-service
war
com.lan
taotao-manager-dao
1.0-SNAPSHOT
com.lan
taotao-content-interface
1.0-SNAPSHOT
org.springframework
spring-context
org.springframework
spring-beans
org.springframework
spring-webmvc
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.springframework
spring-jms
org.springframework
spring-context-support
com.alibaba
dubbo
spring
org.springframework
netty
org.jboss.netty
org.apache.zookeeper
zookeeper
com.github.sgroschupf
zkclient
在taotao-content-service层配置框架所需要的配置文件:
我们把taotao-manager-service中的mybatis、properties、spring三个目录粘贴过来。SqlMapConfig.xml文件不用动,db.propertes也不用动;
需要修改的文件如下:
applicationContext-service.xml
dubbo服务名称修改为"taotao-content",dubbo服务向外暴露的端口为"20881"(每发布一个服务就要更改一个端口)
applicationContext-trans.xml
将applicationContext-trans.xml文件,修改切面的包为com.taotao.content.service
web.xml
taotao-content
index.jsp
contextConfigLocation
classpath:spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
2.3 内容分类管理
功能分析
我们再来看下content-category.jsp页面,这个页面就是内容分类页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
分析:
contentCategory是树形结构展示内容分类。
请求的url:/content/category/list
请求的参数:id,当前节点的id。第一次请求是没有参数,需要给默认值“0”
响应数据:List
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
4、把列表转换成List
这里我们可以确定请求的地址和返回的数据格式了。
Dao层:
使用逆向工程
Service:
参数:long parentId
返回值:List
发布服务:
Controller层:
注意:
这个刚开始的时候是没有参数的。因此需要设置默认值为0
接收服务:
在taotao-mamnger-web中增加依赖:
好了。基本的设置和编码就是这样~在启动之前记得要将taotao-manager和taotao-content导入到本地仓库
然后启动taotao-manager和taotao-manager-web,taotao-content就可以访问了。
分类列表展示有了。还有的就是新增,重命名,删除等操作。
新增
当我们点击添加的时候会跳到content-add.jsp的页面;分析add.jsp的页面
提交表单请求的url:/content/save
参数:表单的数据。使用pojo接收TbContent
返回值:TaotaoResult(json数据)
业务逻辑:
1、把TbContent对象属性补全。
2、向tb_content表中插入数据。
3、返回TaotaoResult
Dao层:
逆向工程
Service层:
@Override
public TaotaoResult addContent(TbContent tbContent) {
tbContent.setCreated(new Date());
tbContent.setUpdated(new Date());
tbContentMapper.insert(tbContent);
return TaotaoResult.ok();
}
在上面我们已经发布服务了。直接在Controller中添加对应的代码
@RequestMapping("/content/save")
@ResponseBody
public TaotaoResult addContent(TbContent content) {
return contentService.addContent(content);
}