Pager-taglib is a jsp label library implementation paging tool, in its demo has a variety of common paging methods, when using only pager-taglib.jar package can be, pager adds "? Page. offset = "to identify the current offset. When used, the pager-taglib tag library is introduced on the jsp page:
<% @ Taglib prefix = "pg" uri = "http://jsptags.com/tags/navigation/pager" %>
Label Introduction
<Pg: pager>: All Sub-tags work in it
<Pg: pager items = "int" maxItems = "int" maxPageItems = "int" maxIndexPages = "int" export = "expression" scope = "page | request" omitted>
Items: Total number of records
MaxPageItems: number of lines displayed per page. The default value is 10.
MaxIndexPages: Maximum number of output pages
Export: This attribute exposes variables to tags. Of course, these variables are optional, for example, in the Pager tag, the variables that can be exposed include pageOffset and pageNumber (see the document), that is, the page number offset and page number. The two variable names can be obtained from the Request in Jsp or Java. The value accepted by the Export property also has an expression, such as currentPage = pageNumber, which exposes the value of pageNumber and assigns it to a variable named CurrentPage, which will be saved to the Request, it can be obtained in Jsp or Java. Scope: Reuqest and page are optional.
<Pg: param>Set the parameters added to the URL, as shown in <pg: param name = "pagesize" value = "5"/>
Pg: first homepage tag
Value that can be exported: pageUrl-pagelink URL address pageNumber-page number firstItem-index value of the first row of the homepage lastItem-index value of the last row of the homepage
Pg: pre Previous Page
Pg: next
Pg: last page
Pg: pages loop output page number
The following is an example:
<% @ Page language = "java" pageEncoding = "UTF-8" %> <% @ taglib prefix = "pg" uri = "http://jsptags.com/tags/navigation/pager" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Running result:
Link: http: // localhost: 8080/msg/testPager. jsp? Pager. offset = 50
Pager-taglib:
First, make sure that it is not because of regular url garbled characters (manually set the received parameter String str = new String (request. getParameter ("something "). getBytes ("ISO-8859-1"), "UTF-8") workable)
Pager-taglib uses the default jdk encoding, so garbled characters may occur during text transmission. You can solve the problem by modifying the source code again:
Create a common java project named pager-taglib. Copy the two files extracted from the source code to src. tld copy to the META-INF, modify the PagerTag under com \ jsptags \ navigation \ pager addParam (String name, String value), it was originally used URLEncoding. the encode (value) method is used to encode the passed parameters and change them to URLEncoding. encode (value, "UTF-8"), into a jar package can be used
The following is an example of a practical application:
Pager. java
Public class Pager <E> {private int pageSize; // The number of private int pageOffset entries displayed on each page. // The start value of the page is private int totalRecord; // total number of records private int totalPage; // total number of pages private List <E> datas; // put the List of specific data // omit getter setter}
DAO layer:
public Pager<User> list(String condition) {int pageOffset = SystemContext.getPageOffset();int pageSize = SystemContext.getPageSize();Connection con = null;PreparedStatement ps = null;ResultSet rs = null;Pager<User> pages = new Pager<User>();List<User> users = new ArrayList<User>();User u = null;try {con = DBUtil.getConnection();String sql = "select * from t_user";String sqlCount = "select count(*) from t_user";if(condition!=null||!"".equals(condition)) {sql+=" where username like '%"+condition+"%' or nickname like '%"+condition+"%'";sqlCount+=" where username like '%"+condition+"%' or nickname like '%"+condition+"%'";}sql+=" limit ?,?";ps = con.prepareStatement(sql);ps.setInt(1, pageOffset);ps.setInt(2, pageSize);rs = ps.executeQuery();while(rs.next()) {u = new User();u.setId(rs.getInt("id"));u.setNickname(rs.getString("nickname"));u.setUsername(rs.getString("username"));}ps = con.prepareStatement(sqlCount);rs = ps.executeQuery();int totalRecord = 0;while(rs.next()) {totalRecord = rs.getInt(1);}int totalPage = (totalRecord-1)/pageSize+1;pages.setPageOffset(pageOffset);pages.setPageSize(pageSize);pages.setTotalPage(totalPage);pages.setTotalRecord(totalRecord);} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(rs);DBUtil.close(ps);DBUtil.close(con);}pages.setDatas(users);return pages;}
Pager. jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" pg "uri =" http://jsptags.com/tags/navigation/pager "%> <% int items = Integer. parseInt (request. getParameter ("items"); String params = request. getParameter ("params"); // conString v = ""; if (params = null) params = ""; if (request. getParameter (params )! = Null) v = new String (request. getParameter (params ). getBytes ("ISO-8859-1"), "UTF-8"); %> <pg: pager maxPageItems = "15" items = "<% = items %>" export = "curPage = pageNumber"> <pg: param name = "<% = params %>" value = "<% = v %>"/> <pg: last> total records <% = items %>, total <% = pageNumber %> pages, </pg: last> current page <% = curPage %> <pg: first> <a href = "<% = pageUrl %>"> homepage </a> </pg: first> <pg: prev> <a href = "<% = pageUrl %>"> previous page </a> </pg: prev> <pg: pages> <% if (curPage = pageNumber) {%> [<% = pageNumber %>] <%} else {%> <a href = "<% = pageUrl %>"> <% = pageNumber %> </ ><%}%> </pg: pages> <pg: next> <a href = "<% = pageUrl %>"> next page </a> </pg: next> <pg: last> <a href = "<% = pageUrl %>"> last page </a> </pg: last> </pg: pager>
In the data display section, you only need to call pager. jsp. To pass the value