各位大大,给点帮助 新人自学jsp……有翻页功能,一页显示5条,多少页数由数据库内信息的数量自动更新,被点击过的页码颜色和未点击过的页码颜色不同,非本页的页码有下划线(本页页码没下划线...)差不多就这样了 最好有代码啊……

解决方案 »

  1.   

    学struts了没?还是直接从jsp读取数据库?
      

  2.   


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
    <head>
    <title>书城主页</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head> <body>
    <div id="header" class="wrap">
    <div id="banner"></div>
    <div id="navbar">
    <div class="userMenu">
    <ul>
    <li class="current">
    <c:choose>
    <c:when test="${empty user.userName}">
    <font><a href="login.jsp" style="float: left;">登录&nbsp;&nbsp;&nbsp;||</a>
    <a href="register.jsp" style="float: left;">注册</a></font>
    </c:when>
    <c:otherwise>
    <font color="BLACK">欢迎您,<strong>${user.userName}</strong></font>
    </c:otherwise>
    </c:choose>&nbsp;&nbsp;&nbsp;
    </li>
    <li>
    首页
    </li>
    <li>
    <a href="orderlist.jsp">我的订单</a>
    </li>
    <li>
    <a href="cart.jsp">购物车</a>
    </li>
    <li>
    <a href="logout.jsp">注销</a>
    </li>
    </ul>
    </div>
    </div>
    </div>
    <div id="content" class="wrap">
    <div class="list bookList">
    <form method="post" name="shoping" action="CartBookServlet.do">
    <table>
    <tr class="title">
    <th class="checker"></th>
    <th>书名</th>
    <th class="info">简介</th>
    <th class="price">价格</th>
    <th class="store">库存</th>
    <th class="view">图片预览</th>
    </tr>
    <c:choose>
    <c:when test="${empty books}">
    <tr><td><h2>暂无图书</h2></td></tr>
    </c:when>
    <c:otherwise>
    <c:forEach items="${books}" var="book">
    <tr>
    <td>
    <input type="checkbox" name="bookId" value="${book.bid}" />
    </td>
    <td class="title">
    ${book.bookname }
    <input type="text" name="uid" value="${book.bookname }" style="display: none;" />
    </td>
    <td class="info">
    ${book.info }
    <input type="text" name="info" value="${book.info }" style="display: none;" />
    </td>
    <td>
    ¥${book.price }
    <input type="text" name="price" value="${book.price }" style="display: none;" />
    </td>
    <td>
    ${book.stock }
    </td>
    <td class="thumb">
    <img src="images/book/${book.image }" />
    <input type="text" name="image" value="${book.image }" style="display: none;" />
    </td>
    </tr>
    </c:forEach>
    </c:otherwise>
    </c:choose>
    </table>
    <!-- 分页 -->
    <div class="page-spliter">
    <a href="ListBookServlet.do?pageIndex=1">首页</a>
    <c:forEach begin="1" end="${totalPages}" varStatus="num">
    <c:choose>
    <c:when test="${currentPage == num.count}">
    <span class="current">${num.count}</span>
    </c:when>
    <c:otherwise>
    <a href="ListBookServlet.do?pageIndex=${num.count}">${num.count}</a>
    </c:otherwise>
    </c:choose>
    </c:forEach>
    <a href="ListBookServlet.do?pageIndex=${totalPages}">尾页</a>
    </div>
    <div class="button">
    <input class="input-btn" type="submit" name="submit" value="" />
    </div>
    </form>
    </div>
    </div>
    <div id="footer" class="wrap">
    网上书城 &copy; 版权所有
    </div>
    </body>
    </html>
      

  3.   


    很详细了~我个人觉得,以后你还会写很多项目,很多地方要用到分页,所以我建议你自己写一个分页标签,使用的就是tag。这样呢,以后在项目中,直接把你自己写的这个配置过去,然后就可以完成分页信息了。
    不用每次都写链接,做计算什么的~
      

  4.   

    什么是tag啊……好吧 我在努力的学习中……
      

  5.   


    tag就是标签,我给你贴一个我曾经自己写的吧,仅供参考哦~// 这里是tag标签的代码
    /*
     * Copyright: @ yl_mu 2010
     * 
     * Created on 2010-9-17 下午01:21:54
     */
    package cn.yanmushi.util;import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;/**
     * 类功能描述:分页显示数据
     * 
     * @author <a href="mailto:[email protected]">艳沐石 </a>
     * @version $Id: java_code_templates.xml,v 1.1.4.1 2010/07/23 07:52:13 juchen
     *          Exp $ Create: 2010-9-17 下午01:21:54
     */
    public class Paging extends TagSupport { private static final long serialVersionUID = -7828324903896540155L; private String toUrl;  // 跳转路径
    private int currentPage;  // 当前页面
    private int totalPage;  // 总页面
    private String param;  // 查询参数 private String url;  // 链接路径 /**
     * @return the toUrl
     */
    public String getToUrl() {
    return toUrl;
    } /**
     * @param toUrl
     *            the toUrl to set
     */
    public void setToUrl(String toUrl) {
    this.toUrl = toUrl;
    } /**
     * @return the currentPage
     */
    public int getCurrentPage() {
    return currentPage;
    } /**
     * @param currentPage
     *            the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
    } /**
     * @return the totalPage
     */
    public int getTotalPage() {
    return totalPage;
    } /**
     * @param totalPage
     *            the totalPage to set
     */
    public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
    } /**
     * @return the param
     */
    public String getParam() {
    return param;
    } /**
     * @param param
     *            the param to set
     */
    public void setParam(String param) {
    this.param = param;
    } /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
     */
    @Override
    public int doEndTag() throws JspException {
    int index = getCurrentPage() - 2; // 数字快捷键的游标
    JspWriter out = super.pageContext.getOut();

    confUrl(); // 配置链接路径 try {
    // 当前页大于“1”的时候
    // 添加跳转到“首页”的符号
    // 添加跳转到“上一页”的符号
    if (getCurrentPage() > 1) {
    out.print(getDefaultTagIcon(1, "首页&nbsp;"));
    out
    .print(getDefaultTagIcon(getCurrentPage() - 1,
    "&nbsp;上一页&nbsp;"));
    } // 显示以当前页为中心,左右的快捷数字键
    for (int i = 0; i < 5 && index <= getTotalPage(); i++) {
    // 如果游标小于1的时候,跳过添加标签
    if (index < 1) {
    index++;
    continue;
    }

    // 将数字键值与当前页进行比较
    //     如果相等,则将数字键突出显示
    //     否则,进行默认显示
    if (index == getCurrentPage()) {
    out.print(getCurrentPageTagIcon(index, index + "&nbsp;"));
    } else {
    out.print(getDefaultTagIcon(index, index + "&nbsp;"));
    } index++;  // 游标加1
    } // 当前页小于“总页数”的时候
    // 添加跳转到“末页”的符号
    // 添加跳转到“下一页”的符号
    if (getCurrentPage() < getTotalPage()) {
    out.print(getDefaultTagIcon(getCurrentPage() + 1, "下一页&nbsp;"));
    out.print(getDefaultTagIcon(getTotalPage(), "&nbsp;末页"));
    }
    } catch (Exception e) {
    e.printStackTrace();
    } return EVAL_PAGE; // 继续执行页面的其他内容
    } /*
     * 配置链接路径
     */
    private void confUrl() {
    url = getToUrl() + "?"; // 如果有查询参数,将查询参数添加的链接路径中
    if (getParam() != null) {
    url += getParam();
    }
    } /*
     * 获得默认的标签图标
     */
    private String getDefaultTagIcon(int pagenum, String tagIcon) {
    return confSingleTag(pagenum, tagIcon, 15, "#000000");
    }

    /*
     * 获得当前页的标签图标
     */
    private String getCurrentPageTagIcon(int pagenum, String tagIcon) {
    return confSingleTag(pagenum, tagIcon, 20, "red");
    }

    /*
     * 配置标签字符
     */
    private String confSingleTag(int pagenum, String tagIcon, int fontSize,
    String color) {
    return "<a href=\"" + url + "&dp.currentPage=" + pagenum + "&dp.totalPage="
    + getTotalPage() + "\" style=\"font-size: " + fontSize
    + "px; color: " + color + ";\">" + tagIcon + "</a>";
    }
    }<!-- 这里配置的是标签的tld文件 -->
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                            "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
    <taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>psc</short-name> <!-- 配置分页标签 -->
    <tag>
    <name>paging</name>
    <tag-class>cn.edu.tjuci.util.page.Paging</tag-class>
    <body-content>empty</body-content>

    <attribute>
      <name>toUrl</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>

    <attribute>
    <name>currentPage</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>

    <attribute>
    <name>totalPage</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>

    <attribute>
    <name>param</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
    </taglib>
    <!-- 将tld添加到web.xml文件中 -->
    <jsp-config>
    <taglib>
    <taglib-uri>/pc</taglib-uri>
    <taglib-location>/WEB-INF/tlds/pageControl.tld</taglib-location>
    </taglib>
    </jsp-config>
      

  6.   

    分个页 有那么纠结吗? 不知道什么时候我搞了个大牛写的分页插件,SSH框架的,屡试不爽,留个印儿,发给你。
      

  7.   

    分页网上多的是,后台一个是每页显示个数,一个是多少页,数据库有自带的分页方法,把数据传到前台,用CSS控制下划线,颜色变化的
      

  8.   

    我都有点不好意思发表了
    我这是最没技术含量的
    刚写好,经过测试
    <%@ page language="java" import="java.util.*,java.sql.Connection,util.JdbcUtil,java.sql.SQLException " pageEncoding="utf-8"%><%@page import="java.sql.Statement"%><%@page import="java.sql.ResultSet"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      
        
        <title>My JSP 'Test.jsp' starting page</title>
        

      </head>
      
      <body>
      <%
       /*************************************/

    int nowpage=1;  //nowpage为当前的页面号
    if(request.getParameter("dipage")!=null){
    nowpage=Integer.valueOf(request.getParameter("dipage")).intValue();

     
    /**********************************/
       %>
      
      <center>
        This is my JSP-SQL Server Test Page. <br>
        <table border="1px red solid">
         <tr>
         <td colspan="5"><center>学生信息统计</center></td>
         </tr>
         <tr>
         <td>学号</td>
         <td>姓名</td>
         <td>年龄</td>
         <td>性别</td>
         <td>生日</td>
         </tr>
        
         <%
         Connection conn = null;
         Statement st = null;
         ResultSet rs = null;
         try {
         conn = JdbcUtil.getConnection();
         st   = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
         rs   =st.executeQuery("select * from STUDENT");
        
         /*************** 下面开始分页的内容*****************/
         rs.last();
         int countRecord  = rs.getRow();//countRecord得到结果集ResultSet里面的记录数
         int everyPageRecord = 2;   //everyPageRecord:每一页显示的记录数
         int countPage  = 0;     //countPage 总共的页数
         /*下面来计算countPage*/
         if(countRecord%everyPageRecord==0){
          countPage = countRecord/everyPageRecord;
         }else{
         countPage = countRecord/everyPageRecord+1;
         }
         out.print("countRecord="+countRecord+"countPage="+countPage);
        
         rs.beforeFirst();//将记录指针移动到第一页之前
        
         int point = 0;//point为记录指针移动的次数 
      if(nowpage==1);//如果当前是第1页,什么也不做
      else{  //否则resule指针移动到自己想要的位置
         while(rs.next()){
         point++;
         if(point==((nowpage-1)*everyPageRecord)){   //将result的指针移动到所要显示的页面的第一行数据的前一项数据
         out.print("当前的point="+point);
         break;
         }
         }
         }
        
         point = 0 ; //重新计数
         while(rs.next()){
         out.print("<tr>");
         out.print("<td>");out.print(rs.getInt(1));out.print("</td>");
         out.print("<td>");out.print(rs.getString(2));out.print("</td>");
         out.print("<td>");out.print(rs.getString(3));out.print("</td>");
         out.print("<td>");out.print(rs.getInt("STUAGE"));out.print("</td>");
         out.print("<td>");out.print(rs.getDate("STUBIRTHDAY"));out.print("</td>");
         out.print("</tr>");
         point++;
         if(point==everyPageRecord){
         break;
         }
         }
         out.print("<tr>");
         out.print("<td colspan='5'><center>");
         out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+nowpage+"页,");
         if(nowpage==1);//当前是首页,什么也不做
         else{ //否则
         out.print("<a href=Test.jsp?dipage=1>首页</a>");
         out.print("<a href=Test.jsp?dipage="+(nowpage-1)+">上一页</a>");
         }
        
         if(nowpage==countPage);//当前是末页,什么也不做
         else{ //否则
        
         out.print("<a href=Test.jsp?dipage="+(nowpage+1)+">下一页</a>");
         out.print("<a href=Test.jsp?dipage="+countPage+">末页</a>");
         }
        
         out.print("</center></td>");
         out.print("</tr>");
        
         } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    JdbcUtil.free(rs, st, conn);
    }

          %>
        
        </table>
        
        
          <center>
      </body>
    </html>
      

  9.   

    有的out.print()是作为测试输出的。。你懂的。
      

  10.   

    分页,
    jquery也有一些分页的例子!
      

  11.   

    恩 我懂的………………手动dbug么
      

  12.   

    select top 5 * from table where Id not in (select top "+(CurrentPage-1)*5+" Id  from table Order by Id desc) Order by Id desc