Export to Excel, PDF, CSV , RTF and XML using Display tag

In this article we will show you how to export data as different formats in struts using display tags. To know more about Display Tag. The display tag library also helps in pagination, sorting and grouping 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

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
    <dependency>
        <groupId>displaytag</groupId>
        <artifactId>displaytag</artifactId>
        <version>1.2</version>
    </dependency
    <dependency>
        <groupId>displaytag</groupId>
        <artifactId>displaytag-export-poi</artifactId>
        <version>1.2</version>
    </dependency>        
</dependencies>

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

1. Model Class

Create an Article.java class and write the following code.

Article.java

package com.kruders.model;
 
import java.io.Serializable;
 
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 StrutsPaginationForm.java that contains the following code.

StrutsPaginationForm.java

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.

StrutsPaginationAction.java

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","http://www.kruders.com/forum/search/article/title/Struts%20Hello%20World"));
        articleList.add(new Article("Struts Login Example","http://www.kruders.com/forum/search/article/title/Struts%20Login%20Example"));
        articleList.add(new Article("Struts DynaActionForm","http://www.kruders.com/forum/search/article/title/Struts%20DynaActionForm"));
        articleList.add(new Article("Struts ForwardAction","http://www.kruders.com/forum/search/article/title/Struts%20ForwardAction"));
        articleList.add(new Article("Struts DispatchAction","http://www.kruders.com/forum/search/article/title/Struts%20DispatchAction"));
        articleList.add(new Article("Struts Client Side Validation","http://www.kruders.com/forum/search/article/title/Struts%20Client%20Side%20Validation"));
        articleList.add(new Article("Struts Built-In Actions","http://www.kruders.com/forum/search/article/title/Struts%20Built-In%20Actions"));
        articleList.add(new Article("Struts EventDispatchAction","http://www.kruders.com/forum/search/article/title/Struts%20EventDispatchAction"));
         
        paginationForm.setArticleList(articleList);
        return mapping.findForward("success");
    }
}

4. Display Tag Properties file

Create displaytag.properties write the following text.

export.types=csv excel xml pdf rtf
export.excel=true
export.csv=true
export.xml=true
export.pdf=true
export.rtf=true
export.excel.class=org.displaytag.export.excel.DefaultHssfExportView
export.pdf.class=org.displaytag.export.DefaultPdfExportView
export.rtf.class=org.displaytag.export.DefaultRtfExportView

5. View

Create welcome.jsp and write the following code.

welcome.jsp

<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<logic:forward name="articles" />

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

articles.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
<html>
    <head>
        <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" />
    </head>
    <body>
    <h2>Articles</h2>
        <display:table export="true"  id="data"
                    name="sessionScope.StrutsPaginationForm.articleList"
                    requestURI="/articles.do" 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"/>
        </display:table>
    </body>
</html>

6. 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" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
    <form-beans>
        <form-bean name="StrutsPaginationForm" type="com.kruders.form.StrutsPaginationForm"/>
    </form-beans>
     
    <global-forwards>
        <forward name="articles" path="/articles.do"/>
    </global-forwards>
     
    <action-mappings>
        <action input="/articles.jsp" name="StrutsPaginationForm" path="/articles"  validate="true" scope="session" type="com.kruders.action.StrutsPaginationAction">
            <forward name="success" path="/articles.jsp" />
        </action>
    </action-mappings>
</struts-config>

7. 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

<welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

7. Run Project

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

Figure 45.1 Figure 45.1

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

Figure 45.2 Figure 45.2

You can download the source code of this example here.

One Response to Export to Excel, PDF, CSV , RTF and XML using Display tag

  1. Sridevi January 26, 2013 at 3:38 pm #

    how to add table caption in the exported pdf?

Leave a Reply