SSH实战OA 11:BBS模块

时间:2022-12-24 19:15:55

《SSH实战OA》系列博客的系统管理、权限管理等内容后面再补上吧,先继续第三个模块:网上交流模块。网上交流主要做两个需求:论坛管理和论坛。

BBS的一些基本术语:

  1. 板块:也叫做“版面”、“讨论区”,用于对帖子进行分类
  2. 主题:也叫做“主贴”,表示一个新的话题,可以有很多回帖,属于某个板块。
  3. 回复:也叫做“回帖”、“跟帖”,属于某个主贴。

论坛模块的功能说明:

  1. 浏览
    • 板块列表
    • 显示单个板块(主题列表)
    • 显示单个主题(主题+回帖列表)
  2. 参与
    • 发新帖
    • 回帖
  3. 管理文章
    • 主题
      • 设置类型
      • 移动到其他板块
      • 删除
      • 修改
    • 回复
  4. 板块管理
    • 增删改查
    • 上下移动

板块管理

SSH实战OA 11:BBS模块

先来看看板块管理的需求,由上图可以看出,板块管理主要的需求有板块的新增、删除,修改,列表,上移,下移这几个需求。那么对应的Action方法如下:

@Controller
@Scope("prototype")
public class ForumManageAction extends BaseAction<Forum> {

    Log log = LogFactory.getLog(this.getClass());

    /**
     * @return 板块列表
     * @throws Exception
     */
    public String list() throws Exception {
        List<Forum> forumList = forumService.selectAll();
        ActionContext.getContext().put("forumList", forumList);

        return "list";
    }

    /**
     * @return 新增页面
     * @throws Exception
     */
    public String addUI() throws Exception {
        return "saveUI";
    }

    /**
     * @return 新增操作
     * @throws Exception
     */
    public String add() throws Exception {
        forumService.add(model);
        return "toList";
    }

    /**
     * @return 删除操作
     * @throws Exception
     */
    public String delete() throws Exception {
        forumService.delete(model.getId());
        return "toList";
    }

    /**
     * @return 修改页面
     * @throws Exception
     */
    public String editUI() throws Exception {
        Forum forum  = forumService.selectById(model.getId());
        ActionContext.getContext().put("forum", forum);

        return "saveUI";
    }

    /**
     * @return 修改
     * @throws Exception
     */
    public String edit() throws Exception {
        Forum forum = forumService.selectById(model.getId());
        if(forum != null) {
            forum.setDescription(model.getDescription());
            forum.setName(model.getName());
            forumService.update(forum);
        }

        return "toList";
    }

    /**
     * @return 上移
     * @throws Exception
     */
    public String moveUp() throws Exception {
        forumService.moveUp(model.getId());
        return "toList";
    }

    /**
     * @return 下移
     * @throws Exception
     */
    public String moveDown() throws Exception {
        forumService.moveDown(model.getId());
        return "toList";
    }
}

