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<? order by id desc limit 0,1 ;");
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+"/";
%>

<!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 } &nbsp;&nbsp;编辑:${viewnews.zuozhe } &nbsp;&nbsp;发布时间:${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>
<span>下一篇: <a href="admin/news.do?method=show&id=${maxnews.id}">${maxnews.title }</a></span>
</div>
<%
}
if(currentid>=maxId){
%>
  <div id="prenext">
<span>上一篇: <a href="admin/news.do?method=show&id=${minnews.id}">${minnews.title }</a></span>
<span>下一篇: 没有下一篇</span></div>
<%
}
}
if(maxNews==null && minNews!=null){
%>
 <div id="prenext">
<span>上一篇: <a href="admin/news.do?method=show&id=${minnews.id}">${minnews.title }</a></span>
<span>下一篇: <font color="red">没有下一篇</font></span></div>
<%
}
if(maxNews!=null && minNews==null){
%>
 <div id="prenext">
<span>上一篇: <font color="red">没有上一篇</font></span>
<span>下一篇: <a href="admin/news.do?method=show&id=${maxnews.id}">${maxnews.title }</a></span>
<%
}
%>

</fieldset>
</body>
</html>
=============================================================
==============================================================
下面开始说第二种方法: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<? order by id desc limit 0,1 ;");
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("<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){
minId = minNews.getId();
System.out.println(minId+"@@@@@@@@@@@@@@@");
String minstr = "admin/news.do?method=show&id=" + 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{
/**
* 后台查看新闻详细信息
*/
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}

你可能感兴趣的:(jsp)