Using display tag library, we can export the data grid as excel, pdf, csv and xml. In the following example we will see how to dispaly data using display tag and to export as excel, pdf, csv and xml.
The following jar files should be placed in the WEB-INF/lib directory
- antlr
- commons-beanutils
- commons-beanutils-1.7.0
- commons-collections-3.1
- commons-digester
- commons-fileupload-1.0
- commons-lang-2.3
- commons-logging
- commons-validator
- displaytag-1.2
- displaytag-export-poi-1.2
- displaytag-portlet-1.2
- itext-1.3
- jakarta-oro
- log4j-1.2.13
- struts
1.
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
The following attributes and methods are present in the ActorData class.
01.
public
class
ActorData
02.
{
03.
private
String tvShow;
04.
private
String userName;
05.
private
String emailId;
06.
public
ActorData()
07.
{
08.
09.
}
10.
11.
public
ActorData(String tvShow, String userName, String emailId)
12.
{
13.
this
.tvShow = tvShow;
14.
this
.userName = userName;
15.
this
.emailId = emailId;
16.
}
17.
public
ArrayList loadData()
18.
{
19.
ArrayList userList =
new
ArrayList();
20.
userList.add(
new
ActorData(
"The Office"
,
"Michael Scott"
,
21.
"michael.scott@dundermifflin.com"
));
22.
userList.add(
new
ActorData(
"The Office"
,
"Dwight Schrute"
,
23.
"dwight.schrute@dundermifflin.com"
));
24.
userList.add(
new
ActorData(
"The Office"
,
"Jim Halpert"
,
25.
"jim.halpert@dundermifflin.com"
));
26.
userList.add(
new
ActorData(
"The Office"
,
"Pam Beesly"
,
27.
"pam.beesly@dundermifflin.com"
));
28.
userList.add(
new
ActorData(
"The Office"
,
"Andy Bernad"
,
29.
"andy.bernad@dundermifflin.com"
));
30.
userList.add(
new
ActorData(
"The Office"
,
"Angela Martin"
,
31.
"angela.martin@dundermifflin.com"
));
32.
userList.add(
new
ActorData(
"Friends"
,
"Rachel Green"
,
33.
"rachel.green@friends.com"
));
34.
userList.add(
new
ActorData(
"Friends"
,
"Monica Geller"
,
35.
"monica.geller@friends.com"
));
36.
userList.add(
new
ActorData(
"Friends"
,
"Phoebe Buffay"
,
37.
"phoebe.buffay@friends.com"
));
38.
userList.add(
new
ActorData(
"Friends"
,
"Joey Tribbiani"
,
39.
"joey.tribbiani@friends.com"
));
40.
userList.add(
new
ActorData(
"Friends"
,
"Chandler Bing"
,
41.
"chandler.bing@friends.com"
));
42.
userList.add(
new
ActorData(
"Friends"
,
"Ross Geller"
,
43.
"ross.geller@friends.com"
));
44.
return
userList;
45.
}
46.
public
String getTvShow() {
47.
return
tvShow;
48.
}
49.
public
String getUserName() {
50.
return
userName;
51.
}
52.
public
String getEmailId() {
53.
return
emailId;
54.
}
55.
56.
}
1.
public
ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
throws
Exception {
2.
UserForm userForm = (UserForm) form;
3.
ActorData actorData =
new
ActorData();
4.
userForm.setActorList(actorData.loadData());
5.
return
mapping.findForward(SUCCESS);
6.
}
1.
<
display:table
id
=
"data"
name
=
"sessionScope.UserForm.actorList"
requestURI
=
"/userAction.do"
pagesize
=
"10"
>
2.
<
display:column
property
=
"tvShow"
title
=
"TV Show"
sortable
=
"true"
media
=
"html"
group
=
"1"
/>
3.
<
display:column
property
=
"userName"
title
=
"User Name"
sortable
=
"true"
/>
4.
<
display:column
property
=
"emailId"
title
=
"Email Id"
sortable
=
"true"
/>
5.
</
display:table
>
1.
<
display:setProperty
name
=
"export.pdf"
value
=
"true"
/>
1.
<
display:setProperty
name
=
"export.pdf.filename"
value
=
"ActorDetails.pdf"
/>
2.
<
display:setProperty
name
=
"export.excel.filename"
value
=
"ActorDetails.xls"
/>
The property attribute of the column tag hold the value of the property to be displayed in this each column. The value of the property can be any one of the property of the ActorData class. The ActorData class should have a getter method for that corresponding property. For instance if the property name is tvShow then their should be a corresponding getTvShow() method in the ActorData class.
If the sortable attribute of the column tag is set to "true" then that column will be sortable.
The group attribute of the column tag is set to "1" , which means each unique data will be displayed only once and will not be repeated for the subsequent times.
The following image shows the first page of the data grid. Since the pagesize is set to ten, ten records are displayed in the first page.
On clicking the Excel link the user will be prompted to open or save the file.
On clicking open the data grid will be displayed in Excel format.
The media property of the column tag is used to specify in which media that column should be shown. If the media is set to "html" then only in the jsp page that particular column will be displayed. If the media is set to "html, excel" then that particular column will be displayed in jsp page as well as in excel.
1.
<
display:column
property
=
"tvShow"
title
=
"TV Show"
sortable
=
"true"
media
=
"html"
group
=
"1"
/>
On clicking open the data grid will be displayed in PDF format.
On clicking the CSV link the data grid will be displayed in the csv format.
On clicking the XML link the data grid will be displayed in the xml format.
You can download the source code of the displaytag export to excel example by clicking on the Download link below.
Source + Lib: Download
0 comments:
Post a Comment