NoteAction.java
资源名称:Myblog.rar [点击查看]
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:4k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.opensource.blog.web.action;
- import java.util.*;
- import javax.servlet.http.*;
- import org.apache.struts.action.*;
- import com.opensource.blog.exception.*;
- import com.opensource.blog.model.*;
- import com.opensource.blog.service.*;
- import com.opensource.blog.service.cache.app.*;
- import com.opensource.blog.web.form.*;
- public class NoteAction
- extends Action {
- private BlogCache blogCache;
- private NoteService noteService;
- private ArticleService articleService;
- private BlogService blogService;
- public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
- HttpServletRequest servletRequest,
- HttpServletResponse servletResponse) {
- ActionMessages errors = new ActionMessages();
- NoteForm form = (NoteForm) actionForm;
- if (form.getAction().equalsIgnoreCase("add")) {
- HttpSession session = servletRequest.getSession();
- String authCode = (String) session.getAttribute("authcode");
- if (authCode == null || !authCode.equals(form.getAuthCode())) {
- errors.add("error.note.authcode", new ActionMessage("error.note.authcode"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- Blog blog = this.getBlogCache().getFromCache(form.getUsername());
- if (blog == null) {
- errors.add("error.blogblog.getinfo", new ActionMessage("error.blogblog.getinfo"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- Article art = this.getArticleService().findArticleByID_BlogID(form.getArtid(), form.getBlogid());
- if (art == null) {
- errors.add("error.read.getart", new ActionMessage("error.read.getart"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- Note note = new Note();
- note.setArtid(form.getArtid());
- note.setBlogid(form.getBlogid());
- note.setContent(form.getContent());
- note.setEmail(form.getEmail());
- note.setFace(0);
- note.setHomepage(form.getHomepage());
- note.setName(form.getName());
- note.setCreatetime(new Date());
- note.setIp(servletRequest.getRemoteAddr());
- try {
- note = this.getNoteService().saveNote(note);
- }
- catch (BlogException ex) {
- errors.add("error.note.add.error", new ActionMessage("error.note.add.error"));
- saveErrors(servletRequest, errors);
- return actionMapping.findForward("error");
- }
- int total = this.getNoteService().getNoteCountByArtID(form.getArtid());
- art.setCommentnum(total);
- try {
- this.getArticleService().saveArticle(art);
- }
- catch (BlogException ex1) {
- ex1.printStackTrace();
- }
- total = this.getNoteService().getNoteCountByBlogID(form.getBlogid());
- blog.setNotetotal(total);
- try {
- this.getBlogService().saveBlog(blog);
- }
- catch (BlogException ex2) {
- ex2.printStackTrace();
- }
- this.getBlogCache().removeFromCache(form.getUsername());
- /*
- ActionForward f = new ActionForward("/read.do?artid=" + form.getArtid() +
- "&blogid=" + form.getBlogid() + "&username=" +
- form.getUsername(), true);*/
- ActionForward f = new ActionForward("/" + form.getUsername() + "-" + form.getBlogid() + "-" +
- form.getArtid() + ".html", true);
- return f;
- }
- return actionMapping.findForward("error");
- }
- public BlogCache getBlogCache() {
- return blogCache;
- }
- public NoteService getNoteService() {
- return noteService;
- }
- public ArticleService getArticleService() {
- return articleService;
- }
- public BlogService getBlogService() {
- return blogService;
- }
- public void setBlogCache(BlogCache blogCache) {
- this.blogCache = blogCache;
- }
- public void setNoteService(NoteService noteService) {
- this.noteService = noteService;
- }
- public void setArticleService(ArticleService articleService) {
- this.articleService = articleService;
- }
- public void setBlogService(BlogService blogService) {
- this.blogService = blogService;
- }
- }