Using the Display Tag with external sorting and paging

I was comparing between using using Value List Handler and the Display Tag Library to do sorting and paging of my web appliaction's result sets and decided on using the Display Tag Library for it's much simpler configuration and because of the latest release's external sorting and paging features. Created a helper class to get the parameters provided by the display tag to pass to the DAOs, otherwise the code in the controller/action gets a litlle messy. Here's pieces of what i did.

In the JSP

    <display:table name="jobs" sort="external" defaultsort="1" pagesize="${pageSize}" id="jobs" partialList="true" size="${size}" requestURI="">
<display:column property="datePosted" format="{0,date,dd MMM yy}" sortName="datePosted" sortable="true" title="Date" />
<display:column sortable="true" sortName="position" title="Position" >
<a href="job.view?id=${jobs.id}">${jobs.position}</a>
</display:column>
<display:column property="company.name" sortable="true" sortName="company.name" title="Company" />
<display:column property="experience" sortable="true" sortName="experience" title="Exp" />
.
.
</display:table>

In the controller (I'm using Spring MVC)

    public ModelAndView industryHandler(HttpServletRequest request,
        HttpServletResponse response) throws ServletRequestBindingException {
        int pageSize = getPageSize(); // retrieve from application context declaration
        DisplayTagHelper dth = new DisplayTagHelper();
        dth.setDefaultOrder("datePosted desc");
        dth.setOrderByObject("job"); //to match name used in JPAQL in DAO
        dth.setPageSize(pageSize);
        dth.setTableId("jobs"); //to match name in displaytag name property in  tag
        dth.process(request);

        Long id = ServletRequestUtils.getRequiredLongParameter(request, "id");

        request.setAttribute("size", getJobDao().findByIndustry(id).size());
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("industry", getIndustryDao().load(id).getName());

        List jobs = getJobDao()
                             .findByIndustry(dth.getStartPosition(), pageSize,
                dth.getOrderBy(), id);

        return new ModelAndView("jobs", "jobs", jobs);
    }

In the DAO

    public List findByIndustry(int startPosition, int maxResult,
        String orderBy, Long id) throws DataAccessException {
        Query q = em.createQuery(
                "SELECT DISTINCT job FROM Job job, IN (job.industries) AS i WHERE i.id = :id" +
                orderBy);
        q.setParameter("id", id);
        q.setFirstResult(startPosition);
        q.setMaxResults(maxResult);

        return q.getResultList();
    }

Currently the banner does not show correct values. e.g if u have 10 as the page size, it should show 1-10 for first page, 11-20 for second page. But it always shows 1-10 for all the pages. However, this will probably be fixed in next release.

2 comments:

  sushovan

Thursday, October 05, 2006 10:33:00 PM

Hi
I am using extenal paging in my display tags.when i am clicking the next button shown in the tag,its fetching data based on the startPosition value.But after that there is no previous butto to go back to the previous records..How to overcome it..Plz help
Thank You
Sushovan

  Iskandar Salim

Friday, October 06, 2006 12:02:00 PM

Hi,

You need to add the partialList attribute in your display:table tag and set it to "true"

e.g. <display:table partialList="true" ...... >

Hope that helps.