论坛板块ForumAction需要继承基本Action抽象类BaseAction。

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

    protected T model;

    @Autowired
    protected DepartmentService departmentService;

    @Autowired
    protected RoleService roleService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected PrivilegeService privilegeService;

    @Autowired
    protected ForumService forumService;

    public BaseAction() {
        try {
            // 通过反射获取model的真实类型
            ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
            // 通过反射创建model的实例
            model = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public T getModel() {
        return model;
    }
}

在暂未考虑与其他实体关联的提前下,我们的model类可以这样设计:

/**
 * @date 2017/05/06
 * @author shizongger
 * 论坛板块
 */
public class Forum {

    private Long id; //主键id

    private String name; //板块名称

    private String description; //板块描述

    private int position;  //板块所在位置

    //getter/settter
}

前几篇文章提到映射的pojo的主键属性id都默认为Long型,forum属性自己的属性有name,description,position。name用来记录板块名称,description是对本板块的描述,而position是记录板块的排序位置,方面上下移动的操作。

Forum.hbm.xml文件如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.shizongger.oa.domain">

    <class name="Forum" table="itcast_forum">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name" />
        <property name="description" />
        <property name="position" />

    </class>
</hibernate-mapping>

String类型的映射都用Hibernate默认的配置。别忘了在Hibernate的配置文件hiberante.cfg.xml添加本文件的位置。

<mapping resource="com/shizongger/oa/domain/Forum.hbm.xml" />

由于目前我采用的是两层架构,合并和Serivce层和Dao层,所以我把Dao层对数据库基本增删改查都抽象到DaoSupport抽象类里。这是一个泛型参数的抽象类,具体传递进来的model类型属于什么类型是在构造方法中通过java反射机制得到的。

/**
 * @author shizongger
 * @param <T> 实际操作的daomain实体
 */
@Transactional
@SuppressWarnings("unchecked")
public abstract class DaoSupportImpl<T> implements DaoSupport<T> {

    private Log log = LogFactory.getLog(this.getClass());   

    /**
     * sessionFactory工厂
     */
    @Autowired
    private SessionFactory sessionFactory;

    private Class<T> clazz;

    @SuppressWarnings("unchecked")
    public DaoSupportImpl() {
        // 使用反射技术得到T的真实类型
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
        this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
    }

    /**
     * 增加
     */
    @Override
    public void add(T entity) {
        log.info("add:" + entity.toString());
        getSession().save(entity);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Long id) {
        Object object = selectById(id);
        if(object != null) {
            getSession().delete(object);
        }
    }

    /**
     * 修改
     */
    @Override
    public void update(T entity) {
        getSession().update(entity);
    }

    /**
     * 根据id查询
     */
    @Override
    public T selectById(Long id) {
        return (T) getSession().get(this.clazz, id);
    }

    /**
     * 根据id数组查找对象集合
     * @param ids id的列表
     * @return
     */
    @Override
    public List<T> getByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Collections.EMPTY_LIST;
        } else {
            return getSession().createQuery(//
                    "FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")//
                    .setParameterList("ids", ids)//
                    .list();
        }
    }

    /**
     * 根据id数组查询
     */
    @Override
    public List<T> selectAll() {
        List<T> list = getSession().createQuery("FROM " + this.clazz.getSimpleName()).list();

        return list;
    }

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }
}

论坛管理的Service实现类代码如下:

@Service
@Transactional
@SuppressWarnings("unchecked")
public class ForumServiceImpl extends DaoSupportImpl<Forum> implements ForumService {

    Log log = LogFactory.getLog(this.getClass());

    @Override
    public void add(Forum forum) {
        super.add(forum);
        forum.setPosition(forum.getId().intValue());
    }

    @Override
    public List<Forum> selectAll() {
        return getSession()
                .createQuery("FROM Forum f ORDER BY f.position")
                .list();
    }

    /**
     * 上移当前板块forum的位置position值
     */
    @Override
    public void moveUp(Long id) {
        //获取当前板块
        Forum forum = selectById(id);
        //上一个forum
        Forum prexForum = (Forum)getSession()
                            .createQuery("FROM Forum f WHERE f.position < ? ORDER BY f.position DESC")
                            .setParameter(0, forum.getPosition())
                            .setFirstResult(0)
                            .setMaxResults(1)
                            .uniqueResult();
        //最上面的不能再往上移动
        if(prexForum == null) {
            return;
        }

        //交换当前和上一个的position
        int position = forum.getPosition();
        forum.setPosition(prexForum.getPosition());
        prexForum.setPosition(position);

        //更新两个对象到数据库中
        getSession().save(forum);
        getSession().save(prexForum);
    }

    /**
     * 向下移动当前板块
     */
    @Override
    public void moveDown(Long id) {
        //获取当前板块
        Forum forum = selectById(id);

        //下一个forum
        Forum nextForum = (Forum)getSession()
                            .createQuery("FROM Forum f WHERE f.position > ? ORDER BY f.position ASC")
                            .setParameter(0, forum.getPosition())
                            .setFirstResult(0)
                            .setMaxResults(1)
                            .uniqueResult();    

        //最下面的不能再往下移
        if(nextForum == null) {
            return;
        }

        //交换当前forum和下一个forum的position
        int position = nextForum.getPosition();
        nextForum.setPosition(forum.getPosition());
        forum.setPosition(position);

        //更新两个对象到数据库中去
        getSession().save(nextForum);
        getSession().save(forum);
    }
}

