Page number logic and reference code (java)

■ Table of contents 1, at the beginning 2, page number logic 3, code example

■ Introduction When I made a search site, I took a lot of time to display the page number, so I will post it thought. I would appreciate it if you could refer to it. (Since it is an example, please let me know if there is an improvement or better code)

■ Page number logic

When the page is 1, the numbers up to 5 are displayed, and "..." is displayed in between to display the last number. image.png

When the page number is 5 or more, the first page, the front two page numbers, the back two, and the last number are displayed. image.png

If the page is (last page-3), the first page, the last page, and the previous page are displayed. image.png

■ Code example (jsp) The href of the a tag is the code without the link. Since there is no sense in naming variables and constants, please let me know if you have a good name.

<%
//Number of pages / Variable declaration of current page
int currentPage;
int lastPageNum;
//Variable assignment
currentPage = 5;
lastPageNum = 9;
//Constant declaration
final int FIRST_PAGE = 1;
final int FIRST_HALF_CHECK = 5;
final int LAST_HALF_CHECK = 4;
final int BEFORE_AND_AFTER = 2;
%>

<section>
<div class="pageLeft">
<%if(currentPage != FIRST_PAGE){ %>
<a href="">To the previous page</a>
<%} %>
</div>
<div class="pageCenter">
<%if(currentPage >= FIRST_HALF_CHECK){ %>
<a href="">1</a>
…
<%} %>
<%if(currentPage < FIRST_HALF_CHECK){ %>
	<%for(int i = 1; i <= FIRST_HALF_CHECK; i++){ %>
		<%if(i <= lastPageNum){ %>
		//Branch whether to include a tag or not (currently page does not include a tag)
			<%if(i == currentPage){ %>
				<span><%=i %></span>
			<%}else{ %>
				<a href=""><%=i %></a>
			<%} %>
		<%} %>
	<%} %>
<%}else if(lastPageNum - currentPage < LAST_HALF_CHECK){ %>
	<%for(int i = lastPageNum - LAST_HALF_CHECK; i <= lastPageNum; i++){ %>
		<%if(i > 0){ %>
		//Branch whether to include a tag or not (currently page does not include a tag)
			<%if(i == currentPage){ %>
				<span><%=i %></span>
			<%}else{ %>
				<a href=""><%=i %></a>
			<%} %>
		<%} %>
	<%} %>
<%}else{ %>
	<%for(int i = currentPage - BEFORE_AND_AFTER; i <= currentPage + BEFORE_AND_AFTER; i++){ %>
		//Branch whether to include a tag or not (currently page does not include a tag)
		<%if(i == currentPage){ %>
			<span><%=i %></span>
		<%}else{ %>
			<a href=""><%=i %></a>
		<%} %>
	<%} %>
<%} %>
<%if(lastPageNum - currentPage >= LAST_HALF_CHECK){ %>
	…<a href=""><%=lastPageNum %></a>
<%} %>
</div>
<div class="pageRight">
<%if(currentPage != lastPageNum){ %>
	<a href="">to the next page</a>
<%} %>
</div>
</section>

■ Summary I wrote it because I got stuck in page number logic once on the search site. I think there are better ways and improvements, so I would appreciate it if you could let me know.

Recommended Posts

Page number logic and reference code (java)
Basic data types and reference types (Java)
Java 15 implementation and VS Code preferences
Java reference mechanism (stack and heap)
Java 9 new features and sample code
Java pass by value and pass by reference
About Java primitive types and reference types
Java basic data types and reference types
Link Java and C ++ code with SWIG
Script Java code
java (random number)
Java code TIPS
Java sample code 02
Java sample code 03
Java8 method reference
Java sample code 04
Java and JavaScript
XXE and Java
Java sample code 01
Java character code
java8 method reference
JAVA reference materials
My Java reference
About Java basic data types and reference type memory
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
timedatectl and Java TimeZone
[Java] Variables and types
java (classes and instances)
[java] Decimal number → n-ary number n-ary number → decimal number
[Java] Overload and override
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Command to check the number and status of Java threads
Understand the Singleton pattern by comparing Java and JavaScript code
Sample code to parse date and time with Java SimpleDateFormat
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Correct the character code in Java and read from the URL
Eclipse installation and code completion enhancements (Mac for Java development)
Understand the Iterator pattern by comparing Java and JavaScript code