Tuesday, July 27, 2021

How to Export HTML to PDF, CSV, and Excel using Display Tag in JSP? Example

Display tag provides export options to export page into PDF, CSV, Excel, and XML in Java web application written using JSP, Servlet, Struts, or Spring MVC framework. Display tag is a tag library and you just need to use it in your JSP page to display tabular data or generate HTML tables dynamically. Earlier we have seen 10 display tag tips for getting most out of display tag and in this JSP Servlet tutorial we will see one display tag export issue, which prevents display tag export functionality to work properly. 
Display tag experience is considered good in various JSP interviews and some questions related to sorting, paging, both internal paging and external paging and exporting on display tag also appear as various J2EE and Servlet Interview questions.

In my opinion, after the JSTL core tag library, display tag is the most popular tag library and every Servlet and JSP developer should familiar with the display tag.


Display tag Export Example

Display tag export example issue and solution in JSP J2EE JavaIn order to enable export functionality in the display tag, configure the display tag in JSP as shown below. I believe this is the easiest way to include export functionality in Java web applications.

<display:table uid="UserDetailsScreen" name="sessionScope.userList" defaultsort="1" defaultorder="ascending" pagesize="10"  export ="true" requestURI="/SpringTest/displaytag.jsp">
    <display:column property="userId" sortable="true" title="Employee ID" maxLength="25" />
    <display:column property="name" sortable="true" title="Real Name" maxLength="25" />
    <display:column property="email"  title="Email Address" maxLength="25" />
    <display:column property="phone"  title="Phone" maxLength="25" /> 
 
    <display:setProperty name="basic.empty.showtable" value="true" />
    <display:setProperty name="paging.banner.group_size" value="10" />
    <display:setProperty name="paging.banner.item_name" value="user" />
    <display:setProperty name="paging.banner.item_names" value="users" />
 
</display:table>

export="true" option  enable export functionality in displaytag. You also need to include corresponding libraries like :
displaytag-export-poi-1.2.jar , itext-1.3.jar, poi-3.2.jar etc. to export HTML table into Microsoft Excel, PDF , CSV and XML format. In next section we will see a common display tag export issue which can break export functionality in your Java web application.



Displaytag Export Issue

After enabling display tag export functionality, though it was working in the test program, export option on display tag for CSV, PDF, Excel, and XML was not working on the actual project. Display tag was throwing following error message when user clicks on the export button :

"Unable to reset response before returning exported data. You are not using an export filter. Be sure that no other jsp tags are used before display:table or refer to the displaytag documentation on how to configure the export filter"



Cause of Display tag export problem:

We are using Spring security for LDAP authentication and controlling concurrent active session in the Java web applications. Spring security framework uses filters to implement security features and has following filter declared in web.xml

<filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

I think you won't face this display tag export issue until you are using any filter in your web.xml file. As I haven't seen this before adding Spring security support in web application and the export option of displaytag was working even without this configuration.



How to fix Display tag export issue:

You can use the following steps to fix the display tag export issue in your Java web application :

1) Add ResponseOverrideFilter filter as shown below.

<filter>
        <filter-name>ResponseOverrideFilter</filter-name>
        <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>ResponseOverrideFilter</filter-name>
       <url-pattern>*.htm</url-pattern>
</filter-mapping>

Make sure your filter mapping is correct i.e. if your URL pattern is ending with *.jsp then use that or if you using Struts and your URL pattern is using *.do or *.action than specify accordingly. I was using the Spring MVC framework which uses ViewResolver design pattern to resolve view e.g. JSP and we have configured it to listen to .htm extension.

2) Make sure that the display tag's ResponseOverrideFilter must be first filter in web.xml

That’s all on How to fix the display tag export issue in the Java web application. As discussed in 10 display tag examples with JSP and Spring, the display tag is the easiest way to generate tables from JSP based web applications. It also the easiest way to enable export functionality which seamlessly allows user to export the table’s content into PDF, CSV, XML, and Microsoft Excel files. 


Related Java J2EE tutorials from Javarevisited

5 comments:

  1. Hello there, I have a requirement where I am getting some text which is longer than space in column, I wan to display only 50 characters. So if my String contains 50 char than display them as it is otherwise put ellipses at the end e.g. .... and when I hover mouse over them, display complete text as tool tip. I am using display tag, so please suggest, How can we do that?

    ReplyDelete
  2. @Anonymous, you can use maxLength attribute of column tag to only show certain number of characters e.g. 50 and then display tag will put triple dot (...) at the end of it. When you hover mouse, it will show full text. By the way be careful, if you are not displaying value of column from object, instead showing it by yourself, in that case every white space will be counted as character and you might see less number of character, depending upon value of maxLength. You can see in above display tag export to excel example, I have specified maxLength as 25 for most of attributes.

    ReplyDelete
  3. I'm using SpringMVC framework and configured web.xml as stated by you. While clicking on export, a get method inside controller, which is mapped as requestURI attribute is called and shows the view, but excel is not exported. Please help. My web.xml looks like

    spring

    org.springframework.web.servlet.DispatcherServlet

    1


    ResponseOverrideFilter
    org.displaytag.filter.ResponseOverrideFilter


    spring
    *.do




    ResponseOverrideFilter
    *.htm

    ReplyDelete
  4. how can i use Display tag in jsp .while not using any framework(Neither spring nor struts.)

    ReplyDelete
  5. Hi,
    This is My Issue Pls Fix me:
    export.banner=br div class="exportlinks" Export to: {0} div
    paging.banner.all_items_found= 
    export.csv.label=| span class="exportcsv"     CSV /span  |
    export.excel.label=span class="exportexcel"     Excel /span  |
    export.xml.label=span class="exportxml">     XML /span  |
    export.pdf.label=span class="exportpdf">     PDF /span  |
    export.banner.sepchar= 
    paging.banner.no_items_found= 
    paging.banner.one_item_found= 
    basic.msg.empty_list= 
    paging.banner.some_items_found= 


    The Above code is displaytag.properties. I got the csv,xml,Excel file but Pdf is not working. That means, I am unable to c the PDF in our UI. If i got that PDF label surely My code is work fine. Anyone Know the Reason pls help me.

    Thanks and Regards,
    Mohan R
    Mitosis Technologies Pvt., Ltd.,

    ReplyDelete