search
HomeJavajavaTutorialSimple implementation of JSP paging display effect

Simple implementation of JSP paging display effect

Jan 18, 2018 am 09:46 AM
javascriptEffectshow

This article mainly introduces in detail the simple implementation of JSP paging display effect. It has certain reference and learning value for JSP. Friends who are interested in JSP can refer to this article.

This article The example shares the specific code for the JSP paging display effect for your reference. The specific content is as follows

1. Mysql's limit keyword (DAO)

select * from tablename limit startPoint, numberPerPage;

tablename It is the name of the table to be displayed in pages;

startPoint is the starting position -1;

numberPerPage is the number of items displayed on one page.

For example: select * from comment limit 20,5;

extracts comments 21~25 from the comment table:

2. jQuery load function (page JS)

The limit keyword of MySQL can complete the extraction of records in a certain range (n, n+m], which means that two parameters are needed to Determine the content displayed on a certain page, that is, "page x" and the number displayed on each page.

The number displayed on each page can be set in the program or by the user. The parameter "page x" must be given by the user. When the user clicks the page number, next page/previous page button or jumps to a certain page, the parameter "page x" needs to be sent to the server so that Extract records.

function goToPage(page){

  $('body').load("getComments.do?page=" + page);

}

Or, if both parameters are specified by the user, the function can be written as:

function goToPage(page, numberPerPage){

  $('body').load("getComments.do?page=" + page + "&npp=" + numberPerPage);

}

3, servletReceive the parameters and organize them. Content (servlet file)

The servlet learns that the user wants to browse page X and how many records are displayed on one page by accepting the page and npp parameters in the request object from the jsp page.

int page = Integer.parseInt(req.getParameter("page"));

4. The servlet calculates the displayed page list

Generally, about 10 pages are displayed at a time, that is, if you are on page 52 now, then the optional page list is 50 , 51, 52... Until 60.

The calculation method is, assuming that it is on page x, the starting value is x/10*10, the premise is that x>10. #
int start = 1;
if(page >= 10){
  start = page/10 * 10;
 }

There are two special cases:

① The total number of pages is less than 10

② The number of pages is not an integral multiple of 10

The number of pages will appear If the list is less than 10, it is easy to handle. Just add if

conditional judgment

. The approximate code is as follows:

int total = sm.getCommentCount();
int totalPage = total/itemsPerPage;
if(total % itemsPerPage != 0){
  totalPage += 1;
}
Vector<Integer> pageArr = new Vector<Integer>();
int start = 1;
if(page >= 10){
   start = page/10 * 10;
 }
int num = start;
while(!(num > totalPage || num > start + 10)){
   pageArr.add(new Integer(num));
  ++num;
}

5. Display the page number list on the jsp page.

Through 4 we get a calculated page list pageArr, which shows which pages we should display for the current page so that users can directly click on it in the servlet. Put it into the response object, along with page (current page number) and totalPage (maximum page number) to help us make some judgments

##
<!-- 上一页 按钮 -->
<p id="pageControl">
<c:choose>
<c:when test="${page != 1}">
<a href="checkComments.do?page=${page-1}" rel="external nofollow" ><input type="button" name="lastPage" value="上一页" /></a>
</c:when>
<c:otherwise>
<input type="button" disabled="true" name="lastPage" value="上一页" /><!-- 为了要那个灰掉的button -->
</c:otherwise>
</c:choose>

<!-- 页数列表 -->
<c:forEach items="${pageList}" var="item">
<c:choose>
<c:when test="${item == page}">
<a href="checkComments.do?page=${item}" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="currentPage">${item}</a>
</c:when>
<c:otherwise>
<a href="checkComments.do?page=${item}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${item}</a>
</c:otherwise>
</c:choose>
</c:forEach>

<!-- 下一页 按钮 -->
<c:choose>
<c:when test="${page != totalPages}">
<a href="checkComments.do?page=${page+1}" rel="external nofollow" >
<input type="button" name="nextPage" value="下一页" />
</a>
</c:when>
<c:otherwise>
<input type="button" disabled=true name="nextPage" value="下一页" /><!-- 为了要那个灰掉的button -->
</c:otherwise>
</c:choose>

<!-- 直接跳转 -->
共${totalPages}页 -向<input type="text" id="jumpTo" />页 <input type="button" value="跳转" onclick="jumpTo(${totalPages})" />
</p>

The js function used.

function jumpTo(maxPage){
  var page = $("#jumpTo").val();
  if(page > maxPage || page < 1){
    alert("对不起,无法到达该页")
  }else{
    $(&#39;body&#39;).load(&#39;checkComments.do?page=&#39; + page);
  }
}

6. CSS enhancement effect

In order to highlight the page number we are currently on, we specially made a judgment in the above code:

<c:when test="${item == page}">
<a href="checkComments.do?page=${item}" class="currentPage">${item}</a>
</c:when>

In this way, the current page number will be marked as the currentPage class, so that it can be emphasized in the CSS file. For example:

.currentPage{
  font-weight:bold;
  color:#ff9a00;
}

Or set the width of the following jump page input box

#jumpTo{
width:20px;
}

In this way, the current page will be marked in bold and orange:

7. Improvement

Although it is more convenient to use the a tag method to make links, underlines will appear, which feels very unstylish. You can use css to eliminate it, or add some changes when hovering.

#pageControl a {
  text-decoration:none;
}

The above is the entire content of this article, I hope it will be helpful to everyone’s study! !

Related recommendations:

JSP Basics Introduction

JsPlumb Flow Chart Experience Summary

jsp implements the paging function of upper and lower pages

The above is the detailed content of Simple implementation of JSP paging display effect. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software