How to use pagination on portlet

Liferay provides its own way for pagination using SearchContainer class. But if you’re not familiar with this, and you prefer to use displaytag as your paging solution, you can do so since Liferay ext environment already included displaytag library on its package.

In this article I’ll explain step by step how to use displaytag on portlet, which is basically the same with the way you use it on your web application. For every paging action, we don’t want to bring the whole record into session variable, which will make our portal not scalable. Rather we just want to limit the record , say 10 records per query, and put the result into request attribute.

So here are the steps :

  1. On the java class, count the total record. Put the total into request attribute
  2. Still on the java class, get the page number from request parameter, which will determine the start and end of our query.
  3. Query the records using start and end which we calculate on step 2.
  4. Put the total record and the records into request attribute
  5. Add displaytag custom tag into the JSP

Let’s say that I have a Project entity, with properties projectId, title, startdate, duration. So I’ve created method on ProjectLocalServiceUtil to count total record and to query the records.

Now in my java class I do the query :


protected void doSearch(ActionForm form, ActionRequest req) throws SystemException {

  // ... get all query parameters here

  int total = ProjectLocalServiceUtil.countByAllCriteria(description, locationId, employmentTypeId, isHotjob );

  int recordperpage = 10;

  int start = 0;

  int end = 0;

 // get the page number

  String paramPageNumber = (new ParamEncoder("project").encodeParameterName(TableTagParameters.PARAMETER_PAGE));

            start = Integer.parseInt(req.getParameter(paramPageNumber));
            if(start == 0 || start ==1){
                start = 0;
            }else{
                start = (start-1)*recordperpage;
            }

    end = start+recordperpage;

List projects = ProjectLocalServiceUtil.findByAllCriteria(description, locationId, employmentTypeId, isHotjob,start,end);    

req. setAttribute("PROJECTS", projects);

req.setAttribute("TOTAL", new Integer(total) );

}

Now on the JSP part


<display:table name="requestScope.PROJECTS" id="project" partialList="true" requestURI="/ext/onlineproject/search?actionURL=true&windowState=maximized" size="TOTAL" pagesize="10">
<display:column property="title"  />
<display:column property="startdate" />
<display:column property="duration" />
</display:table>

Take a look on the JSP part (line 1)

requestURI attribute is URL of your search action.
id=”project” is the variable that contains Project entity instance for iteration inside the displaytag table. This value should be used to get the page number parameter inside the java class (See line 15 on java class. ).
If you change the value of id attribute, for example : id=”firm”, then to get the page number you should write like this:
String paramPageNumber = (new ParamEncoder(“firm”).encodeParameterName(TableTagParameters.PARAMETER_PAGE));

If you want a column to be clickable ( for example, you add hyperlink for editing or show detail of the record ) you can just make the jsp like this :

<display:table name="requestScope.PROJECTS" id="project" partialList="true" requestURI="/ext/onlineproject/search?actionURL=true&windowState=maximized" size="TOTAL" pagesize="10">
<display:column property="title" href="/ext/onlineproject/detail?actionURL=true&windowState=maximized" paramId="projectId" paramProperty="projectId" />
<display:column property="startdate" />
<display:column property="duration" />
</display:table>

By default, displaytag has its own localization mechanism. So how to enable displaytag to read our localization files, that are defined with Liferay’s Language-ext.*.properties. If you use struts framework to develop your portlet, you can create a file displaytag.properties, put this file on $SERVER_WEB_ROOT/ROOT/WEB-INF/classes, and create these two properties :

locale.resolver=org.displaytag.localization.I18nStrutsAdapter
locale.provider=org.displaytag.localization.I18nStrutsAdapter

Tested on Liferay 4.3.3

11 comments so far

  1. Khomeini on

    Hi,
    thx for sharing…
    does it work the same for LR 5.1.2?

  2. Khomeini on

    Hi,
    thx for sharing…
    does it work the same for LR 5.1.2?

    Khomeini

  3. Angelo on

    Hi.
    I’m using this environment:

    Display-tag 1.1
    Spring and Spring MVC 2.5.6
    AjaxTags 1.5

    I have built my own portlet; and i used your code to paginate result in my page. It seems tha display tag, altough query is pretty good executed and results are correctly taken, it doesn’t change then page number….can u help me?
    Thnxs
    Angelo

  4. Kalyan on

    Hi,

    i am using struts portlet .. and i am using displaytag for pagination.. allz is well but when i am clicking on the links i mean numbers 1,2,3 or next or prev.. the url is changing and it is becoming normal struts application. what is solution for this..

    Thanks in advance

    kalyan… please urgent

  5. guest on

    @kalyan, you can probably filter out display tag pagination request they are started with “-d”. for more details see here

  6. […] can look out these threads pagination-on-portlet and pagination Flag Please sign in to flag this as […]

  7. amarateidiko on

    can we use display tag in JSR 286

  8. teaching a toddler on

    Unquestionably consider that which you stated.
    Your favorite reason appeared to be on the web the easiest factor to take into accout of.
    I say to you, I definitely get annoyed whilst other people think about concerns that
    they just don’t know about. You controlled to hit the nail upon the top and also outlined out the entire thing without having side-effects , people could take a signal. Will probably be again to get more. Thank you

  9. Killing Nail Fungus on

    I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often.
    Did you hire out a designer to create your theme? Superb work!

  10. www.tv-essen-ost.de on

    Thank you for another magnificent post. Where else could anybody get that type of info in such a perfect approach of
    writing? I’ve a presentation next week, and I’m on the search
    for such info.

  11. Hi, this weekend is fastidious designed for me, since this occasion i am reading this impressive informative piece of writing here at my house.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: