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

📄 postaction.java

📁 一个不错的bbs论坛系统.对初学者很有帮助
💻 JAVA
字号:
package com.yhbbs.article.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
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.yhbbs.article.bean.ArticleIm;
import com.yhbbs.article.bean.ContentIm;
import com.yhbbs.article.biz.ArticleBiz;
import com.yhbbs.article.form.PostForm;
import com.yhbbs.article.itface.Article;
import com.yhbbs.article.itface.Content;
import com.yhbbs.forum.bean.ForumArtPostIm;
import com.yhbbs.forum.itface.ForumArtPost;
import com.yhbbs.upload.biz.UpLoadBiz;
import com.yhbbs.user.itface.bean.UserSession;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.DateUtils;
import com.yhbbs.vote.biz.VoteBiz;
/**
 * <p>Title:处理发表帖子的Action</p>
 * <li>	包括对发帖、回帖、发表投票、编辑帖子和投票的处理
 * <li> 编辑帖子分:主题帖子和回复帖子
 * <li> 在发表回复帖子后系统要建立更新索引,故建立索引所需的基本属性不可少
 * <li>	处理成功或失败后分别跳转到相关页面并提示相关信息和操作<br>
 * <br><b>WebSite: www.yyhweb.com</b>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YHBBS-2.0
 */
public class PostAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			  HttpServletRequest request,HttpServletResponse response) {
		ActionMessages errors = new ActionMessages();
		if(isTokenValid(request,true)){
			this.resetToken(request);
		}else{
			this.saveToken(request);
			errors.add("article.post.token",new ActionMessage("article.post.token"));
			saveErrors(request,errors);
			return mapping.findForward("Failure");
		}
		
		HttpSession session = request.getSession(true);
		UserSession curUser = (UserSession) session.getAttribute(Constants.bbsuser);
		PostForm postform = (PostForm)form;
		
		String userName = "";
		int userId = 0;
		int maxId = ArticleBiz.getArtMaxId();
		String action = postform.getAction();
		int classId = postform.getClassId();
		int forumId = postform.getForumId();
		String emot = postform.getEmot();
		String pcontent = postform.getContent();
		String postip = request.getRemoteAddr();
		int moneynum = postform.getMoneynum();
		int read = postform.getRead();
		String ptitle = postform.getPtitle();
		int artId = postform.getArtId();
		String repurl = postform.getRepurl();
		String votesel = postform.getVotesel();
		String votect = postform.getVotect();
		String nowtime = DateUtils.getCurFormatDate(Constants.dateFL);
		
		Article article = null;
		Content content = null;
		ForumArtPost forum = new ForumArtPostIm();
		boolean flag = false;		
		if(curUser!=null){
		   userName = curUser.getUsername();
		   userId = curUser.getUserId();
		   article = (Article) new ArticleIm();    	// 帖子信息
		   content = (Content)ContentIm.getInstance();// 内容信息
		   article.setUserid(userId);
		   article.setUser(userName);
		   article.setPosttime(nowtime);
		   article.setForum(forumId);	
		   article.setClassid(classId);
		  if(action.equals("post")||action.equals("replay") ||action.equals("vote")){
			content.setId(maxId);
			content.setContent(pcontent);
			
			article.setPostip(postip);
			article.setTitleimg(emot);
			if(read<4)
				article.setReadmoney(read);
			else
				article.setReadmoney(moneynum);
			
			if(action.equals("replay")){ //	回复帖
				article.setId(maxId);
				article.setParentid(artId);
				article.setTitle(Constants.replaytitle);
			}else{					 //	主题帖
				artId = maxId;
				article.setId(artId);
				article.setParentid(0);
				article.setTitle(ptitle);
			}
			
			if(action.equals("vote")){  // 投票贴
				article.setIsvote(votesel);
				VoteBiz.addVote(votect,artId);
			}else
				article.setIsvote("0");
			
			// 需要更新相关论坛的消息
			forum.setId(forumId);
			forum.setLastartid(artId);
			int titlelength = Constants.titlelength;
			if(ptitle.length()<=titlelength)
				forum.setLastart(ptitle);
			else
				forum.setLastart(ptitle.substring(0,titlelength)+"···");
			forum.setLastuser(userName);
			forum.setLastuserid(userId);
			forum.setLasttime(DateUtils.getCurFormatDate(Constants.dateFL));
			
			flag = ArticleBiz.postArticle(article,content,forum);	// 成功发表帖子
			if(flag){
				if(!action.equals("replay")){
					UpLoadBiz.postArtFile(artId, userId);
				}
				request.setAttribute("aId",artId);
				request.setAttribute("fId",forumId);
				request.setAttribute("cId",classId);
				return mapping.findForward("Success");
			}else{
				errors.add("article.post.fail",new ActionMessage("article.post.fail"));
				saveErrors(request,errors);
				return mapping.findForward("Failure");
			}
		  }else if(action.equals("edit")||action.equals("editr")){  // 修改帖子
			  content.setContent(pcontent);
			  content.setId(artId);
			  article.setId(artId);
			  article.setEdituser(userName);
			  // this for update index
			     article.setParentid(ArticleBiz.getReplayParentId(artId));
			  //
			  if(action.equals("edit")){
				  article.setTitle(ptitle);
			  }else{
				  article.setTitle(Constants.replaytitle);
				  // 如果是修改回复贴,此时request中的aId应该是其主题贴的Id
				  artId = ArticleBiz.getReplayParentId(artId);
			  }
			  article.setTitleimg(emot);
			  if(read<4)
					article.setReadmoney(read);
				else
					article.setReadmoney(moneynum);
			    
			    flag = ArticleBiz.updateArticle(article,content);	// 成功发表帖子
				if(flag){
					request.setAttribute("aId",artId);
					request.setAttribute("fId",forumId);
					request.setAttribute("cId",classId);
					return mapping.findForward("Success");
				}else{
					errors.add("article.edit.fail",new ActionMessage("article.edit.fail"));
					saveErrors(request,errors);
					return mapping.findForward("Failure");
				}
		  }
		}else{
			errors.add("article.post.no.login",new ActionMessage("article.post.no.login"));
			saveErrors(request,errors);
			return mapping.findForward("Failure");
		}
		return null;
	}
}

⌨️ 快捷键说明

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