article_action.java

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

JAVA
421
字号
    
    //文章浏览中获得总记录数(通过Ajax)
    public ActionForward searchCountByAjax(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {    	
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html");
		java.io.PrintWriter out = null;
		try 
		{
			out = response.getWriter();
		} 
		catch(IOException e) 
		{
			e.printStackTrace();
		}
    	currency(request);
    	int count = articlesviewsDAO.getCount();
    	if(count % pageSize != 0)
    	{
			count = count / pageSize + 1;
    	}
		else
		{
			count = count / pageSize;
		}
    	System.out.println("count=" + count + "=count");
		out.print(count);
    	return null;
    }
    
    public String parasToXML(List v, HttpServletRequest request) 
    {		
    	HttpSession session = request.getSession();
		StringBuffer buf = new StringBuffer();
		//将向量中的数据组织成XML数据
		buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buf.append("<articlesviews>");
		for(int i = 0; i < v.size() ; i ++) 
		{
			Articlesviews articlesviews = (Articlesviews) v.get(i);
			buf.append("<articlesview>");
			buf.append("<id>" + articlesviews.getId().getId() + "</id>");
			buf.append("<title>" + articlesviews.getId().getTitle() + "</title>");
			buf.append("<deployer>" + session.getAttribute("username") + "</deployer>");
			buf.append("<bigclass>" + articlesviews.getId().getBigclassname() + "</bigclass>");
			buf.append("<smallclass>" + articlesviews.getId().getSmallclassname() + "</smallclass>");
			buf.append("<deploytime>" + articlesviews.getId().getDeploytime() + "</deploytime>");
			buf.append("</articlesview>");
		}
		buf.append("</articlesviews>");
		return buf.toString();
	}
    
    //文章浏览(分按id不为空与标题为空时,按id为空与标题不为空时,按id与标题均不为空时,按id与标题均为空时),用于超链接查询
    public ActionForward searchPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {    	
        currency(request);
        Object constForm = request.getSession().getAttribute("constForm");
        Article_ActionForm article_ActionForm = (Article_ActionForm) constForm;
        //click表示:当其不为空时,则是通过下拉列表提交过来的
        String click = request.getParameter("click") == null ? "" : request.getParameter("click");
        int id = -1;
        String title = "";
        if(click.equals(""))
        {
            id = article_ActionForm.getId() == null ? 0 : Integer.parseInt(article_ActionForm.getId().toString());
            title = article_ActionForm.getTitle() == null ? "" : article_ActionForm.getTitle();
        }
        else
        {
            id = request.getParameter("id").equals("") ? 0 : Integer.parseInt(request.getParameter("id"));
            title = new String(request.getParameter("title")) == null ? "" : request.getParameter("title");
        }
        Object[][] val = new Object[][]{
                                          {"id.id", new Integer(id)},
                                          {"id.title", title},
                                          {"id.bigclassname", ""},
                                          {"id.smallclassname", ""}
                                        };
        int position_index = searchUtil(request);
        List aList = articlesviewsDAO.findByProperty(val, position_index, 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", position_index + "");
            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);
            request.setAttribute("dis", "none");
            return mapping.findForward("success");
        } 
        else 
        {
            return mapping.findForward("error");
        }
    }

    //加载文章添加页面及填充一级分类与二级分类下拉列表(通过一级分类导航到二级分类)
    public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        currency(request);
        return mapping.findForward("show");
    }

    //抽象出一个通用方法,供多处方法调用(即查询出一级分类与二级分类,通过一级分类导航到二级分类)
    void currency(HttpServletRequest request)
    {
        List arrList = articleDAO.searchBigClass();
        if(arrList.size()>0)
        {
            request.setAttribute("arrList", arrList);
        }
    }

    //查看文章详细内容
    public ActionForward searchDesc(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Article article = articleDAO.searchDesc(id);
        if(article != null)
        {
            request.setAttribute("article", article);
            return mapping.findForward("showdesc");
        }
        else
        {
            return mapping.findForward("error");
        }
    }

    //修改发布的文章的信息(首先显示出原数据)
    public ActionForward updateArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        Article_ActionForm article_ActionForm = (Article_ActionForm) form;
        Integer id = Integer.valueOf(request.getParameter("id"));
        Article article = articleDAO.updateArticle(id);
        article_ActionForm.setBigclassid(article.getBigclass().getId().intValue());
        article_ActionForm.setSmallclassid(article.getSmallclass().getId());
        article_ActionForm.setContent(article.getContent());
        article_ActionForm.setTitle(article.getTitle());
        article_ActionForm.setPicpath(article.getPicpath());
        article_ActionForm.setDeploytime(article.getDeploytime());
        if(article != null)
        {
            currency(request);
            request.setAttribute("article_ActionForm", article_ActionForm);
            return mapping.findForward("update");
        }
        else
        {
            return mapping.findForward("error");
        }
    }

    //修改发布的文章的信息
    public ActionForward updateOnlyArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        Article_ActionForm article_ActionForm = (Article_ActionForm) form;
        Integer id = Integer.valueOf(request.getParameter("id"));
        Article article = articleDAO.updateArticle(id);
        article.setTitle(article_ActionForm.getTitle());
        article.setPicpath(article_ActionForm.getPicpath());
        Integer bid = Integer.valueOf(String.valueOf(article_ActionForm.getBigclassid()));
        BigClass bigclass = bigclassDAO.loadBigClass(bid);
        article.setBigclass(bigclass);
        Integer sid = article_ActionForm.getSmallclassid();
        SmallClass smallclass = smallclassDAO.loadSmallClass(sid);
        article.setSmallclass(smallclass);
        article.setContent(article_ActionForm.getContent());
        article.setDeploytime(article_ActionForm.getDeploytime());
        if(articleDAO.updateOnlyArticle(article))
        {
            return searchPage(mapping, form, request, response);//再次查询发布的文章的信息
        }
        else
        {
            return mapping.findForward("error");
        }
    }

    //删除发布的文章的信息
    public ActionForward deleteArticle(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        int ids = Integer.parseInt(request.getParameter("id"));
        Integer id = new Integer(ids);
        if(articleDAO.deleteArticle(id))
        {
            return searchPage(mapping, form, request, response);//再次查询发布的文章的信息
        }
        else
        {
            return mapping.findForward("error");
        }
    }

    //在栏目管理模块中显示一级分类与二级分类
    public ActionForward searchClass(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        currency(request);
        return mapping.findForward("searchclass");
    }
}

⌨️ 快捷键说明

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