How to create Pagination Taglib in Java, Spring

Points To Remember

Bootstrap is one of the major UI componnet used worldwide, So you may want to implemnt pagination using bootstrap in java , j2ee or spring application. So all you need to do is

  1. Create a Class that will behave like a taglib.
  2. Create a Tld file that will map the tablib class with the jsp's.
  3. Use the taglib on jsp using the tld file.

Create Pagination Taglib for Jsp with Bootstrap.

Following is an example of the a custom taglib built for Bootstrap. This taglib takes in

  • uri -> action to be hit when clicked.
  • offset -> the offset of pagination.
  • count -> total number of elements to be shown.
  • max -> maximum number of pages to be shown in the pagination bar.
  • steps -> maximum number of elements to be shown per page.
  • previous -> text to be shown for previous page link.
  • next -> text to be shown for next page link.

PaginationTaglib.java
This is a java class that will behave like a taglib on jsp's.
package com.ekiras.taglib;

import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PaginationTaglib extends SimpleTagSupport {
 private String uri;
 private int offset;
 private int count;
 private int max = 10;
 private int steps = 10;
 private String previous = "Previous";
 private String next = "Next";

 private Writer getWriter() {
  JspWriter out = getJspContext().getOut();
  return out;
 }

 @Override
 public void doTag() throws JspException {
  Writer out = getWriter();

  try {
   out.write("<nav>");
   out.write("<ul class=\"pagination\">");
   
   if(offset<steps)
    out.write(constructLink(1, previous, "disabled", true));
   else
    out.write(constructLink(offset-steps, previous, null, false));
   
   for(int itr=0;itr<count;itr+=steps) {
    if(offset==itr)
     out.write(constructLink((itr/10+1)-1 *steps, String.valueOf(itr/10+1), "active", true));
    else
     out.write(constructLink(itr/10*steps, String.valueOf(itr/10+1), null , false));
   }

   if(offset+steps>count)
    out.write(constructLink(offset+steps, next, "disabled", true));
   else
    out.write(constructLink(offset+steps, next, null , false));
   
   
   out.write("</ul>");
   out.write("</nav>");
  } catch (java.io.IOException ex) {
   throw new JspException("Error in Paginator tag", ex);
  }
 }


 private String constructLink(int page, String text, String className, boolean disabled) {
  StringBuilder link = new StringBuilder("<li");
  if (className != null) {
   link.append(" class=\"");
   link.append(className);
   link.append("\"");
  }
  if(disabled)
   link.append(">").append("<a href=\"#\">"+text+"</a></li>");
  else
   link.append(">").append("<a href=\""+uri+"?offset="+page + "\">"+text+"</a></li>");
  return link.toString();
 }

 public String getUri() {
  return uri;
 }

 public void setUri(String uri) {
  this.uri = uri;
 }

 public int getOffset() {
  return offset;
 }

 public void setOffset(int offset) {
  this.offset = offset;
 }

 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public int getMax() {
  return max;
 }

 public void setMax(int max) {
  this.max = max;
 }

 public String getPrevious() {
  return previous;
 }

 public void setPrevious(String previous) {
  this.previous = previous;
 }

 public String getNext() {
  return next;
 }

 public void setNext(String next) {
  this.next = next;
 }

 public int getSteps() {
  return steps;
 }

 public void setSteps(int steps) {
  this.steps = steps;
 }

}

customTaglib.tld
This is the .tld file that defines how to use the java class as a taglib in jsp's
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>paginator</short-name>
  <uri>/WEB-INF/taglibs/customTaglib.tld</uri>
 
  <tag>
    <name>paginate</name>
    <tag-class>com.ekiras.taglib.PaginationTaglib</tag-class>
    <body-content>empty</body-content>
    
    <attribute>
      <name>next</name>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>previous</name>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>uri</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>offset</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
    <attribute>
      <name>count</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
    <attribute>
      <name>max</name>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
  </tag>
</taglib>
Well that's all that you need to do do create a custom taglib and use it in your jsp's.
<%@ taglib prefix="tag" uri="/WEB-INF/taglibs/customTaglib.tld"%>
You can use this taglib in jsp by including it as mentioned above and use it like shown below.
<tag:paginate max="15" offset="${offset}" count="${count}" uri="${uri}"
  next="&raquo;" previous="&laquo;" />
So this when used in the application may give u an output as shown in image below.



No comments :

Post a Comment