• 新闻发布系统!


    新闻发布系统

    1.首先我们要在NewsDAO中创建一个方法,返回List<NewsEntity>集合,其中pageIndex表示当前页,pageSize表示新闻数量

    public List<NewsEntity> GetSelect(int pageIndex,int pageSize);

    2.NewsDAOImpl类实现了NewsDAO,所以自然继承了GetSelect()方法

    复制代码
    public List<NewsEntity> GetSelect(int pageIndex,int pageSize)
    {
    List<NewsEntity> list=new ArrayList<NewsEntity>();
    String sql="select top "+pageSize+" * from newsDetial where newsId not in(select top "+(pageIndex-1)*pageSize+" newsId from newsDetial)";  
    ResultSet rs=executeQuery(sql);
    if(rs!=null)    
    {
        while(rs.next())
       {
        NewsEntity entity=new NewsEntity();
        entity.setNewsId(rs.getInt("newsId"));
        entity.setNewsTitle(rs.getString("newsTitle"));                    
        entity.setNewsContent(rs.getString("newsContent")); 
           entity.setNewsCreateDate(rs.getDate("newsCreateDate"));         
           entity.setNewsAuthor(rs.getString("newsAuthor"));       
           entity.setNewsCateforyId(rs.getInt("newsCategoryId"));
        list.add(entity);
       }
    }
        return list;
    }  
    复制代码

    3.然后我们新建一个Util包,包中有page方法,来存储向用户展示的数据

    复制代码
    public class page
    {
        //当前页
        private int pageIndex;
        //页面记录数
        private int pageSize;
        //本页显示真实数据
        private List<NewsEntity> list;
        //总页数
        private int totalPages;
        //总记录数
        private int totalRecords;
    
    }
    复制代码

    4.在NewsServlet类中,实现其功能(当给总页数赋值时,需要在NewsDAO中新建一个getAllCount方法,在NewsDaoImpl中实现其方法)

    复制代码
    public int getAllCount() throws Exception {
                int result=0;
                String sql="select count(1) as num from newsDetails";
                ResultSet rs=executeQuery(sql);
                if(rs!=null)
                {
                    if(rs.next())
                    {
                        result=rs.getInt("num");
                    }
                    closeAll();
                }
                return result;
            }
    复制代码
    复制代码
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                NewsDAOImpl news=new NewsDAOImpl();
                try {
                    page pages=new page();
                    //默认显示三条数据
                    int pageSize=3;
                    pages.setPageSize(pageSize);
                    //当前页
                    int myIndex;
                    String pageIndex=request.getParameter("pageIndex");
                    if(pageIndex!=null)
                    {
                        myIndex=Integer.parseInt(pageIndex);
                    }
                    else
                    {
                        myIndex=1;
                    }
                    //给当前页赋值
                    pages.setPageIndex(myIndex);  
                    //总页数
                    int mytotalPage=0;
                    int totalPage=news.getAllCount();
                    if(totalPage%pageSize==0)
                    {
                        mytotalPage=totalPage/pageSize;
                    }
                    else
                    {
                        mytotalPage=totalPage/pageSize+1;
                    }
                    pages.setTotalPages(mytotalPage);
                    //查询所有
                    //List<NewsEntity> list=news.Select();
                    //request.setAttribute("list", list);
                    //泛型数据
                    List<NewsEntity> list=news.getSelect(pages.getPageIndex(), pageSize);
                    pages.setList(list);
                    request.setAttribute("list", pages);
                    //登录
                    String name=request.getParameter("uname");
                    String pwd=request.getParameter("upwd");
                    boolean flag= news.Login(name, pwd);
                    if(flag)
                    {
                        
                        response.sendRedirect("newspages/admin.jsp");
                    }    
                    else
                    {
                        request.getRequestDispatcher("/index.jsp").forward(request, response);
                    }
                    request.getRequestDispatcher("/index.jsp").forward(request, response);
                    } catch (Exception e) {
                    
                    e.printStackTrace();
                    }            
        }
    复制代码
    <ul class="classlist">
            <%
                //从作用域中取出一个泛型
                  page page2=  (page)request.getAttribute("list");
                for(NewsEntity item :page2.getList()){
                //一个item就是一个新闻对象
                
            %>
                <li><a href='<%=path %>/newspages/news_read.jsp'> <%=item.getNewsTitle() %> </a><span> <%=item.getNewsCreateDate() %> </span></li>
                
           <% }
            
            %>
            <p align="right"> 当前页数:[<%=page2.getPageIndex() %>/<%=page2.getTotalPages()   %>]&nbsp; <a href="<%=path %>/Servlet/NewsServlet?pageIndex=<%=page2.getPageIndex()+1%>">下一页</a> <a href="#">末页</a> </p>
          </ul>
    复制代码

    其中,page2.getPageIndex()代表当前页码,page2.getTotalPages()代表了总页码,当点击下一页的时候page2.getPageIndex()要加1

  • 相关阅读:
    【区间DP&&记忆化搜索】乘法游戏
    洛谷P1608路径统计
    2021省选游记
    涂色计划P4170
    01迷宫及路径记录(DFS&&BFS)
    [YBTOJ递推算法强化训练4]序列个数
    C++关于string 的优先队列以及重载运算符
    浅谈C++STL容器
    集合的划分
    图的表示
  • 原文地址:https://www.cnblogs.com/ruixinyu/p/5647507.html
Copyright © 2020-2023  润新知