Pagination and Sorting in Struts using Display Tag

In this article we will see how to paginate and sort large data in Struts using Display Tag. The display tag library also helps in grouping and exporting of the data.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


Do not forget to include maven dependencies in Eclipse IDE as shown in following link.

1. Model Class

Create an class and write the following code.

package com.kruders.model;
public class Article implements Serializable{
    private String title;
    private String url;
    public Article() {
    public Article(String title, String url) {
        this.title = title;
        this.url = url;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public String getUrl() {
        return url;
    public void setUrl(String url) {
        this.url = url;

2. Action Form

Create a Action Form class and name it that contains the following code.

package com.kruders.form;
import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;
import com.kruders.model.Article;
public class StrutsPaginationForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private ArrayList<article> articleList;
    public ArrayList<article> getArticleList() {
        return articleList;
    public void setArticleList(ArrayList<article> articleList) {
        this.articleList = articleList;

3. Action Class (Controller)

Let us create an Action class that will handle the request.

package com.kruders.action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kruders.form.StrutsPaginationForm;
import com.kruders.model.Article;
public class StrutsPaginationAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        StrutsPaginationForm paginationForm = (StrutsPaginationForm) form;
        ArrayList<article> articleList = new ArrayList<article>();
        articleList.add(new Article("Struts Hello World",""));
        articleList.add(new Article("Struts Login Example",""));
        articleList.add(new Article("Struts DynaActionForm",""));
        articleList.add(new Article("Struts ForwardAction",""));
        articleList.add(new Article("Struts DispatchAction",""));
        articleList.add(new Article("Struts Client Side Validation",""));
        articleList.add(new Article("Struts Built-In Actions",""));
        articleList.add(new Article("Struts EventDispatchAction",""));
        return mapping.findForward("success");

4. View

Create welcome.jsp and write the following code.


<%@taglib uri="" prefix="logic"%>
<logic:forward name="articles" />

Now create another jsp file articles.jsp that contains the Display Tag to paginate and sort the data.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@taglib uri="" prefix="display" %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pagination in Struts using Display Tag</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <display:table export="true"  id="data"
                    requestURI="/" pagesize="5" >
            <display:column property="title" title="Title" sortable="true"   />
            <display:column property="url" title="URL" sortable="true"  />
            <display:setProperty name="export.excel.filename" value="Articles.xls"/>
            <display:setProperty name="export.pdf.filename" value="Articles.pdf"/>
            <display:setProperty name="export.rtf.filename" value="Articles.rtf"/>
            <display:setProperty name="export.csv.filename" value="Articles.csv"/>

5. Struts Config

Create a struts-config.xml file in WEB-INF folder.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
        <form-bean name="StrutsPaginationForm" type="com.kruders.form.StrutsPaginationForm"/>
        <forward name="articles" path="/"/>
        <action input="/articles.jsp" name="StrutsPaginationForm" path="/articles"  validate="true" scope="session" type="com.kruders.action.StrutsPaginationAction">
            <forward name="success" path="/articles.jsp" />

6. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


7. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 44.1

Figure 44.1 Figure 44.1

The folder structure of the example is shown below in Figure 44.2

Figure 44.2 Figure 44.2

You can download the source code of this example here.

No comments yet.

Leave a Reply