Skip to Sample Code
In this example, we take a single input field, which is used to filter the employee list based on the minimum salary. Follow these steps to implement the solution.
- Start by importing struts-blank.war file into Eclipse.
- Follow the configuration steps 1, 2, 4, 5, and 6 in from the "Pagination with Displaytag" post.
- Create the search page as shown below
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ page import="beans.Employee,data.DAO,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%>
<html:html>
<head>
<title>Search page</title>
<link rel="stylesheet" type="text/css"
href="/StrutsPaging/css/screen.css" />
</head>
<body bgcolor="white">
<html:form action="/search.do">
<table>
<tr>
<td>Minimum Salary:</td>
<td><html:text property="minSalary"></html:text></td>
</tr>
<tr>
<td colspan="2"><html:submit property="submit" /></td>
</tr>
</table>
</html:form>
<logic:notEqual name="empList" value="null">
<jsp:scriptlet>
if (session.getAttribute("empList") != null) {
String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));
DAO.sort((List) session.getAttribute("empList"), sortBy);
}
</jsp:scriptlet>
<display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending">
<display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" />
<display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" />
<display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" />
<display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" />
</display:table>
</logic:notEqual>
</body>
</html:html>pages/search.jsp Note that- The display:table tag has the sort attribute defined as "external".
- Since the sort type is external, we have to provide for the actual sorting, which I implemented in the DAO class itself (see below).
- The column to sort by is obtained by the following peice of code
request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT))
- Create the Action class and Form bean as shown below.
public class SearchAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse httpservletresponse) throws Exception {
if (form == null) {
return mapping.findForward("success");
}
try {
SearchForm searchForm = (SearchForm) form;
if (searchForm.getMinSalary().equals("")) {
return mapping.findForward("success");
}
long minSal = Long.parseLong(searchForm.getMinSalary());
List data = DAO.getData(minSal);
request.getSession().setAttribute("empList", data);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}actions.SearchAction public class SearchForm extends ActionForm {
private String minSalary;
public String getMinSalary() {
return minSalary;
}
public void setMinSalary(String minSalary) {
this.minSalary = minSalary;
}
}forms.SearchForm.java - Modify the struts-config.xml to include the action and actionform as shown below
<form-beans>
<form-bean name="searchForm" type="forms.SearchForm" />
</form-beans>
<action-mappings>
<action path="/Welcome" forward="/pages/Welcome.jsp" />
<action name="searchForm" path="/search"
type="actions.SearchAction" scope="session">
<forward name="success" path="/pages/search.jsp"></forward>
</action>
</action-mappings> - Create the DAO class as shown below
public class DAO {
public static List getData(long minSal) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
List result = null;
try {
session.beginTransaction();
result = session.createQuery("from Employee as emp where emp.empSal >=?").setLong(0, minSal).list();
System.out.println("Result size : " + result.size());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static List sort(List<Employee> list, String sortBy) {
Comparator comp = getComparator(sortBy);
Collections.sort(list, comp);
return list;
}
private static Comparator getComparator(String sortBy) {
System.out.println("Sort by : " + sortBy);
if (sortBy ==null) {
return new NameComparator();
}
if (sortBy.equals("empName"))
return new NameComparator();
if (sortBy.equals("empId"))
return new IdComparator();
if (sortBy.equals("empSal"))
return new SalComparator();
if (sortBy.equals("empJob"))
return new JobComparator();
return null;
}
private static class NameComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
Employee employee1 = (Employee) emp1;
Employee employee2 = (Employee) emp2;
return employee1.getEmpName().compareTo(employee2.getEmpName());
}
}
private static class IdComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
Employee employee1 = (Employee) emp1;
Employee employee2 = (Employee) emp2;
return new Long(employee1.getEmpId()).compareTo(new Long(employee2.getEmpId()));
}
}
private static class SalComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
Employee employee1 = (Employee) emp1;
Employee employee2 = (Employee) emp2;
return new Long(employee1.getEmpSal()).compareTo(new Long(employee2.getEmpSal()));
}
}
private static class JobComparator implements Comparator {
public int compare(Object emp1, Object emp2) {
Employee employee1 = (Employee) emp1;
Employee employee2 = (Employee) emp2;
return employee1.getEmpJob().compareTo(employee2.getEmpJob());
}
}
}DAO.java Note here that- The list sorting is done in this class itself.
- The Comparators are used to compare order the list based on each individual field (there may be scope for improvement here).
No comments:
Post a Comment