做个jsp留言板,分页显示出问题了,求高手。
SayinglistServlet代码
public class SayinglistServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
String curpage=request.getParameter("curpage");
curpage=(curpage==null?"1":curpage);
String pagesize=request.getParameter("pagesize");
pagesize=(pagesize==null?"10":pagesize);

SayingDAO ds=new SayingDAO();
List list =ds.getSayingByPage(Integer.parseInt(curpage),Integer.parseInt(pagesize));
request.setAttribute("list",list);
request.setAttribute("curpage",curpage);
request.setAttribute("pagesize",pagesize);
request.setAttribute("maxsize",ds.getPageCount(Integer.parseInt(pagesize)));
request.setAttribute("totalItems",ds.getSize());
request.getRequestDispatcher("sayinglist.jsp").forward(request,response);
}
}SayingDAO代码public class SayingDAO
{
private DatabaseTool dt=DatabaseTool.getInstance();
/**
 *  分页查询
 *  @param curpage 当前页
 *  @param pagesize 页大小
 *  @return 当前页留言
 */

public List getSayingByPage(int curpage,int pagesize)
{

MessageFormat mf=new MessageFormat("select top{0} * from message where id not in(select top{1} * from message) order by desc");
String sql=mf.format(new Object[]
{
pagesize,
(curpage-1)*pagesize
});
ResultSet rs=dt.select(sql);
List list=null;
Saying saying =null;
if(rs!=null)
{
list=new ArrayList();
try
{
while(rs.next())
{
saying=new Saying();
saying.setId(rs.getInt("id"));
saying.setTitle(rs.getString("title"));
saying.setAuthor(rs.getString("author"));
saying.setContent(rs.getString("content"));
saying.setDatetime(rs.getString("datetime"));

list.add(saying);
}
}
catch(SQLException e)
{
e.printStackTrace();
list=null;
}
}
return list;
}
/** 添加新留言
* @param saying saying对象
* @return  留言成功返回真,否则假
* @throws UnsupportedEncodingException 
*/
public boolean addSaying(Saying saying) throws UnsupportedEncodingException
{
MessageFormat mf=new MessageFormat("insert into message(id,title,author,content,datetime) values({0},''{1}'',''{2}'',''{3}'',''{4}'');");
Date currTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String d1=new String(formatter.format(currTime).getBytes("gb2312"));
String sql=mf.format(new Object[]
{
saying.getId(),
saying.getTitle(),
saying.getAuthor(),
saying.getContent(),
d1 });
return dt.insert(sql);
}
public int getSize()
{
String sql="select count(*) from message";
ResultSet rs=dt.select(sql);
try
{
rs.next();
return rs.getInt(1);
}
catch(SQLException e)
{
e.printStackTrace();

}
return 0;

}
public int getPageCount(int pagesize)
{
int total=this.getSize();
int maxpage=0;
if(total%pagesize==0)
{
maxpage=total/pagesize;
}
else
{
maxpage=total/pagesize+1;
}
return maxpage;
}
}saylist.jsp代码<html>
<head>

<title>留言列表</title>
<script type="text/javascript">
function hideshow(trx)
{
if(trx.style.display=="none")
{
trx.style.display="block";
}
else
{
trx.style.display="none";
}
}
</script>
<style type="text/css">'
<!--
.STYLE1{color:#ECE9D8}
-->
</style>
<body>

<p align="center"><a href="addsaying.jsp" class="STYLE1">添加留言</p>
<br>
<c:forEach items="${list}" var="cur" varStatus="vs">
<table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
<tr><td width="30%" align="center">标题:</td>
<td width="70%">${cur.title}</td>
</tr>
<tr>
<td width="30%" align="center">ID:</td>
<td width="70%">&nbsp;${cur.id}</td>
</tr>
<tr>
<td width="30%" align="center">留言人:</td>
<td width="70%">${cur.author}</td>
</tr>
<tr>
<td width="30%"align="center">留言时间:</td>
<td width="70%">${cur.datetime}</td>
<tr>
<tr>
<td width="30%" align="center" >留言内容:</td>
<td colspan="3" valign="top">&nbsp;${cur.content}</td>
</tr>
</table>
<br>
</c:forEach>
<span class="normal">
    <a href="SayinglistServlet?curpage=${1}&pagesize=${pagesize}"><span style="font-family:Webdings">9</span></a>
    <c:if test="${curpage == 1}">
    <span style="font-family:Webdings">3</span>
   </c:if>
   <c:if test="${curpage > 1}">
     <a href="SayinglistServlet?curpage=${curpage - 1}&pagesize=${pagesize}"><span style="font-family:Webdings">3</span></a>
   </c:if>
   
    <c:if test="${curpage == maxpage}">
    <span style="font-family:Webdings">4</span>
    </c:if>
    <c:if test="${curpage < maxpage}">
    <a href="SayinglistServlet?curpage=${curpage + 1}&pagesize=${pagesize}"><span style="font-family:Webdings">4</span></a>
    </c:if>
    <a href="SayinglistServlet?curpage=${maxpage}&pagesize=${pagesize}"><span style="font-family:Webdings">:</span></a>
总共${totalItems}条留言&nbsp;&nbsp;总页数:${maxpage}&nbsp;&nbsp;当前页:${curpage}

</body>
</html>在SayingDAO里把("select top{0} * from message where id not in(select top{1} * from message) order by desc"改为"select * from message"就能显示留言,不会分页显示;还有saylist.jsp里的${maxpage}不会显示,哪位师傅教教,刚学jsp。

解决方案 »

  1.   

    select * from message limit curpage*pagesize,pagesize;
    这样才能分页
      

  2.   

    兄弟,你用的是SQL Server吧,但是我没见过这种写法{0}、{1}这种占位符SQL Server能识别吗?
    MessageFormat 又是什么东西?
    你的代码看我的头痛!从没见过你这里的写法!
    你把{0}、{1}换成两个?号,或者是直接拼,这样试下!我相信这样可以的...
      

  3.   

    我用的是mysql,上面的方法试过了,没效果;还有saylist.jsp里的${maxpage}不会显示
      

  4.   

    mysql?“select top{0} * from message where ” 你的语句对么? mysql 中有 top 这个命令么? 貌似用limit 来代替吧
      

  5.   

    楼主你SayinglistServlet 中都没有maxpage属性,el表达式取不到数据,就显示空了。