增删改查功能只需要把model为Forum传递进去调用DaoSupport就行了,上移和下移的思路是jsp传递forum进来,先从数据库获得一个forum对象。如果是上移,则获取数据库中position所有小于本forum.position的那个最大的值。因为只要不是最上面的板块,小于自己position的板块可能有多个,而我们只需要最大的那个,也就是仅仅挨着自己的那个板块。然后交换两者的position值。

前端列表list.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
    <title>版块列表</title>
    <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
    <script type="text/javascript">
    </script>
    <style type="text/css">
        .disabled{
            color: gray;
            cursor: pointer;
        }
    </style>
</head>
<body>

<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--页面标题-->
            <img src="${pageContext.request.contextPath }/style/images/title_arrow.gif" width="13" height="13" border="0"> 版块管理
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<div id="MainArea">
    <table class="TableStyle" cellspacing="0" cellpadding="0">

        <!-- 表头-->
        <thead>
            <tr id="TableTitle" valign="MIDDLE" align="CENTER">
                <td width="250px">版块名称</td>
                <td width="300px">版块说明</td>
                <td>相关操作</td>
            </tr>
        </thead>

        <!--显示数据列表-->
        <tbody id="TableData" class="dataContainer" datakey="forumList">

        <!-- 遍历forumList -->
        <s:iterator value="#forumList" status="status">
            <tr class="TableDetail1 demodata_record">
                <td>${name }&nbsp;</td>
                <td>${description }&nbsp;</td>
                <td>
                    <s:a action="forumManage_delete?id=%{id}" onclick="return delConfirm()">删除</s:a>
                    <s:a action="forumManage_editUI?id=%{id }">修改</s:a>
                    <!-- 最上面不能往上移 -->
                    <s:if test="#status.first">
                        <span class="disabled">上移</span>
                    </s:if>
                    <s:else>
                        <s:a action="forumManage_moveUp?id=%{id }">上移</s:a>
                    </s:else>
                    <!-- 最下面的不能再往下移动 -->
                    <s:if test="#status.last">
                        <span class="disabled">下移</span>
                    </s:if>
                    <s:else>
                        <s:a action="forumManage_moveDown?id=%{id }">下移</s:a>
                    </s:else>
                </td>
            </tr>
        </s:iterator>
        </tbody>
    </table>

    <!-- 其他功能超链接 -->
    <div id="TableTail">
        <div id="TableTail_inside">
            <a href="forumManage_addUI.action"><img src="${pageContext.request.contextPath }/style/images/createNew.png"></a>
        </div>
    </div>
</div>

<div class="Description">
    说明:<br>
    1,显示的列表按其sortOrder值升序排列。<br>
    2,可以通过上移与下移功能调整顺序。最上面的不能上移,最下面的不能下移。<br>
</div>

</body></html>

新增和修改的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
    <title>版块设置</title>
    <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
    <script type="text/javascript">
    </script>
</head>
<body>

<!-- 标题显示 -->
<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--页面标题-->
            <img src="${pageContext.request.contextPath }/style/images/title_arrow.gif" width="13" height="13" border="0"> 版块设置
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<!--显示表单内容-->
<div id="MainArea">
    <s:form action="forumManage_%{id == null ? 'add' : 'edit'}">
        <!-- 隐藏表单内容 -->
        <s:hidden name="id" value="%{#request.forum.id}"></s:hidden>

        <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
            <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath }/style/blue/images/item_point.gif" /> 版块信息 </DIV>  -->
        </div>

        <!-- 表单内容显示 -->
        <div class="ItemBlockBorder">
            <div class="ItemBlock">
                <table class="mainForm" cellspacing="0" cellpadding="0">
                    <tbody>
                        <tr>
                            <td width="100">版块名称</td>
                            <td><s:textfield name="name" cssClass="InputStyle" value="%{#request.forum.name}" > *</s:textfield></td>
                        </tr>
                        <tr>
                            <td>版块说明</td>
                            <td><s:textarea name="description" cssClass="TextareaStyle" value="%{#request.forum.description}"></s:textarea></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 表单操作 -->
        <div id="InputDetailBar">
            <input src="${pageContext.request.contextPath }/style/images/save.png" type="image">
            <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath }/style/images/goBack.png"></a>
        </div>
    </s:form>
</div>

<div class="Description">
    说明:<br>
    1,新添加的版块默认显示在最下面。<br>
