一、搭建门户系统
1、创建taotao-poral工程,打包方式为war
2、添加tomcat插件
<build>
<plugins>
<!-- 配置tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8083</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
3、加入配置文件
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取properties资源文件 -->
<!-- <context:property-placeholder location="classpath:resources/env.properties" /> -->
<!-- 配置controller扫描 -->
<context:component-scan base-package="com.taotao.portal.controller" />
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 ,配置前缀和后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置dubbo服务 -->
<dubbo:application name="taotao-portal-web" />
<!-- 使用广播
<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<!-- 使用zookeeper注册中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.37.161:2181" />
<!-- 声明要调用的服务,timeout是设置连接超时最长时间,如果不设置,超时时间默认是3秒 -->
<!-- <dubbo:reference interface="com.taotao.manager.service.TestService"
id="testService" timeout="1000000" /> -->
</beans>
log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
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>taotao-portal</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>
<!-- 解决POST乱码问题 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springMVC前端控制器 -->
<servlet>
<servlet-name>taotao-portal</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- springMVC全局配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- springmvc随着容器的启动而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-portal</servlet-name>
<!-- 以.html结尾的请求进入SpringMVC,作用是用于SEO优化 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
注意:使用 *.html结尾的拦截方式是为了进行SEO搜索引擎优化,搜素引擎的算法有分数,静态页面的分数比动态(jsp、asp、php)页面要高
4、导入门户系统静态页面
注意:静态页面在本博客资源中存在
5、实现访问门户首页
(1)、IndexController.java
package com.taotao.portal.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("index")
public class IndexController {
/**
* 访问系统首页
*/
@RequestMapping(method = RequestMethod.GET)
public String toIndex(){
return "index";
}
}
(2)、使用SwitchHosts修改域名映射
(3)、修改nginx配置文件
(4)、重启nginx,访问门户网站效果图
二、内容管理系统准备工作
1、导入表结构
2、编写对应的pojo类
package com.taotao.manager.pojo;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "tb_content")
public class Content extends BasePojo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "category_id")
private Long categoryId;
private String title;
@Column(name = "sub_title")
private String subTitle;
@Column(name = "title_desc")
private String titleDesc;
private String url;
private String pic;
private String pic2;
private String content;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public String getTitleDesc() {
return titleDesc;
}
public void setTitleDesc(String titleDesc) {
this.titleDesc = titleDesc;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getPic2() {
return pic2;
}
public void setPic2(String pic2) {
this.pic2 = pic2;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.taotao.manager.pojo;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "tb_content_category")
public class ContentCategory extends BasePojo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "parent_id")
private Long parentId;
private String name;
private Integer status;
@Column(name = "sort_order")
private Integer sortOrder;
@Column(name = "is_parent")
private Boolean isParent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getSortOrder() {
return sortOrder;
}
public void setSortOrder(Integer sortOrder) {
this.sortOrder = sortOrder;
}
public Boolean getIsParent() {
return isParent;
}
public void setIsParent(Boolean isParent) {
this.isParent = isParent;
}
// 扩展字段,用于EasyUI中tree结构
public String getText() {
return getName();
}
public String getState() {
return getIsParent() ? "closed" : "open";
}
}
3、编写内容对应的Mapper接口
package com.taotao.manager.mapper;
import com.github.abel533.mapper.Mapper;
import com.taotao.manager.pojo.Content;
/**
* 内容管理
* @author Administrator
*
*/
public interface ContentMapper extends Mapper<Content> {
}
package com.taotao.manager.mapper;
import com.github.abel533.mapper.Mapper;
import com.taotao.manager.pojo.ContentCategory;
/**
* 内容分类
* @author Administrator
*
*/
public interface ContentCategoryMapper extends Mapper<ContentCategory> {
}
4、编写Service层接口
package com.taotao.manager.service;
import com.taotao.manager.pojo.Content;
/**
* 内容业务层接口
* @author Administrator
*
*/
public interface ContentService extends BaseService<Content> {
}
package com.taotao.manager.service;
import com.taotao.manager.pojo.ContentCategory;
/**
* 内容分类业务层接口
* @author Administrator
*
*/
public interface ContentCategoryService extends BaseService<ContentCategory> {
}
5、编写业务层实现类
package com.taotao.manager.service.impl;
import org.springframework.stereotype.Service;
import com.taotao.manager.pojo.ContentCategory;
import com.taotao.manager.service.ContentCategoryService;
/**
* 内容分类业务层实现类
* @author Administrator
*
*/
@Service
public class ContentCategoryServiceImpl extends BaseServiceImpl<ContentCategory> implements ContentCategoryService {
}
package com.taotao.manager.service.impl;
import org.springframework.stereotype.Service;
import com.taotao.manager.pojo.Content;
import com.taotao.manager.service.ContentService;
/**
* 内容业务层实现类
* @author Administrator
*
*/
@Service
public class ContentServiceImpl extends BaseServiceImpl<Content> implements ContentService{
}
6、在配置文件中声明服务
7、在taotao-manager-web中创建controller类
package com.taotao.manager.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.taotao.manager.service.ContentCategoryService;
/**
* 内容分类web层
* @author Administrator
*
*/
@Controller
@RequestMapping("content/category")
public class ContentCategoryController {
@Autowired
private ContentCategoryService contentCategoryService;
}
package com.taotao.manager.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.taotao.manager.service.ContentService;
/**
* 内容web层
* @author Administrator
*
*/
@Controller
@RequestMapping("content")
public class ContentController {
@Autowired
private ContentService contentService;
}
8、在springmvc.xml中声明服务的调用
三、实现内容分类管理
1、内容分类管理树的展示实现
(1)、前端页面分析
(2)、后台Controller代码实现
/**
* 根据父ID查询内容分类列表
*
* @param parentId
* @return
*/
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<ContentCategory> queryContentCategoryByParentId(
@RequestParam(value = "id", defaultValue = "0") Long parentId) {
List<ContentCategory> list = this.contentCategoryService.queryContentCategroyByParentId(parentId);
return list;
}
(3)、Service接口和实现类层代码实现
/**
* 根据父ID查询内容分类列表
* @param parentId
* @return
*/
public List<ContentCategory> queryContentCategroyByParentId(Long parentId);
/**
* 根据父ID查询内容分类列表
* @param parentId
* @return
*/
public List<ContentCategory> queryContentCategroyByParentId(Long parentId) {
//封装查询条件
ContentCategory contentCategory = new ContentCategory();
contentCategory.setParentId(parentId);
// 执行查询操作
List<ContentCategory> list = super.queryListByWhere(contentCategory);
return list;
}
2、内容分类管理树的动态管理前端代码分析
3、实现内容管理树的添加功能
(1)、Controller代码实现
/**
* 新增内容分类
*
* @param contentCategory
* @return
*/
@RequestMapping(value = "add", method = RequestMethod.POST)
@ResponseBody
public ContentCategory saveContentCategory(ContentCategory contentCategory) {
// 调用服务保存内容分类
ContentCategory result = this.contentCategoryService.savenContentCategory(contentCategory);
return result;
}
(2)、Service层接口和实现类代码实现
/**
* 新增内容分类
* @param contentCategory
* @return
*/
public ContentCategory savenContentCategory(ContentCategory contentCategory);
/**
* 新增内容分类
*
* @param contentCategory
* @return
*/
public ContentCategory savenContentCategory(ContentCategory contentCategory) {
// 设置需要保存的内容分类,设置状态为正常
contentCategory.setIsParent(false);
contentCategory.setStatus(1);
// 把内容分类保存到数据库中
super.save(contentCategory);
// 查询父节点,直接根据父节点id查询父节点
ContentCategory parent = super.queryById(contentCategory.getParentId());
// 判断父节点的isParent是否为true
if (!parent.getIsParent()) {
// 如果不是true,需要修改为true
parent.setIsParent(true);
// 更新修改后的父节点
super.updateByIdSelective(parent);
}
return contentCategory;
}
(3)、运行测试
4、内容分类管理树的修改功能实现
ContentCategoryController.java
/**
* 修改的内容分类
*
* @param contentCategory
*/
@RequestMapping(value = "update", method = RequestMethod.POST)
@ResponseBody
public void updateContentCategory(ContentCategory contentCategory) {
this.contentCategoryService.updateByIdSelective(contentCategory);
}
5、内容分类管理树的删除功能实现
(1)、Controller代码
/**
* 删除内容分类
* @param contentCategory
*/
@RequestMapping(value = "delete", method = RequestMethod.POST)
@ResponseBody
public String deleteContentCategory(Long parentId, Long id) {
this.contentCategoryService.delteContentCategory(parentId, id);
return "";
}
(2)、Service层接口和实现类
/**
* 删除内容分类
* @param contentCategory
*/
public void delteContentCategory(Long parentId, Long id);
/**
* 删除内容分类
*
* @param contentCategory
*/
public void delteContentCategory(Long parentId, Long id) {
// 获取所有要删除的节点id
// 声明存放id的容器
List<Object> ids = new ArrayList<Object>();
// 把自己放入容器中
ids.add(id);
// 使用递归方式获取需要删除的节点
this.getIds(ids, id);
// 使用删除方法批量删除节点
super.deleteByIds(ids);
// 查询删除后还有没有兄弟节点
// 声明查询条件
ContentCategory param = new ContentCategory();
param.setParentId(parentId);
// 执行查询
Integer count = super.queryCountByWhere(param);
if (count == 0) {
// 如果没有兄弟节点,父节点变为叶子节点,也就是isParent设置为false
ContentCategory parent = new ContentCategory();
// 设置id
parent.setId(parentId);
// 设置isParent
parent.setIsParent(false);
// 更新父节点
super.updateByIdSelective(parent);
}
// 如果有兄弟节点,父节点不变
}
/**
* 使用递归方法获取要删除的节点
*
* @param ids
* @param id
*/
private void getIds(List<Object> ids, Long id) {
// 查询所有子节点
// 声明查询条件
ContentCategory param = new ContentCategory();
param.setParentId(id);
// 执行查询方法
List<ContentCategory> list = super.queryListByWhere(param);
// 使用递归方法,必须要设置递归结束的条件
// 结束条件:判断是否有子节点,如果有,就进行递归,如果没有叶子节点,就停止递归
if (list.size() > 0) {
// 遍历查询到的子节点
for (ContentCategory contentCategory : list) {
// 把子节点的id放到ids容器中
ids.add(contentCategory.getId());
// 进行递归自己调用自己,获取自己的所有子节点
this.getIds(ids, contentCategory.getId());
}
}
}
四、实现内容管理
1、前端内容管理页面显示内容分类树的代码分析
2、前端内容管理页面中添加按钮的页面分析
3、内容管理页面中内容添加操作后台代码实现
(1)、Controller层代码ContentController.java
package com.taotao.manager.controller;
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.TaoResult;
import com.taotao.manager.pojo.Content;
import com.taotao.manager.service.ContentService;
/**
* 内容web层
*
* @author Administrator
*
*/
@Controller
@RequestMapping("content")
public class ContentController {
@Autowired
private ContentService contentService;
/**
* 新增内容
*
* @param content
*/
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public void saveContent(Content content) {
this.contentService.save(content);
}
/**
* 获取内容的分页数据
*
* @param page
* @param rows
* @return
*/
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public TaoResult<Content> queryContentByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "rows", defaultValue = "20") Integer rows, Long categoryId) {
TaoResult<Content> result = this.contentService.queryContentByPage(page, rows, categoryId);
return result;
}
}
(2)、Service层代码 -- ContentService.java、ContentServiceImpl.java
/**
* 获取内容的分页数据
*
* @param page
* @param rows
* @return
*/
public TaoResult<Content> queryContentByPage(Integer page, Integer rows, Long categoryId);
/**
* 获取内容的分页数据
*
* @param page
* @param rows
* @return
*/
public TaoResult<Content> queryContentByPage(Integer page, Integer rows, Long categoryId) {
// 设置分页数据
PageHelper.startPage(page, rows);
// 设置查询条件
Content param = new Content();
param.setCategoryId(categoryId);
// 执行查询
List<Content> list = super.queryListByWhere(param);
// 封装返回数据
PageInfo<Content> pageInfo = new PageInfo<Content>(list);
TaoResult<Content> taoResult = new TaoResult<Content>(pageInfo.getTotal(), list);
return taoResult;
}
4、浏览器页面展示