In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to achieve paging in J2EE", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to achieve paging in J2EE "this article.
First explain the J2EE to achieve paging need to use the file Pager.java (needless to say, must be saved paging information), PagerHelper.java (processing page information), PageTag.java (custom paging tag), there is a pagetag.tld file, paging tag description file.
Post the source code of these four files first.
1 、 Pager.java
Package org.awc.commns; / * Class that holds paging information * * @ author Administrator * * / public class Pager {private int totalRows; / / Total rows private int pageSize; / / rows displayed per page private int currentPage; / / current page number private int totalPages; / / total pages private int startRow; / / the starting line of the current page in the database private String linkUrl / / URL public Pager () {} public Pager (int _ totalRows,int _ pageSize) {totalRows = _ totalRows; pageSize = _ pageSize; totalPages = totalRows / pageSize; int mod = totalRows% pageSize; if (mod > 0) {totalPages++;} currentPage = 1; startRow = 0 } / / omit the getter and setter methods of the attribute here / * set the number of starting rows of the hibernate query * * @ param currentPage * / public void setStart (int currentPage) {this.currentPage = currentPage; startRow = (currentPage-1) * pageSize;}
2 、 PagerHelper.java
Import javax.servlet.http.*; / * controls the class of paging * * @ author Administrator * * / public class PagerHelper {public static Pager getPager (HttpServletRequest httpServletRequest, int totalRows,int pageSize) {/ / defines the pager object, which is used to pass to the page Pager pager = new Pager (totalRows,pageSize); / / gets the current page number String currentPage = httpServletRequest.getParameter ("cpage") from the Request object / / if the current page number is empty, it means * query the page / / if it is not empty, refresh the pager object and enter information such as the current page number if (currentPage! = null) {pager.setStart (Integer.parseInt (currentPage));} else {pager.setStart (1);} return pager;}}
3 、 PageTag.java
Import java.io.IOException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class PageTag extends TagSupport {/ * / private static final long serialVersionUID = 1L; private Pager pager; public int doStartTag () {try {JspWriter out = pageContext.getOut (); out.print ("page" + pager.getCurrentPage () + "page / total" + (pager.getTotalPages ()) + "page") If (pager.getCurrentPage () = = 1) {out.print ("[top page]"); out.print ("[previous]");} if (pager.getCurrentPage ()! = 1) {out.print ("[
< a href='" + pager.getLinkUrl() + "&cpage=1'>Most homepage
< /a>] "); out.print (" [
< a href='" + pager.getLinkUrl() + "&cpage=" + (pager.getCurrentPage() - 1) + "'>Previous page
< /a>] ")} for (int I = pager.getCurrentPage ()-3; I
< = pager .getCurrentPage() + 3; i++) { if (i < = 0 || i >Pager.getTotalPages () {continue;} if (I = = pager.getCurrentPage ()) {out .print ("[
< span style='color:#FF0000; border: 1px solid #cccccc; font-weight:bold; width:15px;text-align: center;'>"+ I +"
< /span>);} else {out.print ("[
< a href='" + pager.getLinkUrl() + "&cpage=" + i + "'>"+ I +"
< /a>] ");}} if (pager.getCurrentPage () = = pager.getTotalPages () | | pager.getTotalPages () = = 0) {out.print (" [next page] "); out.print (" [last page] ");} if (pager.getCurrentPage ()! = pager.getTotalPages () & & pager.getTotalPages ()! = 0) {out.print (" [
< a href='" + pager.getLinkUrl() + "&cpage=" + (pager.getCurrentPage() + 1) + "'>next page
< /a>] "); out.print (" [
< a href='" + pager.getLinkUrl() + "&cpage=" + (pager.getTotalPages()) + "'>Last page
< /a>] ");} out.flush ();} catch (IOException ex) {ex.printStackTrace ();} return super.SKIP_BODY;} public int doEndTag () {return super.EVAL_PAGE;} public void setPager (Pager pager) {this.pager = pager;} public Pager getPager () {return pager;}}
4 、 pagetag.tld
< !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> < taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> < tlib-version>1.0
< /tlib-version> < jsp-version>1.2
< /jsp-version> < short-name>My Tags
< /short-name> < tag> < name>Page
< /name> < tag-class>Org.awc.commns.PageTag
< /tag-class> < !-- PageTag.java具体项目路径--> < body-content>Empty
< /body-content> < attribute> < name>Pager
< /name> < required>True
< /required> < rtexprvalue>True
< /rtexprvalue> < type>Org.awc.commns.Pager
< /type> < !-- Pager.java具体项目路径--> < /attribute> < /tag> < /taglib>The above four files are fixed, and the rest of the code examples are as follows:
Action or Servlet section:
Public ActionForward display (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {/ / irrelevant code omits the total number of rows recorded by int totalRows;// Pager pager = null; / / page information int pageSize = 15; try {totalRows = tservice.getTotalRowsBySubType (tsid); / / get the total number of lines of the specified topic pager = PagerHelper.getPager (request, totalRows, pageSize) / / initialize the paging object pager.setLinkUrl ("index.asp?method=display&stid=" + tsid); / / set the jump path request.setAttribute ("pb", pager); / / save the paging information in the Request object topicList = tservice.findTopicByStid (tsid, pager); / / query the specified topic request.setAttribute ("results", topicList) based on the paging information; return mapping.findForward ("index") } catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return mapping.findForward ("fail");}
JSP page section:
1. Import the tag library file at the beginning of the page:
< %@ taglib prefix="page" uri="WEB-INF/pageTld/pagetag.tld"%>The specific path depends on the specific project.
2. add in the part where you need to add paging information
< page:page pager="${pb}" />That's it.
The above is all the content of the article "how to achieve paging in J2EE". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.