论坛:

论坛是一个非常典型的示例,使用到了知启蒙登录组件,

论坛功能描述

1、版块的增加修改和删除:由超级管理员设置的具有管理权限的成员可以进行操作。

2、发表帖子:用户注册登录之后,可以在相应的版块下发表帖子。

3、编辑/删除帖子:用户注册登录之后,对自己发表的帖子可以进行编辑和删除。

4、回帖:用户注册登录之后,可以对感兴趣的帖子进行回复。

5、管理回帖:用户注册登录之后,可以对自己发表的帖子下的留言进行删除。

BBS论坛源码工程结构

源码资源目录

  • src/dbo用于存放数据对象源码,由知启蒙论坛数据库字典.dbo设计并生成。
  • src/mian用于存放编写评论组件的功能源码,如action等。

引入外部库资源目录

  • JRE System Library约定的[JavaSE-1.7]环境。
  • Referenced Libraries引用库目录[lib]下所有包。
  • 库目录[lib]存放引用库文件。并通过[Build Path]加载到Referenced Libraries。

工程配置相关目录

  • 配置目录[conf]存放工程配置文件,[zhiqim.xml]是必须的。在zhiqim.xml的[config]选项中可以增加多个配置文件,如本示例httpd.xml和logging.xml。
  • 文档目录[document]存放工程文档,如数据库字典、工程导出配置文件[export/project.exp.xml]。
  • 日志目录[logs]存放调试、运行时生成的日志。
  • 资源目录[resource]存放WEB容器ZML上下文环境的配置文件、ZML页面文件等。包括上下文环境配置[/conf/config.xml][/conf/config.zml]等。
  • 发布目录[release]存放导出的执行程序和源码。
  • zhiqim_bbs
  • src/dbo
  • org.zhiqim.bbs.dbo
  • BbsBoard.java
  • BbsThread.java
  • BbsTopic.java
  • org.zhiqim.bbs.ex
  • BbsBoardEx.java
  • BbsThreadEx.java
  • BbsTopicEx.java
  • src/manage
  • org.zhiqim.bbs.manage.action
  • BoardAction.java
  • src/portal
  • org.zhiqim.bbs.portal.action
  • BbsAction.java
  • BbsModel.java
  • EditAction.java
  • PublishAction.java
  • ThreadAction.java
  • TopicAction.java
  • JRE System Library [JavaSE-1.7]
  • Referenced Libraries
  • zhiqim.jar
  • zhiqim_orm.jar
  • zhiqim_zml.jar
  • zhiqim_httpd.jar
  • zhiqim_ui.jar
  • zhiqim_mysql5.0.8.jar
  • zhiqim_account.jar
  • zhiqim_mail.jar
  • zhiqim_sms.jar
  • conf
  • zhiqim.xml
  • httpd.xml
  • logging.xml
  • document
  • 知启蒙论坛数据库字典.dbo
  • lib
  • libex
  • logs
  • portal
  • conf
  • config.zml
  • config.xml
  • zinc
  • zview
  • manage
  • board.zml
  • boardAdd.zml
  • boardModify.zml
  • bbs.zml
  • edit.zml
  • publish.zml
  • thread.zml
  • topic.zml
  • release
  • src
