Pager Taglib in practice
Written by coregps on November 26th, 2004 in Java.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
It was few days I’ve not written something here since I got my blog. I’m busy working with an e-business project which I come across the task of pagination. The client requires that he can skip any results page easily. I find that pager taglib is an excellent work. The following is my solution by using pager taglib.
<%@ page import=”java.sql.*”%>
<%@ taglib uri=”
http://jsptags.com/tags/navigation/pager” prefix=”pg” %>
<%
String username = “…”;
String password = “…”;
String driver = “com.mysql.jdbc.Driver”;
String surl = “jdbc:mysql://host:3306/db?autoReconnect=true&useUnicode=true&characterEncoding=gb2312″;
try {
Class.forName(driver); //Load Database Driver
} catch(java.lang.ClassNotFoundException e) {
System.err.println(”DBConn():” + e.getMessage());
}
ResultSet rsUserInfo=null;
try {
Connection conn = DriverManager.getConnection(surl, username, password); //Connect to DBMS
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rsUserInfo = stmt.executeQuery(”SELECT userid, username FROM userinfo”);
} catch(SQLException ex) {
System.err.println(”executeQuery():” + ex.getMessage());
}%>
<%
int maxPageItems = 20;
%>
<pg:pager maxPageItems=”<%=maxPageItems%>” export=”currentPageNumber=pageNumber”>
<table width=”95%” cellspacing=”1″ cellpadding=”0″ align=”center”>
<tr>
<td>ID</td>
<td>Name</td>
</tr>
<%while (rsUserInfo.next()) {%>
<pg:item>
<tr>
<td><%=rsUserInfo.getString(1)%></td>
<td><%=rsUserInfo.getString(2)%></td>
</tr>
</pg:item>
<%}%>
</table>
<table width=100% cellpadding=2 cellspacing=0 border=0 bgcolor=e3e9f8>
<form method=”get”>
<tr><td align=right nowrap width=1%><font face=arial size=-1>
<pg:index export=”total=pageCount”>
<pg:page export=”pageNumber”>
Results Page:<%= pageNumber %> / <%= total %>
</pg:page>
<pg:first export=”url” unless=”current”>
<b><a href=”<%= url %>”>First</a></b> |
</pg:first>
<pg:prev export=”url,first,last”>
<b><a href=”<%= url %>”>Prev</a></b>
</pg:prev>
<pg:next export=”url,first,last”>
| <b><a href=”<%= url %>”>Next</a></b>
</pg:next>
<pg:last export=”url”>
| <b><a href=”<%= url %>”>Last</a></b>
</pg:last>
Skip To:<select name=”pager.offset” onchange=”submit();”>
<%
for (int i = 1; i <= total.intValue(); i++) {%>
<option value=”<%=(i -1) * maxPageItems%>” <%=i==currentPageNumber.intValue()?”selected”:”"%>> <%=i%> </option><%
}
%>
</select>
</font></td></tr></form>
</table>
</pg:index>
</pg:pager>




































September 24th, 2008 at 6:39 pm
Man thank you, thank you thank you very much.