jsp页面显示新闻.公告之类的上一篇下一篇

http://hi.baidu.com/a393060727/blog/item/19dc08d5f2408acd51da4b3b.html

 

 

jsp页面显示新闻.公告之类的上一篇下一篇(转载)
2009-07-14 23:23

此方法有二种:
1 在JSP前台页面判断并输出,(适合初学,易理解)
2 在SERVLET里判断并输出(符合MVC架构)

我们先说第一种方法:

先说dao层,主要是取比当前ID小的只一条,取比当前ID大的只一条,注意排序!下面是在查询时的最基本语句,这是最基本的:
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";

   public News max(Long id)throws Exception{
   /**
   * 取下一条
   */
   News maxnews=null;
   try {
    conn=Dbinit.getConn();
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";
    pstmt=conn.prepareStatement("select * from news where id>? order by id asc limit 0,1 ;");
    pstmt.setLong(1, id);
    rs=pstmt.executeQuery();
    if(rs!=null && rs.next()){
     maxnews=new News();
     maxnews.setId(rs.getLong("id"));
     maxnews.setTitle(rs.getString("title"));
     maxnews.setZuozhe(rs.getString("zuozhe"));
     maxnews.setLaiyuan(rs.getString("laiyuan"));
     maxnews.setContent(rs.getString("content"));
     maxnews.setFabutime(rs.getString("fabutime"));
    }
   
   } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
   }finally{
    Dbinit.close(rs, pstmt, conn);
   }
   return maxnews;
}

public News min(Long id)throws Exception{
   /**
   * 取上一条
   */
   News minnews=null;
   try {
    conn=Dbinit.getConn();
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";
    pstmt=conn.prepareStatement("select * from news where id    pstmt.setLong(1, id);
    rs=pstmt.executeQuery();
    if(rs!=null && rs.next()){
       minnews=new News();
     minnews.setId(rs.getLong("id"));
     minnews.setTitle(rs.getString("title"));
     minnews.setZuozhe(rs.getString("zuozhe"));
     minnews.setLaiyuan(rs.getString("laiyuan"));
     minnews.setContent(rs.getString("content"));
     minnews.setFabutime(rs.getString("fabutime"));
    }
   
   } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
   }finally{
    Dbinit.close(rs, pstmt, conn);
   }
   return minnews;
}

 二 再说servlet层

在我们显示详细新闻的页面show.jsp所用的servlet方法show()中调用上面两个DAO方法

public void maxMin(HttpServletRequest request,HttpServletResponse response)throws Exception{
    /**
    * 专用来供前台show.jsp调用的方法(先经show()调用)
    */
    NewsDao newsdao = new NewsDao();
    News maxNews=null;
    News minNews =null;
    //StringBuffer out=new StringBuffer();
    //Long maxId=0L;
    //Long minId=0L;
    Long currentId=Long.parseLong(request.getParameter("id"));
    maxNews=newsdao.max(currentId);
    minNews=newsdao.min(currentId);
    request.setAttribute("currentid",currentId);
    request.setAttribute("maxnews", maxNews);
    request.setAttribute("minnews", minNews);          
    //return out.toString();
  
}

public void show(HttpServletRequest request,HttpServletResponse response)throws Exception{
    /**
    * 后台查看新闻详细信息
    */
    NewsDao newsdao = new NewsDao();
    Long id= Long.parseLong(request.getParameter("id"));
   News news = null;
   if (id!= 0) {
    try {
     news = newsdao.show(id);
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   this.maxMin(request, response);//调用
    request.setAttribute("viewnews", news);
    request.getRequestDispatcher("showNews.jsp").forward(request,response);
   }
    }

第三:前台show.jsp页面

<%@ page language="java" import="java.util.*,bean.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




   
   
    ${viewnews.title }


   





    


    新闻详细信息
  

     
${viewnews.title }

     
新闻来源:${viewnews.laiyuan }   编辑:${viewnews.zuozhe }   发布时间:${viewnews.fabutime}

     
${viewnews.content }

  

  
  
  
        <%
       News maxNews=null;
    News minNews=null;
       Long maxId=0L;
    Long minId=0L;
    Long currentid=(Long)request.getAttribute("currentid");
    maxNews=(News)request.getAttribute("maxnews");
    minNews=(News)request.getAttribute("minnews");
        if(maxNews!=null && minNews!=null){
       maxId=maxNews.getId();
       minId=minNews.getId();
       if(currentid<=minId){
       %>
      

       上一篇: 没有上一篇
       下一篇: ${maxnews.title }
      

       <%
       }
          if(minId       %>
      

       上一篇: ${minnews.title }
       下一篇: ${maxnews.title }
      

    <%
       }
          if(currentid>=maxId){
    %>
      

       上一篇: ${minnews.title }
       下一篇: 没有下一篇

    <%
       }
        }
    if(maxNews==null && minNews!=null){
       %>
     

       上一篇: ${minnews.title }
       下一篇: 没有下一篇

    <%
    }
    if(maxNews!=null && minNews==null){
       %>
     

       上一篇: 没有上一篇
       下一篇: ${maxnews.title }
    <%
    }
    %>
    
  



=============================================================
==============================================================
下面开始说第二种方法:2 在SERVLET里判断并输出(符合MVC架构)

先说dao层,和上面的方法中一模一样:
主要是取比当前ID小的只一条,取比当前ID大的只一条,注意排序!下面是在查询时的最基本语句,这是最基本的:
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";

   public News max(Long id)throws Exception{
   /**
   * 取下一条
   */
   News maxnews=null;
   try {
    conn=Dbinit.getConn();
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";
    pstmt=conn.prepareStatement("select * from news where id>? order by id asc limit 0,1 ;");
    pstmt.setLong(1, id);
    rs=pstmt.executeQuery();
    if(rs!=null && rs.next()){
     maxnews=new News();
     maxnews.setId(rs.getLong("id"));
     maxnews.setTitle(rs.getString("title"));
     maxnews.setZuozhe(rs.getString("zuozhe"));
     maxnews.setLaiyuan(rs.getString("laiyuan"));
     maxnews.setContent(rs.getString("content"));
     maxnews.setFabutime(rs.getString("fabutime"));
    }
   
   } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
   }finally{
    Dbinit.close(rs, pstmt, conn);
   }
   return maxnews;
}

public News min(Long id)throws Exception{
   /**
   * 取上一条
   */
   News minnews=null;
   try {
    conn=Dbinit.getConn();
    //String max="select max(id) from news where id< "+id+" order by id desc limit 0,1;";
    //String min="select min(id) from news where id> "+id+" order by id asc limit 0,1;";
    pstmt=conn.prepareStatement("select * from news where id    pstmt.setLong(1, id);
    rs=pstmt.executeQuery();
    if(rs!=null && rs.next()){
       minnews=new News();
     minnews.setId(rs.getLong("id"));
     minnews.setTitle(rs.getString("title"));
     minnews.setZuozhe(rs.getString("zuozhe"));
     minnews.setLaiyuan(rs.getString("laiyuan"));
     minnews.setContent(rs.getString("content"));
     minnews.setFabutime(rs.getString("fabutime"));
    }
   
   } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
   }finally{
    Dbinit.close(rs, pstmt, conn);
   }
   return minnews;
}

再说servlet层

public String updown(HttpServletRequest request,HttpServletResponse response)throws Exception{
    NewsDao newsdao = new NewsDao();
    News maxNews=null;
    News minNews=null;
    StringBuffer out=new StringBuffer();
    Long maxId=0L;
    Long minId=0L;
    Long currentId=Long.parseLong(request.getParameter("id"));
    maxNews=newsdao.max(currentId);
    minNews=newsdao.min(currentId);
    if (maxNews != null && minNews != null) {
        maxId = maxNews.getId();
        minId = minNews.getId();
        String maxstr = "admin/news.do?method=show&id=" + maxId;
        String minstr = "admin/news.do?method=show&id=" + minId;
       out.append("

");
      if (minId < currentId && currentId < maxId) {
     out.append("上一篇:"+ minNews.getTitle() + "");
     out.append("下一篇:"+ maxNews.getTitle() + "");
    }   
   }
    if(maxNews != null && minNews == null){
     maxId = maxNews.getId();
     String maxstr = "admin/news.do?method=show&id=" + maxId;
    out.append("上一篇:没有上一篇");
    out.append("下一篇:"+ maxNews.getTitle() + "");
     }
    if(maxNews == null && minNews != null){
        minId = minNews.getId();
        System.out.println(minId+"@@@@@@@@@@@@@@@");
        String minstr = "admin/news.do?method=show&id=" + minId;
       out.append("上一篇:"+ minNews.getTitle() + "");
    out.append("下一篇:没有下一篇");
    }

    out.append("

");
    return out.toString();
}

    public void show(HttpServletRequest request,HttpServletResponse response)throws Exception{
    /**
    * 后台查看新闻详细信息
    */
    NewsDao newsdao = new NewsDao();
    Long id= Long.parseLong(request.getParameter("id"));
   News news = null;
   if (id!= 0) {
    try {
     news = newsdao.show(id);
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

    String updown=this.updown(request, response);
    request.setAttribute("updown", updown);

    request.setAttribute("viewnews", news);
    request.getRequestDispatcher("showNews.jsp").forward(request,response);
   }
    }

最后,在前台调用就方便多了

一句话:${updown}

你可能感兴趣的:(Java技术)