Wednesday, 22 January 2014

Display Tag Export Example in JSP – Issue Fix Java Tutorial

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 prevent display tag export functionality to work properly. Display tag experience is considered good in various JSP Interview 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 JSTL core tag library, display tag is most popular tag library and every Servlet and JSP developer should familiar with display tag.

Display tag Export Example

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

<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 test program, export option on display tag for CSV, PDF, Excel and XML was not working on actual project. Display tag was throwing following error message, when user clicks on 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 Java web application. 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 export option of displaytag was working even without this configuration.

How to fix Display tag export issue:
You can use following steps to fix 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 Spring MVC framework which uses ViewResolver design pattern to resolve view e.g. JSP and we have configured it to listen .htm extension.

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

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

Related Java J2EE tutorials from Learn About Linux

No comments:

Post a Comment