?? topicaction.java
字號:
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();
}
// 待寫代碼,重新初始化構(gòu)造函數(shù)
// 是一個(gè)榮譽(yù)的字段,,,需要刪除
Forumpiece fp = El.getEl().getPieceMgr().getForumPieceById(pieceId);
ForumUser fu = El.getEl().getForumUserMgr().findUserById(authorId);
if (fu == null) {
System.out.println("fu is null");
}
// 實(shí)例化一個(gè)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);
// 構(gòu)造路徑
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();// 貼數(shù)加
// 修改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);
// 構(gòu)造位置
String position = constructPosition(request, fp.getId(), fp.getName(),
null, null);
request.setAttribute("position", position);
System.out.println("已經(jīng)進(jìn)入到這里了");
return mapping.findForward("newForm");
}
/*
* 1)分頁 2)是否有新的回復(fù)
*
* (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);
// 獲取回復(fù)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);
// 獲取最后回復(fù),它的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數(shù)
Long topicCount = new Long(0);
// 總頁數(shù)
Long pageCount = new Long(0);
// 頁數(shù)大小
Integer pageSize = Constents.piecePageSize;
// 當(dāng)前頁碼
Integer page = this.getPage(request);
// 是否有新的回復(fù)
Boolean[] isNewReplyArray = null;
// 獲取MAP
map = El.getEl().getTopicMgr().getForumPieceTopic(page, pageSize,
pieceId);
// 獲取pageCount 總的頁數(shù)
pageCount = (Long) map.get("pageCount");
// MAP中的topicCount
topicCount = (Long) map.get("topicCount");
// ..page
page = (Integer) map.get("page");
// .....
List<String> lastReply = new ArrayList<String>();
// 獲取是對應(yīng)于list中否有新的回復(fù)
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);
// 獲取最后回復(fù)
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回復(fù)數(shù)
Long topicCount = new Long(0);
// 總頁數(shù)
Long pageCount = new Long(0);
// 獲取設(shè)置回復(fù)頁數(shù)的大小
Integer pageSize = Constents.topicReplyPageSize;
// 當(dāng)前頁碼
Integer page = this.getPage(request);
// 是否有新的回復(fù)
// ////////////////
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點(diǎn)擊
fa.setClick(fa.getClick() + 1);
El.getEl().getTopicMgr().saveTopic(fa);
// 獲取有發(fā)TOPIC相互對應(yīng)的作者
ForumUser topicAuthor = fa.getForumUser();
// 構(gòu)造位置
String position = constructPosition(request, fa.getPiece().getId(), fa
.getPiece().getName(), fa.getId(), fa.getTitle());
request.setAttribute("position", position);
// 獲取map數(shù)據(jù)結(jié)構(gòu)
map = El.getEl().getTopicMgr().getForumTopicReply(page, pageSize,
topicId);
// ...pageCount
pageCount = (Long) map.get("pageCount");
// ..topicCount
topicCount = (Long) map.get("topicCount");
// ..page ..重新復(fù)制,以防轉(zhuǎn)過的PAGE大與實(shí)際數(shù)據(jù)中的page
page = (Integer) map.get("page");
// 獲取回復(fù)
List<Forumtopicreply> replyList = (List<Forumtopicreply>) map
.get("replyList");
// List<Forumtopicreply> falist =
// El.getEl().getTopicMgr().findTopicReply(
// topicId);
// 獲取回復(fù)的作者
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");
}
/*
* 跳到回復(fù)表單頁面 (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);
// 構(gòu)造位置
String position = constructPosition(request, ft.getPiece().getId(), ft
.getPiece().getName(), ft.getId(), ft.getTitle());
request.setAttribute("position", position);
return mapping.findForward("replyForm");
}
/*
* Action:回復(fù)帖子 (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()));
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -