Hibernate paging technology under JSP

Source: Internet
Author: User

This is a Hibernate paging technology with the least code I know and the most concise, so I am too lazy to reduce the amount of code. The following is a detailed description of the languages that people can understand. There are two paging technologies for Hibernate:

1. Retrieve records from the database and divide the records in the memory. However, if the number of records is large, the efficiency is very high.

2. Hibernate physical paging is used, and only one page is retrieved at a time. The number of records on the page and page that are uploaded from the client must first query the total number of records that match the record, and then query the total number of records and the current page based on the total number of records, the number of records per page can calculate the number of records in the database. However, two queries are inevitable.

Therefore, the advantages and disadvantages of the two methods are summarized. If the data size is not very large, the first method is used. Otherwise, the second method can be used.

Because the amount of information in the database I want to operate has not reached the large standard, I adopted the first method, which is described below.

First, let's take a look at one of my actions:

public ActionForward queryZcDoc(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) {   IZcDocService zcDocService=(IZcDocService)          Application.getInstance().getBean("zcDocServiceProxy");   List docList=zcDocService.queryZcDoc();   request.setAttribute("doc", subMessList);   return mapping.findForward("queryDoc");}

The simple code is to query the data and drop it into a List. Then, setAttribute is displayed on the jsp page.

Next, let's talk about paging. Considering the simplicity and versatility, I encapsulated the paging code into a class separately. Let's look at this class:

public class Fenye {
public List fenye(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){

List list=(ArrayList) request.getAttribute("list");
/*

Some people may not understand it here. Why do we need to include these parameters? Because the action method above is the method before paging, it cannot be seen.

The following post uses the action method after pagination:

public ActionForward queryZcDoc(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

IZcDocService zcDocService=(IZcDocService)Application.getInstance().
getBean("zcDocServiceProxy");
List docList=zcDocService.queryZcDoc();

request.setAttribute("list", docList);
List subMessList=new Fenye().fenye(mapping, form, request, response);

request.setAttribute("doc", subMessList);
return mapping.findForward("queryDoc");
}

Compared with the previous one, there are actually two more lines of code, in order to keep the page simple and use the called method, and then return the required data.

Then let's look at it:

*/List subMessList = null; // The number of records to be displayed after paging is saved: int showCount = 5; // The number of records displayed on each page. Int showPage = 1; // The number of currently displayed pages. Int size = list. size (); // The total number of data obtained. Int pageCount = (size-1)/showCount + 1; // the total number of pages to be displayed if (size
 
  

At this point, the java code has been written. There are 33 lines in brackets. Next, we will display it in jsp. For the sake of page cleanliness and versatility, I put the display on pages into a jsp. See the jsp below:

<%@ page language="java" pageEncoding="gb18030"%>
 <div align=center>
<br>

<%
String method=request.getParameter("method");

The method parameter is the method that treats the specific action differently.

String action=request.getParameter("action");

The role of the parameter action is shown below.

Int showPage = (Integer) (request. getAttribute ("showPage"). intValue ();
Int size = (Integer) (request. getAttribute ("size"). intValue ();
Int pageCount = (Integer) (request. getAttribute ("pageCount"). intValue ();
Int page1 = showPage-1;
Int page2 = showPage + 1;
Int LastPage = pageCount;

%>

<%
Out. println ("Total" + size + "Records & nbsp ");

Out. println ("Total" + pageCount + "Page & nbsp ");
Out. println ("current is the" + showPage + "Page & nbsp ");
If (showPage> 1)
{
Out. println ("<a href = '" + action + ". do? Method = "+ method +" & page = 1'> page 1 </a> ");
}
Else
{
Out. println ("first page ");
}
%>

<%
If (showPage> 1)
{

Out. println ("<a href = '" + action + ". do? Method = "+ method +" & page = "+ page1 +" '> previous page </a> ");
}

Else
{

Out. println ("Previous Page ");

}
%>

<%
If (showPage <pageCount)
{
Out. println ("<a href = '" + action + ". do? Method = "+ method +" & page = "+ page2 +" '> next page </a> ");
}
Else
{
Out. println ("next page ");
}
%>

<%
If (showPage <pageCount)
{

Out. println ("<a href = '" + action + ". do? Method = "+ method +" & page = "+ LastPage +" '> last page </a> ");
}

Else
{

Out. println ("last page ");

}
%>

</Div>

There is no need to explain much about the jsp code. Then, use <jsp: include page = ".../fenye. jsp? Action = link "> </jsp: include> statement.

  1. High-performance, high-flexibility JSP and Servlet Performance Optimization
  2. Advantages and implementation of jstl and el jsp page development
  3. JSP classic configuration instance under Tomcat

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.