Struts 1.3 with COBOL Stored Procedure Implementation with searching, Pagination, Export to Excel and Multilingual
Hi Reader,
I am continuing from my previous blog. In this blog we will be discussed about Pagination and sorting using display tag, export to pdf and excel.
Pagination and Sorting using Display Tag
Display tag library is a open source suite which can be used for displaying tables. The display tag library also helps in paging , sorting , grouping and exporting of the data.
Step 1 :
We to add the following jar files in the the WEB-INF/lib directory of our project.
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
Step 2 :
For using display tag we need to put the following tag extension in the JSP page.
<%@taglib uri="http://displaytag.sf.net" prefix="display"%>
Step 3 :
We need to display the list of employee details like name,dob,gender,address, phone no. EmployeeData class has a loadData() method which will return the ArrayList of all the Employee details.
EmployeeData :
public class EmployeeData
{
private String name;
private String dob;
private String gender;
private String address;
private String email;
public EmployeeData()
{
}
public EmployeeData(String name, String dob, String gender, String address, String email)
{
this.name = name;
this.dob = dob;
this.gender = gender;
this.address = address;
this.email = email
}
public ArrayList loadData()
{
ArrayList empList = new ArrayList(); empList add(new ActorData("Michael Scott","03/04/1981",'male","UK","michael.scott@dundermifflin.com")); empList add(new ActorData("Dwight Schrute","02/06/1990",'male","UK","dwight.schrute@dundermifflin.com")); empList .add(new ActorData("Jim Halpert","01/12/1978",'male","UK","jim.halpert@dundermifflin.com")); empList add(new ActorData("Pam Beesly","03/10/1975",'female","UK","pam.beesly@dundermifflin.com")); userList.add(new ActorData( "Andy Bernad","12/08/1951",'male","UK","andy.bernad@dundermifflin.com")); empList .add(new ActorData("Angela Martin","03/09/1909",'female","UK","angela.martin@dundermifflin.com")); empList .add(new ActorData("Rachel Green","13/11/1954",'male","UK","rachel.green@friends.com")); empList .add(new ActorData("Monica Geller","11/04/1961",'female","UK","monica.geller@friends.com")); empList .add(new ActorData("Phoebe Buffay","28/12/1973",'male","UK","phoebe.buffay@friends.com")); empList .add(new ActorData("Joey Tribbiani","15/04/1964",'female","UK","joey.tribbiani@friends.com")); empList .add(new ActorData("Chandler Bing","27/07/1953",'male","UK","chandler.bing@friends.com")); empList .add(new ActorData("Ross Geller","18/01/1965",'male","UK","ross.geller@friends.com")); return empList ;
}
public String getname() {
return name;
}
public String getdob() {
return dob;
}
public String getgender() {
return gender;
}
public String getaddress() {
return address;
}
public String getemail() {
return email;
}
}
Step 4 :
We need to create EmployeeAction class and inside execute method of the EmployeeAction we need to call loadData() method.
This method will return and ArrayList of the employee details. The ArrayList will be stored in the EmployeeList attribute of the EmployeeForm class.
EmployeeAction :
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
EmployeeForm userForm = (EmployeeForm) form;
EmployeeData empData = new EmployeeData();
EmployeeForm.setEmployeeList(empData.loadData());
return mapping.findForward(SUCCESS);
}
Step 5 :
Finally we need to display all the employee data present in the empList in the JSP page.
The name attribute of the table tag hold the name of the list in the form.The id value specifies an instance name for the ArrayList.The pagesize attribute holds the number of records to be displayed in each page.The requestURI attribute of the table tag contains the action name.When using Tiles if the requestURI is not specified it will through an error when the user navigates to a different page.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 EmployeeData class.The EmployeeData class should have a getter method for that corresponding property.If the sortable attribute column is "true" then the column is sortable.JSP Page :<display:table id="data" name="sessionScope.EmployeeForm.empList" requestURI="/empAction.do" pagesize="10" > <display:column property="name" title="name" sortable="true"/> <display:column property="dob" title="dob" sortable="true"/> <display:column property="gender" title="gender" sortable="true"/> <display:column property="address" title="address" sortable="true"/> <display:column property="email" title="email" sortable="true"/> </display:table>Export To pdf :For exporting employee data to pdf format we need to change "export.pdf" property value to true. The name of the pdf file is filename.<display:setProperty name="export.pdf" value="true"/><display:setProperty name="export.pdf.filename" value="ActorDetails.pdf"/>Export To excel :For exporting employee data to excel format we need to change "export.excel" property value to true. The name of the excel file is filename.<display:setProperty name="export.excel" value="true"/><display:setProperty name="export.excel.filename" value="ActorDetails.excel"/>