</div>

</body></html>

SSH实战OA 11:BBS模块的更多相关文章

  1. Asp&period;Net Core 2&period;0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  2. Python标准库笔记&lpar;11&rpar; — Operator模块

    Operator--标准功能性操作符接口. 代码中使用迭代器时,有时必须要为一个简单表达式创建函数.有些情况这些函数可以用一个lambda函数实现,但是对于某些操作,根本没必要去写一个新的函数.因此o ...

  3. jQuery2&period;0应用开发&colon;SSH框架整合jQuery2&period;0实战OA办公自己主动化&lpar;VSS、operamasks-UI框架&rpar;

    我的qq是2059055336,对这个课程有兴趣的能够加我qq联系. 一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景 jQuery 2.0 正式版公布.不在支持 IE 6/7/8  ...

  4. SSH实战 &&num;183&semi; 唯唯乐购项目(上)

    前台需求分析 一:用户模块 注册 前台JS校验 使用AJAX完成对用户名(邮箱)的异步校验 后台Struts2校验 验证码 发送激活邮件 将用户信息存入到数据库 激活 点击激活邮件中的链接完成激活 根 ...

  5. angularJs项目实战!01:模块划分和目录组织

    近日来我有幸主导了一个典型的web app开发.该项目从产品层次来说是个典型的CRUD应用,故而我毫不犹豫地采用了grunt + boilerplate + angularjs + bootstrap ...

  6. python实战第一天-paramiko模块并练习

    操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装paramiko ...

  7. 每天记录一点:NetCore获得配置文件 appsettings&period;json vue-router页面传值及接收值 详解webpack &plus; vue &plus; node 打造单页面&lpar;入门篇&rpar; 30分钟手把手教你学webpack实战 vue&period;js&plus;webpack模块管理及组件开发

    每天记录一点:NetCore获得配置文件 appsettings.json   用NetCore做项目如果用EF  ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如S ...

  8. SSH实战 &&num;183&semi; 唯唯乐购项目(下)

    后台模块 一:后台用户模块 引入后台管理页面 创建adminuser表: CREATE TABLE `adminuser` (   `uid` int(11) NOT NULL AUTO_INCREM ...

  9. SSH实战 &&num;183&semi; 唯唯乐购项目(中)

    用户模块 三:一级分类的查询 创建一级分类表并导入基本数据 CREATE TABLE `category` (   `cid` int(11) NOT NULL AUTO_INCREMENT,   ` ...

随机推荐

  1. Javascript的this用法及jQuery中&dollar;this和&dollar;&lpar;this&rpar;的区别

    this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 1.this就是全 ...

  2. Python之路Day13--堡垒机

    一.前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒机所具备的功能属性中的 ...

  3. SQL Server数据库性能优化(二)之 索引优化

    参考文献 http://isky000.com/database/mysql-performance-tuning-index 原文作者是做mysql 优化的     但是我觉得  在索引方面    ...

  4. UVALive 7077 - Song Jiang&&num;39&semi;s rank list(模拟)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 使用Razor来进行页面布局

    UI设计师们现在也讲究页面设计的语义化和结构化,把一个页面分成很多个模块,使用语义化的类名或id来标识这些模块.Razor推出了新的布局解决方案来迎合这一潮流. 这里涉及到Razor的一些语法,大家可 ...

  6. C&num;四舍五入保留两位小数

  7. git 入门教程之初识git

    初识 git git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 背景 我们都知道,Linus 在1991年创建了开源的linux系统,随着不断发展壮大,目前已发展成为最大 ...

  8. nodejs中&amp&semi;&num;x5B89&semi;&amp&semi;&num;x5353&semi;&amp&semi;&num;x7AEF&semi;的编码如何转换为中文

    借助一些模块来转换,比如,html-entities Github var Entities = require('html-entities').XmlEntities; entities = ne ...

  9. SVG Path路径使用(一)

    一.<path> 标签 <path> 标签用来定义路径. 下面的命令可用于路径数据: M = moveto L = lineto H = horizontal lineto V ...

  10. sessionStorage和localStorage的使用

    不管是sessionStorage,还是localStorage,可使用的API都相同,常用的有如下几个(以localStorage为例): 保存数据:localStorage.setItem(key ...