1:36 pm - Thursday March 21, 2019

Data export tutorial and example using Struts1x(PDF, XLS, CSV and Doc)

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

  This struts tutorial explain how to download records from database in different format like pdf, xls etc…

We need poi jar file xls download and itext jar file for pdf download.

Project Structure:                         

                                                                   Jar Files:
                                                 TLD Files:                                                                                                                             

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from



package Com.Candidjava;

import java.util.ArrayList;

public class EmployeeData {

	private String id;
	private String userName;
	private String emailId;

	public EmployeeData() {


	public EmployeeData(String id, String userName, String emailId) {
		this.id = id;
		this.userName = userName;
		this.emailId = emailId;

	public ArrayList loadData() {
		ArrayList empList = new ArrayList();
		empList.add(new EmployeeData("001", "Mathanlal sait",
				.add(new EmployeeData("002", "Anburaj",
				.add(new EmployeeData("003", "Vignesh",
		empList.add(new EmployeeData("004", "Antony",
		empList.add(new EmployeeData("005", "Kalaimani",
		empList.add(new EmployeeData("006", "Karthika",
		empList.add(new EmployeeData("007", "Gobukumar",
		empList.add(new EmployeeData("008", "Deepa", "deepa@candidjava.com"));
		empList.add(new EmployeeData("009", "Praphu", "praphu@candidjava.com"));
		empList.add(new EmployeeData("010", "Mahesh", "mahesh@candidjava.com"));
				.add(new EmployeeData("011", "karupia",
		empList.add(new EmployeeData("012", "Rajendran",
		return empList;

	 * @return the id
	public String getId() {
		return id;

	 * @return the userName
	public String getUserName() {
		return userName;

	 * @return the emailId
	public String getEmailId() {
		return emailId;



package Com.Candidjava;

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.ActionMapping;
import org.apache.struts.action.ActionForward;

public class UserAction extends Action {

	/* forward name="success" path="" */
	private final static String SUCCESS = "success";

	 * This is the action called from the Struts framework.
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @throws java.lang.Exception
	 * @return
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		EmployeeData empData = new EmployeeData();
		return mapping.findForward(SUCCESS);


package Com.Candidjava;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private ArrayList employeeList;

	public UserForm() {
		// TODO Auto-generated constructor stub

	 * @return the employeeList
	public ArrayList getEmployeeList() {
		return employeeList;

	 * @param employeeList
	 *            the employeeList to set
	public void setEmployeeList(ArrayList employeeList) {
		this.employeeList = employeeList;



# -- standard errors --
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

		<form-bean name="UserForm" type="Com.Candidjava.UserForm" />



		<forward name="welcome" path="/Welcome.do" />

		<action input="/" name="UserForm" path="/userAction" scope="session"
			<forward name="success" path="/user.jsp" />
		<action path="/Welcome" forward="/welcomeStruts.jsp" />

	<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" />

	<message-resources parameter="Com/Candidjava/ApplicationResource" />

		========================= Tiles plugin ===============================
		This plugin initialize Tiles definition factory. This later can takes
		some parameters explained here after. The plugin first read parameters
		from web.xml, thenoverload them with parameters defined here. All
		parameters are optional. The plugin should be declared in each
		struts-config file. - definitions-config: (optional) Specify
		configuration file names. There can be several comma separated file
		names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if
		the Tiles definition factory is module aware. If true (default), there
		will be one factory for each Struts module. If false, there will be
		one common factory for all module. In this later case, it is still
		needed to declare one plugin per module. The factory will be
		initialized with parameters found in the first initialized plugin
		(generally the one associated with the default module). true : One
		factory per module. (default) false : one single shared factory for
		all modules - definitions-parser-validate: (optional) Specify if xml
		parser should validate the Tiles configuration file. true : validate.
		DTD should be specified in file header (default) false : no validation

		Paths found in Tiles definitions are relative to the main context.
	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
		<set-property property="moduleAware" value="true" />

		========================= Validator plugin
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<jsp:forward page="userAction.do" />


Add an attribute export=true in display tag which enables you to download file in different format.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<display:table id="data" name="sessionScope.UserForm.employeeList"
	requestURI="/userAction.do" pagesize="10" export="true">
	<display:column property="id" title="Id" sortable="true" media="html"
		group="1" />
	<display:column property="userName" title="User Name" sortable="true" />
	<display:column property="emailId" title="Email Id" sortable="true" />
	<display:setProperty name="export.csv.filename"
		value="ActorDetails.csv" />
	<display:setProperty name="export.excel.filename"
		value="ActorDetails.xls" />
	<display:setProperty name="export.pdf.filename"
		value="ActorDetails.pdf" />
	<display:setProperty name="export.pdf" value="true" />


@charset "utf-8";
/* CSS Document */

table {
	border: 1px solid #666;
	width: 80%;
	margin: 20px 0 20px 0 !important;

th,td {
	padding: 2px 4px 2px 4px !important;
	text-align: left;
	vertical-align: top;

thead tr {
	background-color: #0B615E;

th.sorted {
	background-color: #0B615E;

th a,th a:visited {
	color: black;

th a:hover {
	text-decoration: underline;
	color: black;

th.sorted a,th.sortable a {
	background-position: right;
	display: block;
	width: 100%;

th.sortable a {
	background-image: url(../img/arrow_off.png);
    background-repeat: no-repeat;

th.order1 a {
	background-image: url(../img/arrow_down.png);
    background-repeat: no-repeat;

th.order2 a {
	background-image: url(../img/arrow_up.png);
    background-repeat: no-repeat;

tr.odd {
	background-color: #fff

tr.tableRowEven,tr.even {
	background-color: #CCCCCC

.group-1 {
    border-top:1px solid black;
.group-2 {
    border-top: 1px solid black;


.grouped-table tr.even {
    background-color: #fff;
.grouped-table tr.odd {
    background-color: #fff;


DOWNLOAD+SOURCE CODE                                                                       


Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply