天天看點

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();

    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;

    pstmt=conn.preparestatement("select * from news where id<? order by id desc limit 0,1 ;");

       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"));

   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{

    * 背景檢視新聞詳細資訊

    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+"/";

%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">

<html>

<head>

    <base href="<%=basepath%>">

    <title>${viewnews.title }</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<link rel="stylesheet" type="text/css" href="css/styles.css">

</head>

<body>

     <fieldset>

    <legend>新聞詳細資訊</legend>

   <div id="news">

      <div id="title">${viewnews.title }</div>

      <div id="come">新聞來源:${viewnews.laiyuan }   編輯:${viewnews.zuozhe }   釋出時間:${viewnews.fabutime}</div>

      <div id="content">${viewnews.content }</div>

   </div>

   <div style="text-align:center;">[<a href="#" onclick="window.close();"><font color="#ff0000">關閉本頁面</font></a>] </div>

   <!-- 上一篇 下一篇  -->

        <%

       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){

       %>

       <div id="prenext">

       <span>上一篇: 沒有上一篇</span>

       <span>下一篇: <a href="admin/news.do?method=show&id=${maxnews.id}">${maxnews.title }</a></span>

       </div>

       <%

       }

          if(minid<currentid && currentid<maxid){

       <div id="prenext">

       <span>上一篇: <a href="admin/news.do?method=show&id=${minnews.id}">${minnews.title }</a></span>

    <%

          if(currentid>=maxid){

    %>

      <div id="prenext">

       <span>下一篇: 沒有下一篇</span></div>

        }

    if(maxnews==null && minnews!=null){

     <div id="prenext">

       <span>下一篇: <font color="red">沒有下一篇</font></span></div>

    if(maxnews!=null && minnews==null){

       <span>上一篇: <font color="red">沒有上一篇</font></span>

   </fieldset>

</body>

</html>

=============================================================

==============================================================

下面開始說第二種方法:2 在servlet裡判斷并輸出(符合mvc架構)

先說dao層,和上面的方法中一模一樣:

主要是取比目前id小的隻一條,取比目前id大的隻一條,注意排序!下面是在查詢時的最基本語句,這是最基本的:

再說servlet層

public string updown(httpservletrequest request,httpservletresponse response)throws exception{

    stringbuffer out=new stringbuffer();

    long maxid=0l;

    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("<div id='prenext'>");

      if (minid < currentid && currentid < maxid) {

     out.append("<span>上一篇:<a href=" + minstr + ">"+ minnews.gettitle() + "</a></span>");

     out.append("<span>下一篇:<a href=" + maxstr + ">"+ maxnews.gettitle() + "</a></span>");

    }   

    if(maxnews != null && minnews == null){

     maxid = maxnews.getid();

     string maxstr = "admin/news.do?method=show&id=" + maxid;

    out.append("<span>上一篇:沒有上一篇</span>");

    out.append("<span>下一篇:<a href="+maxstr+">"+ maxnews.gettitle() + "</a></span>");

     }

    if(maxnews == null && minnews != null){

        system.out.println(minid+"@@@@@@@@@@@@@@@");

       out.append("<span>上一篇:<a href=" + minstr + ">"+ minnews.gettitle() + "</a></span>");

    out.append("<span>下一篇:沒有下一篇</span>");

    out.append("</div>");

    return out.tostring();

    public void show(httpservletrequest request,httpservletresponse response)throws exception{

    string updown=this.updown(request, response);

    request.setattribute("updown", updown);

最後,在前台調用就友善多了

一句話:${updown}