BBS论坛配置说明
一、/conf/zhiqim.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ZHIQIM-CONFIG PUBLIC "-//ZHIQIM //DTD Zhiqim-Config Configuration 1.5.0//EN" "http://zhiqim.org/xmldtds/zhiqim_config_1_5_0.dtd"> <zhiqim-config> <group id="boot" desc="系统启动配置"> <item key="name" value="知启蒙论坛" type="protected" desc="系统名称" /> <item key="version" value="V1.5.0" type="protected" desc="系统版本" /> <item key="port" value="50080" type="protected" desc="系统监听端口,用于启动监听和命令关闭工程" /> <item key="process" value="zhiqim_bbs" type="protected" desc="使用的进程名,MAC系统下该配置无效" /> <item key="args" value="-server -Xms64m -Xmx256m" type="protected" desc="JVM参数" /> <item key="libex" value="./libex" type="protected" desc="除./lib目录外额外需要加载的库目录,多个逗号隔开" /> <item key="include" value="*/lib/*,*/bin" type="protected" desc="扫描包含库模式,多个用逗号隔开,星号表示通配符" /> </group> <group id="config" desc="系统其他配置文件,./开头表示工程相对路径下,绝对路径使用/或d:/等(Windows也使用顺斜杠作路径)"> <item key="logging" value="./conf/logging.xml" type="protected" desc="日志配置文件" /> <item key="httpd" value="./conf/httpd.xml" type="protected" desc="WEB配置" /> </group> <group id="service" desc="系统服务配置,有依赖时注意顺序,系统按顺序加载,按倒序销毁"> <item key="logging" value="org.zhiqim.kernel.logging.LogServer" type="protected" desc="日志服务" /> <item key="cutter" value="org.zhiqim.kernel.logging.LogLinuxNohupCutter" type="protected" desc="日志Linux切割服务" /> <item key="orm" value="org.zhiqim.orm.ORMServer" type="protected" desc="主数据库服务" /> <item key="resource" value="org.zhiqim.httpd.HttpServer" type="protected" desc="web服务" /> <item key="mail" value="org.zhiqim.mail.MailSender" type="protected" desc="邮件发送服务"/> <item key="sms" value="org.zhiqim.sms.SMSSender" type="protected" desc="短信发送服务"/> </group> <group id="orm" desc="MYSQL数据库连接方式"> <item key="dbType" value="mysql" type="protected" desc="数据库类型,支持oracle|mysql|mssql|hsql|access" /> <item key="driver" value="org.zhiqim.mysql5_0_8.jdbc.Driver" type="protected" desc="数据库驱动" /> <item key="url" value="jdbc:mysql://127.0.0.1:3306/zhiqim_bbs?useUnicode=true&characterEncoding=UTF-8" type="protected" desc="数据库Url" /> <item key="user" value="root" type="protected" desc="数据库用户名" /> <item key="pass" value="root" type="private" desc="数据库密码" /> <item key="minPoolSize" value="2" type="protected" desc="线程池最小值" /> <item key="maxPoolSize" value="10" type="protected" desc="线程池最大值" /> <item key="maxKeepTime" value="7001" type="protected" desc="每连接最大保持时长,大于该值将重建连接,单位秒" /> </group> <group id="mail" desc="邮件服务器配置"> <item key="smtp.host" value="smtp.exmail.qq.com" type="protected" desc="SMTP服务器地址" /> <item key="smtp.port" value="25" type="protected" desc="SMTP服务器端口" /> <item key="smtp.auth" value="true" type="protected" desc="SMTP服务器是否需要身份验证" /> <item key="smtp.auth.name" value="account@zhiqim.com" type="protected" desc="SMTP服务器验证用户名" /> <item key="smtp.auth.pass" value="Redxyz2013" type="protected" desc="SMTP服务器验证密码" /> <item key="from.address" value="account@zhiqim.com" type="protected" desc="SMPT邮件发送地址" /> <item key="from.name" value="知启蒙账号中心" type="protected" desc="SMPT邮件发送者名称" /> </group> <group id="sms" desc="短信发送配置"> <item key="apiUrl" value="http://sms.redxyz.com/smsSend.htm" type="protected" desc="短信发送链接" /> <item key="appId" value="1702200939340082" type="protected" desc="商家编码" /> <item key="appSecret" value="N22L15535LNNCY9D" type="protected" desc="商家密钥" /> <item key="templateCode" value="SMS_47935217" type="protected" desc="模板编码" /> <item key="signName" value="知启蒙" type="protected" desc="短信签名" /> </group> <group id="word" desc="词库"> <item key="sensitiveWordPath" value="./dic/sensitiveWords.txt" type="protected" desc="敏感词库路径" /> </group> </zhiqim-config>
二、/conf/httpd.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ZHIQIM-CONFIG PUBLIC "-//ZHIQIM //DTD Zhiqim-Config Configuration 1.5.0//EN" "http://zhiqim.org/xmldtds/zhiqim_config_1_5_0.dtd"> <zhiqim-config> <group id="resource" desc="HTTP服务"> <item key="context" value="resource.root" type="protected" desc="HTTP服务下包括的上下文环境,多个用逗号隔开" /> <item key="scheme" value="HTTP" type="protected" desc="HTTP或HTTPS协议" /> <item key="port" value="80" type="protected" desc="监听端口" /> <item key="minPoolSize" value="5" type="protected" desc="HTTP处理线程池最小值" /> <item key="maxPoolSize" value="10" type="protected" desc="HTTP处理线程池最大值" /> </group> <group id="resource.root" desc="HTTP根上下文环境"> <item key="domain" value="" type="protected" desc="域名" /> <item key="path" value="/" type="protected" desc="上下文环境路径,必填" /> <item key="resourceType" value="directory" type="protected" desc="资源目录类型classpath|directory,无资源时可不配置" /> <item key="resourcePath" value="./portal" type="protected" desc="资源目录,无资源时可不配置" /> <item key="welcomeUrl" value="/bbs.htm" type="protected" desc="欢迎页,无资源时可不配置,/开头" /> <item key="upload.rootDir" value="./upload" type="protected" desc="上传组件根目录" /> </group> </zhiqim-config>
三、/conf/logging.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ZHIQIM-CONFIG PUBLIC "-//ZHIQIM //DTD Zhiqim-Config Configuration 1.5.0//EN" "http://zhiqim.org/xmldtds/zhiqim_config_1_5_0.dtd"> <zhiqim-config> <group id="logging" desc="信息日志配置"> <item key="logging.info" value="debug-info" type="protected" desc="信息日志配置" /> <item key="logging.error" value="error-fatal" type="protected" desc="错误日志配置" /> </group> <group id="logging.info" desc="信息日志配置"> <item key="console.out" value="true" type="protected" desc="是否输出到控制台" /> <item key="file.out" value="true" type="protected" desc="是否输出到文件" /> <item key="file.path" value="./logs/info.log" type="protected" desc="输出到文件路径" /> </group> <group id="logging.error" desc="错误日志配置"> <item key="console.out" value="true" type="protected" desc="是否输出到控制台" /> <item key="file.out" value="true" type="protected" desc="是否输出到文件" /> <item key="file.path" value="./logs/error.log" type="protected" desc="输出到文件路径" /> </group> <group id="logging.database" desc="数据库日志配置"> <item key="must.name" value="database.log" type="protected" desc="指定日志名称,为空表示不指定" /> <item key="console.out" value="true" type="protected" desc="是否输出到控制台" /> <item key="file.out" value="true" type="protected" desc="是否输出到文件" /> <item key="file.path" value="./logs/database/database.log" type="protected" desc="输出到文件路径" /> </group> </zhiqim-config>
四、/portal/conf/config.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ZHIQIM-CONTEXT PUBLIC "-//ZHIQIM //DTD Zhiqim-Context Configuration 1.5.0//EN" "http://zhiqim.org/xmldtds/zhiqim_context_1_5_0.dtd"> <zhiqim-context> <!-- 组件配置 --> <component name="账号组件" path="/org/zhiqim/account/resource"/> <!-- 访客部分 --> <action name="首页" path="/bbs.htm" view="/zview/bbs.zml" class="org.zhiqim.bbs.portal.action.BbsAction"/> <action name="主题" path="/topic.htm" view="/zview/topic.zml" class="org.zhiqim.bbs.portal.action.TopicAction"/> <action name="帖子" path="/thread.htm" view="/zview/thread.zml" redirect="/index.htm" class="org.zhiqim.bbs.portal.action.ThreadAction"/> <action name="发帖" interceptor="chkZacLogin" path="/publish.htm" view="/zview/publish.zml" class="org.zhiqim.bbs.portal.action.PublishAction"/> <action name="编辑帖子" interceptor="chkZacLogin" path="/edit.htm" view="/zview/edit.zml" class="org.zhiqim.bbs.portal.action.EditAction"/> <!-- 板块管理 --> <action name="板块列表" interceptor="chkZacLogin" path="/board.htm" view="/zview/manage/board.zml" class="org.zhiqim.bbs.manage.action.BoardAction" method="list"/> <action name="增加板块页面" interceptor="chkZacLogin" path="/boardAdd.htm" view="/zview/manage/boardAdd.zml" class="org.zhiqim.bbs.manage.action.BoardAction" method="add"/> <action name="增加板块处理" interceptor="chkZacLogin" path="/boardInsert.htm" redirect="/board.htm" success="增加成功" class="org.zhiqim.bbs.manage.action.BoardAction" method="insert"/> <action name="修改板块页面" interceptor="chkZacLogin" path="/boardModify.htm" view="/zview/manage/boardModify.zml" class="org.zhiqim.bbs.manage.action.BoardAction" method="modify"/> <action name="修改板块处理" interceptor="chkZacLogin" path="/boardUpdate.htm" redirect="/board.htm" success="修改成功" class="org.zhiqim.bbs.manage.action.BoardAction" method="update"/> <action name="删除板块处理" interceptor="chkZacLogin" path="/boardDelete.htm" redirect="/board.htm" success="删除成功" class="org.zhiqim.bbs.manage.action.BoardAction" method="delete"/> </zhiqim-context>
BBS论坛类的编写
一、BoardAction.java
package org.zhiqim.bbs.manage.action; import org.zhiqim.account.dbo.user.UserAccount; import org.zhiqim.bbs.dbo.BbsBoard; import org.zhiqim.bbs.ex.BbsBoardEx; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.extend.StdSwitchAction; import org.zhiqim.httpd.validate.ones.IsLen; import org.zhiqim.httpd.validate.ones.IsNotEmpty; import org.zhiqim.kernel.Global; import org.zhiqim.kernel.paging.PageResult; import org.zhiqim.kernel.util.Ids; import org.zhiqim.orm.ZTable; import org.zhiqim.orm.ZView; import org.zhiqim.orm.dbo.Selector; import org.zhiqim.orm.dbo.Updater; /** * 版块管理 * * @version v1.0.0 @author zhuzhiyuan 2018-10-22 新建与整理 */ public class BoardAction extends StdSwitchAction { @Override protected void validateId(HttpRequest request) { request.addValidate(new IsNotEmpty("boardId", "请选择一个选项")); } @Override protected void validateForm(HttpRequest request) { request.addValidate(new IsLen("boardName", "板块名称不能为空且不能超过32个字符", 1, 32)); request.addValidate(new IsNotEmpty("boardManager", "请填写正确的注册邮箱或手机号")); } protected void item(HttpRequest request) throws Exception { long boardId = request.getParameterLong("boardId"); BbsBoard board = Global.get(ZTable.class).item(BbsBoard.class, boardId); if (board == null) { request.returnHistory("该板块不存在,请重新选择"); return; } request.setAttribute("board", board); } @Override protected void list(HttpRequest request) throws Exception { int page = request.getParameterInt("page", 1); int pageSize = request.getContextAttributeInt("fmr_page_size", 10); PageResult pageResult = Global.get(ZView.class).page(BbsBoardEx.class, page, pageSize); pageResult.addConditionMap(request.getParameterMap()); request.setAttribute("pageResult", pageResult); } @Override protected void add(HttpRequest request) throws Exception { } @Override protected void modify(HttpRequest request) throws Exception { long boardId = request.getParameterLong("boardId"); Selector selecter = new Selector(); selecter.addMust("boardId", boardId); BbsBoardEx board = Global.get(ZView.class).item(BbsBoardEx.class, selecter); if (board == null) { request.returnHistory("该板块不存在,请重新选择"); return; } request.setAttribute("board", board); } @Override protected void insert(HttpRequest request) throws Exception { String boardName = request.getParameter("boardName"); String userName = request.getParameter("boardManager"); String boardDesc = request.getParameter("boardDesc"); UserAccount account = Global.get(ZTable.class).item(UserAccount.class, userName); if (account == null) { request.returnHistory("版主必须是注册的用户名、邮箱或手机号"); return; } BbsBoard board = new BbsBoard(); board.setBoardId(Ids.longId()); board.setBoardName(boardName); board.setBoardManager(account.getUserId()); board.setBoardDesc(boardDesc); Global.get(ZTable.class).insert(board); } @Override protected void update(HttpRequest request) throws Exception { long boardId = request.getParameterLong("boardId"); String boardName = request.getParameter("boardName"); String userName = request.getParameter("boardManager"); String boardDesc = request.getParameter("boardDesc"); UserAccount account = Global.get(ZTable.class).item(UserAccount.class, userName); if (account == null) { request.returnHistory("版主必须是注册的用户名、邮箱或手机号"); return; } Updater updater = new Updater(); updater.addMust("boardId", boardId); updater.addField("boardName", boardName); updater.addField("boardManager", account.getUserId()); updater.addField("boardDesc", boardDesc); Global.get(ZTable.class).update(BbsBoard.class, updater); } @Override protected void delete(HttpRequest request) throws Exception { long boardId = request.getParameterLong("boardId"); Global.get(ZTable.class).delete(BbsBoard.class, boardId); } }
二、BbsAction.java
package org.zhiqim.bbs.portal.action; import java.util.List; import org.zhiqim.bbs.ex.BbsBoardEx; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.core.Action; import org.zhiqim.kernel.Global; import org.zhiqim.orm.ZView; import org.zhiqim.orm.dbo.Selector; public class BbsAction implements Action { @Override public void execute(HttpRequest request) throws Exception { Selector selector = new Selector(); selector.addOrderbyAsc("boardId"); List bbsBoards = Global.get(ZView.class).list(BbsBoardEx.class, selector); request.setAttribute("bbsBoards", bbsBoards); } }
三、BBsModel.java
package org.zhiqim.bbs.portal.action; import org.zhiqim.account.ZacSessionUser; import org.zhiqim.bbs.dbo.BbsThread; import org.zhiqim.bbs.dbo.BbsTopic; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.util.Sessions; import org.zhiqim.httpd.validate.ones.IsIntegerValue; import org.zhiqim.httpd.validate.ones.IsLen; import org.zhiqim.httpd.validate.ones.IsNotEmpty; import org.zhiqim.kernel.Global; import org.zhiqim.kernel.annotation.AnAlias; import org.zhiqim.kernel.util.DateTimes; import org.zhiqim.orm.ZTable; import org.zhiqim.orm.dbo.Selector; import org.zhiqim.orm.dbo.Updater; @AnAlias("BBSModel") public class BBSModel { /** 主题置顶 */ public static void doTopicTop(HttpRequest request) throws Exception { //检查参数 request.addValidate(new IsNotEmpty("topicId", "请选择一个主题")); if (!request.chkValidate()) { request.setResponseError(request.getAlertMsg()); return; } long topicId = request.getParameterLong("topicId"); //更新BBS_TOPIC表 Updater updater = new Updater(); updater.addMust("topicId", topicId); updater.addField("topicTop", true); Global.get(ZTable.class).update(BbsTopic.class, updater); } /** 主题置顶取消 */ public static void doTopicTopCancel(HttpRequest request) throws Exception { //检查参数 request.addValidate(new IsNotEmpty("topicId", "请选择一个主题")); if (!request.chkValidate()) { request.setResponseError(request.getAlertMsg()); return; } long topicId = request.getParameterLong("topicId"); //更新BBS_TOPIC表 Updater updater = new Updater(); updater.addMust("topicId", topicId); updater.addField("topicTop", false); Global.get(ZTable.class).update(BbsTopic.class, updater); } /** 删除主题 */ public static void doDelTopic(HttpRequest request) throws Exception { //检查参数 request.addValidate(new IsNotEmpty("topicId", "请选择一个主题")); if (!request.chkValidate()) { request.setResponseError(request.getAlertMsg()); return; } Long topicId = request.getParameterLong("topicId"); Global.get(ZTable.class).delete(BbsTopic.class, topicId); Global.get(ZTable.class).delete(BbsThread.class, new Selector("topicId", topicId)); } /** 删除楼层 */ public static void doDelReply(HttpRequest request) throws Exception { //检查参数 request.addValidate(new IsNotEmpty("topicId", "请选择一个主题")); request.addValidate(new IsNotEmpty("threadNum", "请选择一个楼层")); if (!request.chkValidate()) { request.setResponseError(request.getAlertMsg()); return; } long topicId = request.getParameterLong("topicId"); int threadNum = request.getParameterInt("threadNum"); Selector selector = new Selector(); selector.addMust("topicId", topicId); selector.addMust("threadNum", threadNum); Global.get(ZTable.class).delete(BbsThread.class, selector); } /**回复帖子*/ public void doReply(HttpRequest request)throws Exception { ZacSessionUser sessionUser = (ZacSessionUser) request.getSessionUser(); if(sessionUser == null) { request.setResponseError("请先登录,再回复"); return; } request.addValidate(new IsNotEmpty("topicId", "回复的帖子不存在,请刷新页面")); request.addValidate(new IsLen("threadContent", "请输入至少5个字符的回复内容", 5, 2000)); request.addValidate(new IsIntegerValue("verificationCode", "请输入正确的验证码", 1000, 9999)); if (!request.chkValidate()) { request.setResponseError(request.getAlertMsg()); return; } long userId = sessionUser.getUserId(); String verificationCode = request.getParameter("verificationCode"); String threadContent = request.getParameterNoFilter("threadContent"); long topicId = request.getParameterLong("topicId"); String vcode = Sessions.getSessionVerificationCode(request); if (!verificationCode.equals(vcode)) { request.setResponseError("验证码不正确或已失效,请新输入!"); return; } String curTime = DateTimes.getDateTimeString(); BbsTopic bbsTopic = Global.get(ZTable.class).item(BbsTopic.class, topicId); int topicReplyNum = bbsTopic.getTopicReplyNum(); BbsThread bbsThread = new BbsThread(); bbsThread.setBoardId(bbsTopic.getBoardId()); bbsThread.setTopicId(topicId); bbsThread.setThreadNum(topicReplyNum + 1); bbsThread.setThreadCreated(curTime); bbsThread.setThreadUserId(userId); bbsThread.setThreadContent(threadContent); Updater updater = new Updater(); updater.addMust("topicId", topicId); updater.addField("topicReplyNum", topicReplyNum + 1); updater.addField("topicLastReplyTime", curTime); updater.addField("topicLastReplyAuthor", userId); Global.get(ZTable.class).insert(bbsThread); Global.get(ZTable.class).update(BbsTopic.class, updater); } }
四、EditAction
package org.zhiqim.bbs.portal.action; import org.zhiqim.bbs.dbo.BbsBoard; import org.zhiqim.bbs.dbo.BbsThread; import org.zhiqim.bbs.dbo.BbsTopic; import org.zhiqim.bbs.ex.BbsThreadEx; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.extend.GetPostAction; import org.zhiqim.httpd.util.Sessions; import org.zhiqim.httpd.validate.ones.IsLen; import org.zhiqim.httpd.validate.ones.IsNumericLen; import org.zhiqim.kernel.Global; import org.zhiqim.orm.ZTable; import org.zhiqim.orm.ZView; import org.zhiqim.orm.dbo.Selector; import org.zhiqim.orm.dbo.Updater; public class EditAction extends GetPostAction { @Override protected void validate(HttpRequest request) {// 帖子编辑页面和处理验证 request.addValidate(new IsLen("topicTitle", "主题不能为空且不超过100汉字", 1, 100)); request.addValidate(new IsLen("threadContent", "帖子内容不能为空且不超过4000汉字", 1, 4000)); request.addValidate(new IsNumericLen("verificationCode", "验证码必须是4位数字", 4, 4)); } @Override protected void doGet(HttpRequest request) throws Exception {// 帖子编辑页面 long topicId = request.getParameterLong("topicId"); Selector sel = new Selector("topicId", topicId); sel.addMust("threadNum", 0); BbsThreadEx bbsThread = Global.get(ZView.class).item(BbsThreadEx.class, sel); request.setAttribute("bbsThread", bbsThread); BbsBoard bbsBoard = Global.get(ZTable.class).item(BbsBoard.class, bbsThread.getBoardId()); request.setAttribute("bbsBoard", bbsBoard); } @Override protected void doPost(HttpRequest request) throws Exception { String verificationCode = request.getParameter("verificationCode"); String vcode = Sessions.getSessionVerificationCode(request); if (!verificationCode.equals(vcode)) { request.returnHistory("验证码不正确或已失效,请新输入!"); return; } long topicId = request.getParameterLong("topicId"); String topicTitle = request.getParameter("topicTitle"); String threadContent = request.getParameterNoFilter("threadContent"); BbsTopic topic = Global.get(ZTable.class).item(BbsTopic.class, topicId); if(topic == null) { request.returnHistory("编辑的帖子不存在,请重新选择"); return; } Updater updater = new Updater(); updater.addMust("topicId", topicId); updater.addField("topicTitle", topicTitle); Global.get(ZTable.class).update(BbsTopic.class, updater); Updater threadUpdater = new Updater(); threadUpdater.addMust("topicId", topicId); threadUpdater.addMust("threadNum", 0); threadUpdater.addField("threadContent", threadContent); Global.get(ZTable.class).update(BbsThread.class, threadUpdater); request.setRedirect("/topic.htm?boardId=" + topic.getBoardId()); } }
五、PublishAction
package org.zhiqim.bbs.portal.action; import java.util.List; import org.zhiqim.account.ZacSessionUser; import org.zhiqim.bbs.dbo.BbsBoard; import org.zhiqim.bbs.dbo.BbsThread; import org.zhiqim.bbs.dbo.BbsTopic; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.extend.GetPostAction; import org.zhiqim.httpd.util.Sessions; import org.zhiqim.httpd.validate.ones.IsLen; import org.zhiqim.httpd.validate.ones.IsNotEmpty; import org.zhiqim.httpd.validate.ones.IsNumericLen; import org.zhiqim.kernel.Global; import org.zhiqim.kernel.util.DateTimes; import org.zhiqim.kernel.util.Ids; import org.zhiqim.orm.ZTable; public class PublishAction extends GetPostAction { @Override protected void validate(HttpRequest request) {// 发帖页面和处理验证 request.addValidate(new IsNotEmpty("boardId", "版块编号不能为空")); request.addValidate(new IsLen("topicTitle", "主题不能为空且不超过100汉字", 1, 100)); request.addValidate(new IsLen("threadContent", "帖子内容不能为空且不超过4000汉字", 1, 4000)); request.addValidate(new IsNumericLen("verificationCode", "验证码必须是4位数字", 4, 4)); } @Override protected void doGet(HttpRequest request) throws Exception {// 发帖页面 List boardList = Global.get(ZTable.class).list(BbsBoard.class); request.setAttribute("boardList", boardList); } @Override protected void doPost(HttpRequest request) throws Exception {// 发帖处理 String verificationCode = request.getParameter("verificationCode"); String vcode = Sessions.getSessionVerificationCode(request); if (!verificationCode.equals(vcode)) { request.returnHistory("验证码不正确或已失效,请新输入!"); return; } long boardId = request.getParameterLong("boardId"); long userId = request.getSessionUser(ZacSessionUser.class).getUserId(); String topicTitle = request.getParameter("topicTitle"); String threadContent = request.getParameterNoFilter("threadContent"); long topicId = Ids.longId(); // 插入到BBS_TOPIC数据库 BbsTopic bbsTopic = new BbsTopic(); bbsTopic.setBoardId(boardId); bbsTopic.setTopicId(topicId); bbsTopic.setTopicTop(false); bbsTopic.setTopicCreated(DateTimes.getDateTimeString()); bbsTopic.setTopicTitle(topicTitle); bbsTopic.setTopicUserId(userId); bbsTopic.setTopicReplyNum(0); bbsTopic.setTopicReadNum(0); bbsTopic.setTopicLastReplyTime(DateTimes.getDateTimeString()); bbsTopic.setTopicLastReplyAuthor(userId); Global.get(ZTable.class).insert(bbsTopic); // 插入到BBS_THREAD数据库 BbsThread bbsThread = new BbsThread(); bbsThread.setBoardId(boardId); bbsThread.setTopicId(topicId); bbsThread.setThreadNum(0); bbsThread.setThreadCreated(DateTimes.getDateTimeString()); bbsThread.setThreadUserId(userId); bbsThread.setThreadContent(threadContent); Global.get(ZTable.class).insert(bbsThread); request.setRedirect("/topic.htm?boardId=" + boardId); } }
六、ThreadAction
package org.zhiqim.bbs.portal.action; import org.zhiqim.bbs.dbo.BbsTopic; import org.zhiqim.bbs.ex.BbsThreadEx; import org.zhiqim.bbs.ex.BbsTopicEx; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.core.Action; import org.zhiqim.kernel.Global; import org.zhiqim.kernel.paging.PageResult; import org.zhiqim.orm.ZTable; import org.zhiqim.orm.ZView; import org.zhiqim.orm.dbo.Selector; import org.zhiqim.orm.dbo.Updater; /** * 详情列表 * @author zhouwenbin * */ public class ThreadAction implements Action { @Override public void execute(HttpRequest request) throws Exception { int page = request.getParameterInt("page", 1); int pageSize = request.getContextAttributeInt("fmr_page_size", 10); long topicId = request.getParameterLong("topicId"); Selector selector = new Selector(); selector.addMust("topicId", topicId); selector.addOrderbyAsc("threadCreated"); PageResult result = Global.get(ZView.class).page(BbsThreadEx.class, page, pageSize, selector); result.addConditionMap(request.getParameterMap()); request.setAttribute("result", result); BbsTopicEx bbsTopic = Global.get(ZView.class).item(BbsTopicEx.class, new Selector("topicId", topicId)); request.setAttribute("bbsTopic", bbsTopic); //更新阅读数 Updater updater = new Updater(); updater.addMust("topicId", topicId); updater.addField("topicReadNum", bbsTopic.getTopicReadNum() + 1); Global.get(ZTable.class).update(BbsTopic.class, updater); } }
七、TopicAction
package org.zhiqim.bbs.portal.action; import java.util.ArrayList; import java.util.List; import org.zhiqim.bbs.dbo.BbsBoard; import org.zhiqim.bbs.ex.BbsTopicEx; import org.zhiqim.httpd.HttpRequest; import org.zhiqim.httpd.context.core.Action; import org.zhiqim.kernel.Global; import org.zhiqim.kernel.paging.PageBuilder; import org.zhiqim.kernel.paging.PageResult; import org.zhiqim.orm.ZTable; import org.zhiqim.orm.ZView; import org.zhiqim.orm.dbo.Selector; /** * 主题列表 * @author zhouwenbin * */ public class TopicAction implements Action { @Override public void execute(HttpRequest request) throws Exception { int page = request.getParameterInt("page", 1); int pageSize = request.getContextAttributeInt("fmr_page_size", 10); long boardId = request.getParameterLong("boardId"); BbsBoard bbsBoard = Global.get(ZTable.class).item(BbsBoard.class, boardId); request.setAttribute("bbsBoard", bbsBoard); //置顶列表 Selector selector1 = new Selector(); selector1.addMust("boardId", boardId); selector1.addMust("topicTop", true); selector1.addOrderbyDesc("topicCreated"); List bbsTopic1 = Global.get(ZView.class).list(BbsTopicEx.class, selector1); Selector selector2 = new Selector(); selector2.addMust("boardId", boardId); selector2.addMust("topicTop", false); selector2.addOrderbyDesc("topicCreated"); List bbsTopic2 = Global.get(ZView.class).list(BbsTopicEx.class, selector2); List tempList = new ArrayList<>(); tempList.addAll(bbsTopic1); tempList.addAll(bbsTopic2); PageResult result = PageBuilder.pageResult(page, pageSize, tempList); result.addConditionMap(request.getParameterMap()); request.setAttribute("result", result); } }