⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topicaction.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.elan.forum.actions.topic;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.elan.forum.elf.El;
import com.elan.forum.forms.ReplyTopicForm;
import com.elan.forum.forms.topic.TopicForm;
import com.elan.forum.model.ForumTopic;
import com.elan.forum.model.ForumUser;
import com.elan.forum.model.Forummodule;
import com.elan.forum.model.Forumpiece;
import com.elan.forum.model.Forumtopicreply;
import com.elan.forum.util.Constents;

/**
 * @author Elan
 * 
 */
public class TopicAction extends TopicBaseAction {

	@Override
	protected ActionForward newTopic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Integer authorId = null;
		String title = null;
		String author = null;
		String text = null;
		String topicType = null;
		Integer pieceId = -1;
		String forwardUrl = request.getContextPath() + "/forum/Test.jsp";
		if (form != null) {
			authorId = ((TopicForm) form).getAuthorId();
			title = ((TopicForm) form).getTitle();
			author = ((TopicForm) form).getAuthor();
			text = ((TopicForm) form).getText();
			topicType = ((TopicForm) form).getTopicType();
			pieceId = ((TopicForm) form).getPieceId();
		}

		// 待写代码,重新初始化构造函数
		// 是一个荣誉的字段,,,需要删除
		Forumpiece fp = El.getEl().getPieceMgr().getForumPieceById(pieceId);
		ForumUser fu = El.getEl().getForumUserMgr().findUserById(authorId);
		if (fu == null) {
			System.out.println("fu is null");
		}
		// 实例化一个topic对象,等待持久化
		ForumTopic topic = new ForumTopic(1, fp, fu, title, author, text,
				"topicType", "0.gif", 0, 0, new Timestamp(System
						.currentTimeMillis()), new Timestamp(System
						.currentTimeMillis()), Byte.valueOf("0"), Byte
						.valueOf("0"), Byte.valueOf("0"), Byte.valueOf("0"),
				"moduleName", "pieceName", Byte.valueOf("0"),
				Byte.valueOf("0"), Byte.valueOf("0"));
		// //
		if (1 != El.getEl().getTopicMgr().postTopic(topic)) {
			ActionErrors errors = new ActionErrors();
			errors.add("posttopicerr", new ActionMessage("post.topic.error"));
			saveErrors(request, errors);
			return (new ActionForward("ForumError"));
		}
		forwardUrl = request.getContextPath()
				+ "/forum/topic.do?action=showTopic&topicId=" + topic.getId();
		ActionMessages msgs = new ActionMessages();
		msgs.add("messages", new ActionMessage("post.topic.success"));
		request.setAttribute("messages", msgs);
		request.setAttribute("forwardUrl", forwardUrl);
		// 构造路径
		String position = constructPosition(request, pieceId, (topic)
				.getPiece().getName(), topic.getId(), topic.getTitle());
		request.setAttribute("position", position);
		System.out.println(position);

		com.elan.forum.util.Constents.addTopicNum();// 贴数加

		// 修改piece中的isNewTopic字段为1,hibernate练级操作
		topic.getPiece().setIsNewTopic(true);

