} } }

    JSTL实现分页

    添加时间:2013-7-7 点击量:

    JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个络续完美的开放源代码的JSP标签库,是由apache的jakarta小组来保护的。JSTL只能运行在支撑JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。在JSP 2.0中也是作为标准支撑的。
    下面介绍应用jstl实现的简单分页。引入的conn.jsp文件是应用jstl连接数据库文件,按照MVC的思惟,JSP作为View层,只是用来负责显示操纵,所以在大型项目开辟中一般不推荐应用JSTL的数据库标签。


    开辟景象:Myeclipse10、sqlservler2005、tomcat7.0.40



    <%@ page language=java import=java.util. pageEncoding=UTF-8%>
    
    <%@ taglib prefix=c uri=http://java.sun.com/jsp/jstl/core %>
    <%@ taglib prefix=sql uri=http://java.sun.com/jsp/jstl/sql %>
    <%@ taglib prefix=fmt uri=http://java.sun.com/jsp/jstl/fmt %>
    <%@ taglib prefix=fn uri=http://java.sun.com/jsp/jstl/functions %>
    <%@include file=conn.jsp %>

    <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
    <html>
    <head>
    <base href=<%=basePath%>>

    <title>My JSP fenye.jsp starting page</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=styles.css>
    -->

    </head>
    <body>
    <div align=center>
    <c:set var=pagesize value=4 scope=page />

    <sql:query var=result1 maxRows=6 startRow=0> Table_1</sql:query>

    <!--记录数 --> <c:set var=number value=¥{result1.rowCount} scope=page />

    <!-- 策画分页数 --> <c:set var=pagenumber value=¥{(number+pagesize-1)/pagesize} />

    <font color=blue><c:out value=¥{number} /></font>



    <table border=1 align=center>
    <tr>
    <td>书名</td>
    <td>作者</td>
    <td>价格</td>
    <th>操纵</th>
    </tr>
    <!--断定是否有信息 -->
    <!-- 若是类别为空 otherwise -->
    <c:choose>

    <c:when test=¥{empty result1.rows}>
    <tr>
    <td colspan=4 align=center>临时没有图书!</td>
    </tr>
    </c:when>

    <c:otherwise>


    <c:forEach items=¥{result1.rowsByIndex} var=row1 begin=¥{param.start} end=¥{param.start+pagesize-1} >
    <tr>
    <td>¥{row1[0]}</td>
    <td>¥{row1[1]}</td>
    <td>¥{row1[2]}</td>
    <td><a href=.jsp?name=¥{row1[0]}>删除</a></td>
    </tr>
    </c:forEach>

    </c:otherwise>
    </c:choose>
    </table>

    <!--分页 -->
    <c:choose>
    <c:when test=¥{param.start>0} >
    <a href=fenye.jsp?start=¥{param.start-pagesize}>上一页</a>
    </c:when>
    <c:otherwise>
    上一页
    </c:otherwise>
    </c:choose>


    <c:choose>
    <c:when test=¥{param.start+pagesize<number} >
    <a href=fenye.jsp?start=¥{param.start+pagesize}>下一页</a>
    </c:when>
    <c:otherwise>
    下一页
    </c:otherwise>
    </c:choose>

    <!-- 显示记录数 -->
    共<font color=red>¥{number}</font>笔记录  

    <!-- 显示分页数 -->
    共<fmt:parseNumber type=number value=¥{pagenumber} integerOnly=true />页

    </div>
    </body>
    </html>


      JSTL标签连接数据库conn.jsp文件:



    <%@ page language=java import=java.util. pageEncoding=UTF-8%>
    
    <%@ taglib prefix=sql uri=http://java.sun.com/jsp/jstl/sql %>
    <%
    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>My JSP conn.jsp starting page</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=styles.css>
    -->

    </head>
    <body>

    <sql:setDataSource
    url=jdbc:sqlserver://localhost:1433;databaseName=library
    driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    user=sa
    password=123
    />
    </body>
    </html>


      导出的数据库脚本:



    USE [library]
    
    GO
    / 对象: Table [dbo].[Table_1] 脚本日期: 07/04/2013 14:32:03 /
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Table_1]
    [name] [nchar]10NOT NULL
    [author] [char]10NULL
    [price] [int] NOT NULL
    CTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED

    [name] ASC
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]
    ON [PRIMARY]

    GO
    SET ANSI_PADDING OFF


    分页实现成果如下:


    原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
    分享到: