article_action.java

来自「前期开发时开发的新闻发布系统」· Java 代码 · 共 421 行 · 第 1/2 页

JAVA
421
字号
package com.xh369.actions;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForward;
import com.xh369.dto.Article_ActionForm;
import com.xh369.dto.Article;
import com.xh369.dto.Articlesviews;
import org.apache.struts.actions.DispatchAction;
import com.xh369.service.ManagerFactory;
import com.xh369.dao.ArticleDAO;
import java.util.*;
import com.xh369.dao.ArticlesviewsDAO;
import com.xh369.dao.BigClassDAO;
import com.xh369.dto.BigClass;
import com.xh369.dao.SmallClassDAO;
import com.xh369.dto.SmallClass;
import com.xh369.service.UtilForm;
import com.xh369.daoimpl.ArticlesviewsDAOimpl;
import java.io.*;

public class Article_Action extends DispatchAction 
{

    ArticleDAO articleDAO = ManagerFactory.createArticleDAO();
    BigClassDAO bigclassDAO = ManagerFactory.createBigClassDAO();
    SmallClassDAO smallclassDAO = ManagerFactory.createSmallClassDAO();
    ArticlesviewsDAO articlesviewsDAO = new ArticlesviewsDAOimpl();
    //每页页大小
    int pageSize = 20;

    //文章添加
    public ActionForward articleAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        Article_ActionForm article_ActionForm = (Article_ActionForm) form;
        try 
        {
            article_ActionForm.setTitle(new String(article_ActionForm.getTitle()
                                                                     .getBytes("ISO-8859-1"), "GB2312"));
            article_ActionForm.setContent(new String(article_ActionForm.getContent()
            														   .getBytes("ISO-8859-1"), "GB2312"));
        } 
        catch(UnsupportedEncodingException ex)
        {
            ex.printStackTrace();
        }
        Article article = (Article) UtilForm.populate(
        												article_ActionForm,
        												"com.xh369.dto.Article",
        												new String[] {"title","content"}
        											 );
        BigClass bigclass = bigclassDAO.loadBigClass(Integer.valueOf(String.valueOf(article_ActionForm.getBigclassid())));
        article.setBigclass(bigclass);
        SmallClass smallclass = smallclassDAO.loadSmallClass(article_ActionForm.getSmallclassid());
        article.setSmallclass(smallclass);
        if(articleDAO.articleAdd(article)) 
        {
            return mapping.findForward("ok");
        } 
        else 
        {
            return mapping.findForward("error");
        }
    }

    //得到页码,从而计算出索引位置
    public int searchUtil(HttpServletRequest request)
    {
        //起始索引
        int position_index = - 1;
        String page = request.getParameter("page") != null ? request.getParameter("page") : "";
        if(!page.equals(""))
        {
            position_index = Integer.parseInt(page);
        }
        else
        {
            position_index = 1;
        }
        return position_index;
    }
    
    //查询标题(通过Ajax)
    public ActionForward searchTitle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {    	
    	String title = request.getParameter("search");
    	List titleList = articleDAO.searchArticleTitle(title);
    	StringBuffer sb = new StringBuffer();
    	for(int i = 0 ; i < titleList.size() ; i ++)
    	{
    		sb.append(titleList.get(i).toString() + "\n");
    	}
    	try 
    	{
			response.getWriter().print(parasToXML(sb));
		} 
    	catch(IOException e) 
    	{
			e.printStackTrace();
		}
    	return null;
    }
    
    //该方法将数据转化成XML格式输出
    public String parasToXML(StringBuffer sb) 
    {    	
		StringBuffer buf = new StringBuffer();
		buf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		buf.append("<pictures>");
		String[] str = sb.toString().split("\n");
		for(int i = 0 ; i < str.length ; i ++) 
		{
			buf.append("<item>");
			buf.append("<title>" + str[i] + "</title>");
			buf.append("</item>");
		}
		buf.append("</pictures>");
		return buf.toString();
	}
    
    //文章浏览(分按id不为空与标题为空时,按id为空与标题不为空时,按id与标题均不为空时,按id与标题均为空时),或者按一级分类名与二级分类名查询
    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {    	
		Articlesviews articlesviews = new Articlesviews();
		List articlesviewsList = articlesviewsDAO.findByExample(articlesviews);
		request.getSession().setAttribute("articles", articlesviewsList);
		request.setAttribute("articlesviewsList", articlesviewsList);
    	String strTitle = request.getParameter("title") == null ? "" : request.getParameter("title");
        request.getSession().setAttribute("constForm", form);
        Article_ActionForm article_ActionForm = (Article_ActionForm) form;
        article_ActionForm.setTitle(strTitle);
        currency(request);
        int id = article_ActionForm.getId() == null ? 0 : Integer.parseInt(article_ActionForm.getId().toString());
        String title = article_ActionForm.getTitle() == null ? "" : article_ActionForm.getTitle();
        String bigclassname = request.getParameter("bigclassname") == null ? "" : request.getParameter("bigclassname");
        String smallclassname = request.getParameter("smallclassname") == null ? "" : request.getParameter("smallclassname");
        String searchid = request.getParameter("searchid") == null ? "" : request.getParameter("searchid");
        Object[][] val = new Object[][]{
                                          {"id.id", new Integer(id)},
                                          {"id.title", title},
                                          {"id.bigclassname", bigclassname},
                                          {"id.smallclassname", smallclassname}
                                        };
        List aList = articlesviewsDAO.findByProperty(val, 1, pageSize);
        if(aList.size()>0) 
        {
            request.getSession().setAttribute("aList", aList);
            //总记录数
            int count = articlesviewsDAO.getCount(val);
            int t = count % pageSize == 0 ? count/pageSize : count/pageSize + 1;
            request.getSession().setAttribute("currpage", 1 + "");
            request.getSession().setAttribute("totalPage",t + "");
            request.getSession().setAttribute("count", count + "");
            List aryList = new ArrayList();
            for(int i = 1; i <= t; i ++)
            {
                aryList.add(i + "");
            }
            request.setAttribute("aryList", aryList);
            if(bigclassname.equals("") || searchid.equals(""))
            {
                request.setAttribute("dis", "none");
            }
            if(!bigclassname.equals("") || !searchid.equals(""))
            {
                request.setAttribute("searchid", searchid);
                request.setAttribute("dis", "");
            }
        } 
        else 
        {
            if(!searchid.equals(""))
            {
                request.setAttribute("searchid", searchid);
                request.setAttribute("dis", "");
            }
            request.setAttribute("noRecords", "没有记录");
        }
        request.setAttribute("play", "");
        request.setAttribute("noplay", "none");
        request.setAttribute("divajax", "none");
        return mapping.findForward("success");
    }
    
    //文章浏览(通过Ajax)
    public ActionForward searchByAjax(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {    	
    	response.setCharacterEncoding("UTF-8");
    	//设置返回数据为xml格式
		response.setContentType("text/xml");
		java.io.PrintWriter out = null;
		try 
		{
			out = response.getWriter();
		} 
		catch(IOException e) 
		{
			e.printStackTrace();
		}
    	currency(request);
    	int position_index = searchUtil(request);
        List aList = articlesviewsDAO.findByProperty(position_index, pageSize);
        if(aList.size()>0) 
        {
        	out.print(parasToXML(aList, request));
        } 
    	return null;
    }

⌨️ 快捷键说明

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