		return mapping.findForward("postTopicSuccess");
	}

	@Override
	protected ActionForward newForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Integer pieceId = getPieceId(request);
		Forumpiece fp = El.getEl().getPieceMgr().getForumPieceById(pieceId);
		// 构造位置
		String position = constructPosition(request, fp.getId(), fp.getName(),
				null, null);
		request.setAttribute("position", position);
		System.out.println("已经进入到这里了");
		return mapping.findForward("newForm");
	}

	/*
	 * 1)分页 2)是否有新的回复
	 * 
	 * (non-Javadoc)
	 * 
	 * @see com.elan.forum.actions.topic.TopicBaseAction#showPiece(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ActionForward showPiece(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// System.out.println("in showPiece >>");
		// mapping.setValidate(false);

		Integer pieceId = this.getPieceId(request);
		// 获取精华帖子
		List<ForumTopic> listEssenceTopic = El.getEl().getTopicMgr()
				.getEssenceTopic(pieceId);
		// 获取回复size(reply) == size(EssenceTopic)
		List<String> listEssenceTopicReply = new ArrayList<String>();
		int intEssenceTopicReply = 0;
		for (intEssenceTopicReply = 0; listEssenceTopic != null
				&& intEssenceTopicReply < listEssenceTopic.size(); intEssenceTopicReply++) {
			Forumtopicreply ftr = El.getEl().getTopicMgr()
					.getLastTopicReplyById(
							((ForumTopic) listEssenceTopic
									.get(intEssenceTopicReply)).getId());
			if (ftr != null) {
				listEssenceTopicReply.add(ftr.getReplyTime() + "<br>by:"
						+ ftr.getAuthor());
			} else {
				listEssenceTopicReply
						.add("<font color='red'>无</font> <br>by <font color='red'>无</font>");
			}
		}
		if (intEssenceTopicReply != 0) {
			this.setAttribute(request, "listEssenceTopicReply",
					listEssenceTopicReply);
			this.setAttribute(request, "listEssenceTopic", listEssenceTopic);
		}

		// 获取固定的帖子 有可能返回的是空的
		List<ForumTopic> listTopTopic = El.getEl().getTopicMgr().getTopTopic(
				pieceId);
		// 获取最后回复,它的size与listTopTopic的size是一样的大小的
		List<String> lastTopTopicReply = new ArrayList<String>();
		int tempI = 0;
		for (tempI = 0; listTopTopic != null && tempI < listTopTopic.size(); tempI++) {
			Forumtopicreply ftr = El.getEl().getTopicMgr()
					.getLastTopicReplyById(
							((ForumTopic) listTopTopic.get(tempI)).getId());
			if (ftr != null) {
				lastTopTopicReply.add(ftr.getReplyTime() + "<br>by:"
						+ ftr.getAuthor());
			} else {
				lastTopTopicReply
						.add("<font color='red'>无</font> <br>by <font color='red'>无</font>");
			}
		}
		if (tempI != 0) {
			this.setAttribute(request, "lastTopTopicReply", lastTopTopicReply);
			this.setAttribute(request, "listTopTopic", listTopTopic);
		}

		Map map = new HashMap();

		// 总的TOPIC数
		Long topicCount = new Long(0);

		// 总页数
		Long pageCount = new Long(0);
		// 页数大小
		Integer pageSize = Constents.piecePageSize;
		// 当前页码
		Integer page = this.getPage(request);
		// 是否有新的回复
		Boolean[] isNewReplyArray = null;

		// 获取MAP
		map = El.getEl().getTopicMgr().getForumPieceTopic(page, pageSize,
				pieceId);
		// 获取pageCount 总的页数
		pageCount = (Long) map.get("pageCount");
		// MAP中的topicCount
		topicCount = (Long) map.get("topicCount");
		// ..page
		page = (Integer) map.get("page");
		// .....
		List<String> lastReply = new ArrayList<String>();
		// 获取是对应于list中否有新的回复
		isNewReplyArray = (Boolean[]) map.get("isNewReplyArray");
		// 获取全部
		// List<ForumTopic> list = El.getEl().getTopicMgr().findPiece(pieceId);

		// 获取部分(分页)
		List<ForumTopic> list = (List<ForumTopic>) map.get("list");
		if (list != null && list.size() > 0) {
			String position = constructPosition(request, pieceId,
					((ForumTopic) list.get(0)).getPiece().getName(), null, null);
			setAttribute(request, "position", position);
			setAttribute(request, "piece", list);
			setAttribute(request, "topicCount", topicCount);
			setAttribute(request, "pageSize", pageSize);
			setAttribute(request, "pageCount", pageCount);
			setAttribute(request, "page", page);
			setAttribute(request, "pieceId", pieceId);
			setAttribute(request, "isNewReplyArray", isNewReplyArray);
			// 获取最后回复
			for (int i = 0; i < list.size(); i++) {
				Forumtopicreply ftr = El.getEl().getTopicMgr()
						.getLastTopicReplyById(
								((ForumTopic) list.get(i)).getId());
				if (ftr != null) {
					lastReply.add(ftr.getReplyTime() + "<br>by:"
							+ ftr.getAuthor());
				} else {
					lastReply
							.add("<font color='red'>无</font> <br>by <font color='red'>无</font>");
				}
			}
			this.setAttribute(request, "lastReply", lastReply);

		} else {
			// 找不到任何文章的piece
			ActionMessages messages = new ActionMessages();
			messages.add("", new ActionMessage("piece.no.topic"));
			saveMessages(request, messages);
		}

		return mapping.findForward("showPiece");
	}

	/*
	 * 
	 * 获取帖子 (non-Javadoc)
	 * 
	 * @see com.elan.forum.actions.topic.TopicBaseAction#showTopic(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ActionForward showTopic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();

		// topicId
		// 总的TOPIC回复数
		Long topicCount = new Long(0);

		// 总页数
		Long pageCount = new Long(0);
		// 获取设置回复页数的大小
		Integer pageSize = Constents.topicReplyPageSize;
		// 当前页码
		Integer page = this.getPage(request);
		// 是否有新的回复
		// ////////////////

		Integer topicId = getTopicId(request);
		System.out.println("TOpicId:..........................:" + topicId);
		//System.exit(0);
		ForumTopic fa = El.getEl().getTopicMgr().findPieceTopic(topicId);
		// 找不到该topic文章
		if (fa == null) {
			ActionErrors errors = new ActionErrors();
			errors.add("", new ActionError("topic.no.find"));
			saveErrors(request, errors);
			return mapping.findForward("topicNotFind");
		}
		// +TOPIC点击
		fa.setClick(fa.getClick() + 1);
		El.getEl().getTopicMgr().saveTopic(fa);

		// 获取有发TOPIC相互对应的作者
		ForumUser topicAuthor = fa.getForumUser();

		// 构造位置
		String position = constructPosition(request, fa.getPiece().getId(), fa
				.getPiece().getName(), fa.getId(), fa.getTitle());
		request.setAttribute("position", position);

		// 获取map数据结构
		map = El.getEl().getTopicMgr().getForumTopicReply(page, pageSize,
				topicId);
		// ...pageCount
		pageCount = (Long) map.get("pageCount");
		// ..topicCount
		topicCount = (Long) map.get("topicCount");
		// ..page ..重新复制,以防转过的PAGE大与实际数据中的page
		page = (Integer) map.get("page");
		// 获取回复
		List<Forumtopicreply> replyList = (List<Forumtopicreply>) map
				.get("replyList");
		// List<Forumtopicreply> falist =
		// El.getEl().getTopicMgr().findTopicReply(
		// topicId);
		// 获取回复的作者
		List<ForumUser> replyUserList = new ArrayList<ForumUser>();
		for (int i = 0; i < replyList.size(); i++) {
			Forumtopicreply ftr = replyList.get(i);
			replyUserList.add(ftr.getForumUser());
		}

		this.setAttribute(request, "topicAuthor", topicAuthor);
		this.setAttribute(request, "replyList", replyList);
		this.setAttribute(request, "replyUserList", replyUserList);
		this.setAttribute(request, "topic", fa);
		this.setAttribute(request, "pieceId", fa.getPiece().getId());
		this.setAttribute(request, "topicId", topicId);

		this.setAttribute(request, "pageSize", pageSize);
		this.setAttribute(request, "topicCount", topicCount);
		this.setAttribute(request, "pageCount", pageCount);
		this.setAttribute(request, "page", page);
		
		return mapping.findForward("showTopic");
	}

	/*
	 * 跳到回复表单页面 (non-Javadoc)
	 * 
	 * @see com.elan.forum.actions.topic.TopicBaseAction#replyForm(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ActionForward replyForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//Integer pieceId = Integer.valueOf(request.getParameter("pieceId"));
		Integer topicId = Integer.valueOf(request.getParameter("topicId"));
		ForumTopic ft = El.getEl().getTopicMgr().findPieceTopic(topicId);

		// 构造位置
		String position = constructPosition(request, ft.getPiece().getId(), ft
				.getPiece().getName(), ft.getId(), ft.getTitle());
		request.setAttribute("position", position);
		return mapping.findForward("replyForm");
	}

	/*
	 * Action:回复帖子 (non-Javadoc)
	 * 
	 * @see com.elan.forum.actions.topic.TopicBaseAction#replyTopic(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ActionForward replyTopic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		byte locked = 0;
		Integer pieceId = 1;
		Integer topicId = 1;
		Integer authorId = this.getAuthorId(request);
		String text = null;
		String author = null;

		ForumUser fu = El.getEl().getForumUserMgr().findUserById(authorId);
		Forumtopicreply forumtopicreply = new Forumtopicreply();
		if (form != null) {
			topicId = ((TopicForm) form).getTopicId();
			text = ((TopicForm) form).getText();
			author = ((TopicForm) form).getAuthor();
			pieceId = ((TopicForm) form).getPieceId();
		}
		forumtopicreply.setTopicId(topicId);
		forumtopicreply.setForumUser(fu);
		forumtopicreply.setText(text);
		forumtopicreply.setAuthor(author);
		forumtopicreply.setIcon("3.gif");
		forumtopicreply.setReplyTime(new Timestamp(System.currentTimeMillis()));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -