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

📄 commentaction.java

📁 使用JSP+Servlet+Hibernate+Struts实现的一个学生软件发布平台
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package edu.neu.sspp.struts.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;

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

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.ActionRedirect;
import org.hibernate.Transaction;

import edu.neu.sspp.SSPPLogger;
import edu.neu.sspp.TextAreaConventer;
import edu.neu.sspp.hibernate.HibernateSessionFactory;
import edu.neu.sspp.hibernate.TComment;
import edu.neu.sspp.hibernate.TCommentDAO;
import edu.neu.sspp.hibernate.TProject;
import edu.neu.sspp.hibernate.TProjectDAO;
import edu.neu.sspp.hibernate.TTeacher;
import edu.neu.sspp.hibernate.TTeacherDAO;
import edu.neu.sspp.hibernate.TUser;
import edu.neu.sspp.hibernate.TUserDAO;
import edu.neu.sspp.struts.form.CommentForm;

/** 
 * MyEclipse Struts
 * Creation date: 06-10-2007
 * 
 * XDoclet definition:
 * @struts.action path="/comment" name="commentForm" input="/proj_jsp/proj_info.jsp" scope="request" validate="true"
 */
public class CommentAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * 
	 * struts中的重定向方法
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionRedirect
	 */
	public ActionRedirect execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form;// TODO Auto-generated method stub
		
		try {
			commentForm.setNick(new String(commentForm.getNick().getBytes("ISO-8859-1"), "UTF-8"));
			commentForm.setContent(new String(commentForm.getContent().getBytes("ISO-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		TComment comment = new TComment();
		TCommentDAO commentDAO = new TCommentDAO();
		
		String content = commentForm.getContent();
		if(content.length() > 500)
			content = content.substring(0, 500);
		
		comment.setContent(content);
		
		if(!commentForm.getUnknow())
			comment.setIsUser(new Byte((byte)1));
		else
			comment.setIsUser(new Byte((byte)0));
		

		
		if(commentForm.getNick() == null || 
				commentForm.getNick().equals("")) {
			comment.setNick("匿名");
			SSPPLogger.logInfo("匿名" + " 添加评论" + " IP:" + request.getRemoteAddr());	
		}

		else {
			//身份验证
			String login = (String)request.getSession().getAttribute("login");
			if(login == null) {
				comment.setNick(commentForm.getNick());
				SSPPLogger.logInfo(commentForm.getNick() + "(游客)" + " 添加评论" + " IP:" + request.getRemoteAddr());
			}
			else if(login.equals("user") && !commentForm.getUnknow()) {
				TUserDAO userDAO = new TUserDAO();
				TUser user = userDAO.findById((String)request.getSession().getAttribute("id"));
				if(user.getNick().equals(commentForm.getNick())) {
					comment.setNick(commentForm.getNick());
					comment.setUserName(user.getUserName());
				}
				
				SSPPLogger.logInfo(user.getUserName() + " 添加评论" + " IP:" + request.getRemoteAddr());
			}
			else if(login.equals("teacher") && !commentForm.getUnknow()) {
				TTeacherDAO teacherDAO = new TTeacherDAO();
				TTeacher teacher = teacherDAO.findById((String)request.getSession().getAttribute("id"));
				if(teacher.getRealName().equals(commentForm.getNick())) {
					comment.setNick(commentForm.getNick() + "老师");
					comment.setTeacherEmail(teacher.getEmail());
				}
				
				SSPPLogger.logInfo(teacher.getName() + " 添加评论" + " IP:" + request.getRemoteAddr());		
			}
			else {
				comment.setNick("匿名");
				SSPPLogger.logInfo("匿名" + " 添加评论" + " IP:" + request.getRemoteAddr());
			}
		}
		
		comment.setDate(new Date());
		
		TProjectDAO projectDAO = new TProjectDAO();
		TProject project = projectDAO.findById(commentForm.getProjID());
		project.setCount(project.getCount() + 1);
		
		comment.setTProject(project);
		
		Transaction transaction = 
			HibernateSessionFactory.getSession().beginTransaction();
		
		commentDAO.save(comment);
		
		transaction.commit();
		
		HibernateSessionFactory.closeSession();
		//用以下方法完成重定向
		ActionRedirect redirect = new ActionRedirect(commentForm.getUrl() + "#comment");
		
		return redirect;
	}
}

⌨️ 快捷键说明

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