Today you are going to learn about how to export data as pdf in struts using displaytags.
To know more about displaytags and download the example project
View pagination in struts Tutorial Click here
By default you can export the data's in csv,excel and xml formats but for pdf and rtf you have to add a jar file named itext.jar.
After downloading itext.jar
put it in classpath.
Download the itext.jar file here
itext-1.3.jar Download
In the displaytag.properties file you can add your own displaytag properties so that it doesn't take the default names
export.types=csv excel xml pdf rtf
export.excel=true
export.csv=true
export.xml=true
export.pdf=true
export.rtf=true
export.pdf.class=org.displaytag.export.DefaultPdfExportView
export.rtf.class=org.displaytag.export.DefaultRtfExportView
export.excel.label=Export as Excel
export.csv.label=Export as CSV
export.xml.label=Export as Xml
export.rtf.label=Export as RTF
export.pdf.label=Export as PDF
export.xml.filename=s2sgateway.xml
export.csv.filename=s2sgateway.csv
export.excel.filename=s2sgateway.excel
export.pdf.filename=s2sgateway.pdf
export.rtf.filename=s2sgateway.rtf
To avoid the file downloaded in same .do format when exporting,
we have to add the below code in web.xml file
Quote:<filter>
<filter-name>ResponseOverrideFilter</filter-name>
<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ResponseOverrideFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>ResponseOverrideFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
Here The media types defines the needed xml,csv,excel,pdf and rtf formats
The Label is used to define your own label
The class that are defined are predefined in itext.jar file
The filename is given because when you click the file for exporting it will download with the name filename.do
so it is best to add the filename so it is downloaded with the format in it say s2sgateway.pdf
If filename not defined the pdf is open in browser itself.
Happy coding....