Pages are often used in the network, and there are many paging implementations. The methods for returning paging data from the database are generally similar, mainly because of the differences on the webpage. Generally, we define an object class that stores all paging-related data and settings. When using struts2, you can directly fill the data in the paging object class in the background. Then, you can obtain the corresponding information on the result page by using the label-alive EL expression to display the information on the page. You can use JSP labels to display pages.
First, we define an object class that contains some settings and carries the paging data we want to display.
[Java]
Public class PageBean {
Private int pageSize; // page size
Private int totalrows; // The total number of records.
Private int pageNum; // the total number of pages.
Private int curPage; // the current page number.
Private List items; // store data
// Default constructor, initializing member variables
Public PageBean (int totalRows, int curPage, List items ){
This. pageSize = 10;
This. totalrows = totalRows;
This. curPage = curPage;
This. pageNum = (int) Math. ceil (double) totalRows/pageSize );
This. items = items;
}
// Omit the get/set Method
}
Then we start to write our paging labels. We define a class, let it inherit the TagSupport class, and then rewrite some of the methods here:
[Java]
Public class PagerTag extends TagSupport {
Private String value = "pb"; // name of the data entity
Public void setValue (String value ){
This. value = value;
}
@ Override
Public int doStartTag () throws JspException {
JspWriter out = pageContext. getOut ();
String outStr = makeString ();
Try {
Out. write (outStr );
} Catch (IOException e ){
E. printStackTrace ();
}
Return SKIP_BODY;
}
Private String makeString (){
// Obtain the data entity stored in the request
Object p = pageContext. getRequest (). getAttribute (
Value );
PageBean pageBean = (PageBean) pageContext. getRequest (). getAttribute (value );
StringBuffer sb = new StringBuffer ();
If (pageBean! = Null & pageBean. getItems ()! = Null
& Amp; pageBean. getItems (). size ()> 0 ){
Sb. append ("Total" + pageBean. getCurPage () + "/"
+ PageBean. getPageNum () + "page ");
// Homepage Previous Page
If (pageBean. getCurPage ()> 1 ){
Sb. append ("<a href = '? Page = 1'> homepage </a> ");
Sb. append ("<a href = '? Page = "+ (pageBean. getCurPage ()-1) +" '> previous page </a> ");
}
// Display the intermediate page number cyclically. You can control the maximum number of pages displayed here.
For (int I = 1; I <pageBean. getPageNum () + 1; I ++ ){
If (I = pageBean. getCurPage ()){
Sb. append ("" + I + "");
} Else {
Sb. append ("<a href = '? Page = "+ I +" '> "+ I +" </a> ");
}
}
// Last page of the next page
If (pageBean. getCurPage ()! = PageBean. getPageNum ()){
Sb. append ("<a href = '? Page = "+ (pageBean. getCurPage () + 1) +" '> next page </a> ");
Sb. append ("<a href = '? Page = "+ pageBean. getPageNum () +" '> last page </a> ");
}
}
Return sb. toString ();
}
}
Here we use the value attribute to obtain the PageBean object that we previously filled in data from the background from Requst.
After writing the label class, we need to write the corresponding tlg file:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE taglib PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
<Taglib>
<Tlibversion> 1.2 </tlibversion>
<Jspversion> 1.1 </jspversion>
<Shortname> page </shortname>
<Uri>/luo_pager </uri>
<Tag>
<Name> pager </name>
<Tagclass> com. luojing. pager. PagerTag </tagclass>
<Bodycontent> empty </bodycontent>
<Attribute>
<Name> value </name>
<Required> true </required>
<Rtexprvalue> false </rtexprvalue>
</Attribute>
</Tag>
</Taglib>
Then register in web. xml:
[Html]
<Jsp-config>
<Taglib>
<Taglib-uri>/luo_pager </taglib-uri>
<Taglib-location>/WEB-INF/page_taglib.tld </taglib-location>
</Taglib>
</Jsp-config>
Let's write another test Servlet:
[Java]
Public class PagerServlet extends HttpServlet {
@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
Int curPage = Integer. parseInt (req. getParameter ("page") = null? "1"
: Req. getParameter ("page "));
List list = new ArrayList ();
Int start = (curPage-1) * 10 + 1;
Int end = start + 10> 26? 26: start + 10;
For (int I = start; I <end; I ++ ){
List. add (I );
}
PageBean pageBean = new PageBean (25, curPage, list );
Req. setAttribute ("pb", pageBean );
RequestDispatcher rd = req. getRequestDispatcher ("index. jsp ");
Rd. forward (req, resp );
}
}
In this way, we can use our own labels on our JSP page to display the pagination entries.
[Html]
<% @ Page language = "java" import = "java. util. *, com. luojing. pager. *" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "luo" uri = "/luo_pager" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> pager test </title>
</Head>
<Body>
<%
PageBean p = (PageBean) request. getAttribute ("pb ");
List <Integer> list = p. getItems ();
For (Integer I: list ){
PageContext. getOut (). write ("value:" + I + "<br/> ");
}
%>
<Luo: pager value = "pb"/>
</Body>
</Html>
For convenience, I will output the data carried in the PageBean object cyclically on the JSP page.
The effect is as follows:
In a hurry, the page effect is ugly, but all required functions can be implemented. If other parameters need to be passed on the page, it is best to put the page parameter (specify the page number to be displayed) at the end of the query string so that other parameters can be retained during paging. I have never used JSP tags for paging before, and I am not very familiar with JSP tags. Now let's take a look at JSP custom tags.
Author: jdluojing