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

📄 articleaction.java

📁 数学网
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//Created by MyEclipse Struts// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xslpackage math.article.controller;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.sql.DataSource;import math.article.dao.ArticleDAO;import math.article.model.Article;import math.news.DAO.NewsDAO;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.logging.LogFactory;import org.apache.struts.action.Action;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.upload.FormFile;/** * MyEclipse Struts Creation date: 11-20-2006 *  * XDoclet definition: *  * @struts.action parameter="ARTICLE" validate="true" * @struts.action-forward name="error" path="/sd_login.html" * @struts.action-forward name="success" path="/studentview.jsp" */public class ArticleAction extends Action {	private org.apache.commons.logging.Log __log = LogFactory.getFactory()			.getInstance(this.getClass());	private DataSource ds = null;	public ActionForward execute(ActionMapping actionMapping,			ActionForm actionForm, HttpServletRequest httpServletRequest,			HttpServletResponse httpServletResponse) {		ds = getDataSource(httpServletRequest);		ActionForward myforward = null;		String myaction = actionMapping.getParameter();		// ds = getDataSource(httpServletRequest);		if (isCancelled(httpServletRequest)) {			if (__log.isInfoEnabled()) {				__log.info(" [Article] " + actionMapping.getAttribute()						+ " - action was cancelled");			}			return actionMapping.findForward("cancel");		}		if (__log.isInfoEnabled()) {			__log.info(" [Article] action: " + myaction);		}		if ("".equalsIgnoreCase(myaction)) {			myforward = actionMapping.findForward("failure");		} else if ("ARTICLEADD".equalsIgnoreCase(myaction)) {			myforward = performAdd(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else if ("ARTICLELIST".equalsIgnoreCase(myaction)) {			myforward = performList(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else if ("ARTICLESHOW".equalsIgnoreCase(myaction)) {			myforward = performShow(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else if ("ARTICLEUPDATE".equalsIgnoreCase(myaction)) {			myforward = performUpdate(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else if ("ARTICLESAVE".equalsIgnoreCase(myaction)) {			myforward = performSave(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else if ("ARTICLEDELETE".equalsIgnoreCase(myaction)) {			myforward = performDelete(actionMapping, actionForm,					httpServletRequest, httpServletResponse);		} else {			myforward = actionMapping.findForward("regerror");		}		return myforward;	}	private ActionForward performAdd(ActionMapping mapping,			ActionForm actionForm, HttpServletRequest request,			HttpServletResponse response) {		try {			ArticleDAO articleDAO = new ArticleDAO(ds);			// ��ñ?��Ϣ			ArticleForm articleForm = (ArticleForm) actionForm;			// ʵ��Reg;			Article article = new Article();			// ��RegForm�е�ֵ����reg			BeanUtils.populate(article, BeanUtils.describe(articleForm));			String pic_path = "";			if (articleForm.getTheFile().getFileName() == null					|| articleForm.getTheFile().getFileName().equalsIgnoreCase(							"")) {				pic_path = "";			} else {				if (actionForm instanceof ArticleForm) {					ds = getDataSource(request);					String encoding = request.getCharacterEncoding();					if ((encoding != null)							&& (encoding.equalsIgnoreCase("GB2312"))) {						response.setContentType("text/html;charset=GB2312");					}					String Dir = "../webapps" + request.getContextPath()							+ "/article_download";					File dirName = new File(Dir);					if (!dirName.exists()) {						dirName.mkdirs();					}					String gs = articleForm.getGs();					Date mydate = new Date();					long picname = (long) mydate.getTime();					pic_path = "article_download/" + picname + gs;					FormFile file = articleForm.getTheFile();					try {						InputStream stream = file.getInputStream();						OutputStream bos = new FileOutputStream(Dir + "/"								+ picname + gs);						int bytesRead = 0;						byte[] buffer = new byte[8192];						while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {							bos.write(buffer, 0, bytesRead);						}						bos.close();						// close the stream						stream.close();					} catch (FileNotFoundException fnfe) {						return null;					} catch (IOException ioe) {						return null;					}					// destroy the temporary file created					file.destroy();									}else{					return mapping.findForward("failure");				}			}			article.setArticle_img(pic_path);			article.setUserip(request.getRemoteHost());			articleDAO.insert(article);			return mapping.findForward("success");		} catch (Exception e) {			generalError(request, e);			return mapping.findForward("failure");		}	}	// /////////////////////////////////////////////////////////////	private ActionForward performList(ActionMapping mapping,			ActionForm actionForm, HttpServletRequest request,			HttpServletResponse response) {		try {			HttpSession session = request.getSession();			String condition = "";			ArticleDAO articleDAO = new ArticleDAO(ds);			String article1 = request.getParameter("articletype1");			String article2 = request.getParameter("articletype2");			int offset;			int length = 10;			int articletype1;			int articletype2;			String pageOffset = request.getParameter("pager.offset");			if (article1 != null || article2 != null) {				articletype1 = Integer.parseInt(article1);				articletype2 = Integer.parseInt(article2);				if (pageOffset == null || pageOffset.equals("")) {					offset = 0;					SearchForm searchForm = (SearchForm) actionForm;					if (searchForm.getConditionid() == 1) {						condition = "where articletitle like '%"								+ searchForm.getCondition()								+ "%' and  articletype1=" + articletype1								+ " and articletype2=" + articletype2;					} else if (searchForm.getConditionid() == 2) {						condition = "where articlecontent like '%"								+ searchForm.getCondition()								+ "%' and  articletype1=" + articletype1								+ " and articletype2=" + articletype2;					} else if (searchForm.getConditionid() == 0) {						if (searchForm.getCondition() == null								|| searchForm.getCondition() == null)							condition = "";						else							condition = "where articletitle like '%"									+ searchForm.getCondition()									+ "%' or articlecontent like '%"									+ searchForm.getCondition()									+ "%' and  articletype1=" + articletype1									+ " and articletype2=" + articletype2;					}					session.setAttribute("CONDITION", condition);				} else {					offset = Integer.parseInt(pageOffset);					condition = (String) session.getAttribute("CONDITION");					if (condition == null) {						condition = "";					}				}				List articlelist = (List) articleDAO.list(offset, length,						condition);				int size = articleDAO.getSize("article", condition);				String url = request.getContextPath() + mapping.getPath()						+ ".do";				String pagerHeader = math.util.Pager.generate(offset, size,						length, url);				request.setAttribute("pagerHeader", pagerHeader);				request.setAttribute("LIST", articlelist);				request.setAttribute("articletype1", articlelist);				return mapping.findForward("presuccess");			}			// ����Ϊǰ̨�����б�			else {				if (pageOffset == null || pageOffset.equals("")) {					offset = 0;					SearchForm searchForm = (SearchForm) actionForm;					if (searchForm.getConditionid() == 1) {						condition = "where articletitle like '%"								+ searchForm.getCondition() + "%'";					} else if (searchForm.getConditionid() == 2) {						condition = "where articlecontent like '%"								+ searchForm.getCondition() + "%'";					} else if (searchForm.getConditionid() == 0) {

⌨️ 快捷键说明

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