码迷,mamicode.com
首页 > 其他好文 > 详细

新闻发布系统架构

时间:2016-07-01 18:08:25      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

实现效果图:

技术分享

分层架构图:

技术分享

 

关键代码:控制台输出新闻内容

实现类:

技术分享
package cn.news.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import cn.news.dao.BaseDao;
import cn.news.dao.NewsDetailDao;
import cn.news.entity.NewsDetail;

public class NewsDetailDaoImpl extends BaseDao implements NewsDetailDao{
   
    @Test
    public void test() throws Exception
    {
        List<NewsDetail> list=getAllNews();
        for (NewsDetail item : list) {
            System.out.println(item.getNewsContent());
        }
     
    }
    
    @Override
    public List<NewsDetail> getAllNews() throws Exception {
        List<NewsDetail> list=new ArrayList<NewsDetail>();
        
        String sql="select * from newsDetails";
        
        ResultSet rs=executeQuery(sql);
        
        if(rs!=null)
        {
            while(rs.next())
            {
                NewsDetail news=new NewsDetail();
                
                news.setNewsId(rs.getInt("newsId"));
                news.setNewsTitle(rs.getString("newsTitle"));
                news.setNewsContent(rs.getString("newsContent"));
                news.setNewsCreateDate(rs.getDate("newsCreateDate"));
                news.setNewsAuthor(rs.getString("newsAuthor"));
                news.setNewsCategoryId(rs.getInt("newsCategoryId"));
                
                list.add(news);
            }
        }
        return list;
    }

}
技术分享

页面显示新闻标题以及时间:

新增NewsServlet类继承HttpServlet。关键代码:

技术分享
package cn.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import cn.news.entity.NewsDetail;
import cn.news.impl.NewsDetailDaoImpl;

public class NewsServlet extends HttpServlet {

    /**

     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
        
    }

    /**
    
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //实例化dao
        NewsDetailDaoImpl dao=new NewsDetailDaoImpl();
        
        try {
            List<NewsDetail> list=dao.getAllNews();
            
            request.setAttribute("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

}
技术分享

index.jsp

技术分享
   
      <ul class="classlist">
      
      <%
      List<NewsDetail>list=(List<NewsDetail>)request.getAttribute("list");
      
      for(NewsDetail item:list)
      {

      %>
      <li><a href=‘newspages/news_read.jsp‘><%=item.getNewsTitle() %></a><span><%=item.getNewsCreateDate() %></span></li>
      
      <%
      }
      
      %>
      
    <p align="right"> 当前页数:[1/2]&nbsp; <a href="#">下一页</a> <a href="#">末页</a> </p>
      </ul>
技术分享

web.xml

技术分享

新闻发布系统架构

标签:

原文地址:http://www.cnblogs.com/jingpeipei/p/5633873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!