• 新闻发布系统


    实现效果图:

    分层架构图:

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

    实现类:

    复制代码
    复制代码
    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

  • 相关阅读:
    构建CMDB的一些启发
    一个NB的安全认证机制
    SQLAlchemy
    Tornado基本使用
    Tornado源码探寻(请求到来)
    Tornado源码探寻(准备阶段)
    Tornado源码探寻(开篇)
    我的个人博客网站
    IDEA/AS快捷键收集&习惯
    ubuntu命令收集
  • 原文地址:https://www.cnblogs.com/sunbin123/p/6766931.html
Copyright © 2020-2023  润新知