How to implement a pagination in Struts 2

edaklij picture edaklij · Feb 14, 2013 · Viewed 10.3k times · Source

How can I implement paging in Struts 2 for the client side display and Hibernate as the persistence layer.

Here is the code that I have done so far:

<display:table id="students" name="students" pagesize="2"
    export="false" requestURI="/student">
    <display:column property="studentRoll" title="Roll"
        paramId="studentRoll" sortable="true" />
    <display:column property="studentName" title="Name" sortable="true" />
    <display:column property="studentCourse" title="Course"
        sortable="true" />
    <display:setProperty name="paging.banner.placement" value="bottom" />
</display:table>

Is there any way to implement this without display tag?

Answer

Roman C picture Roman C · Feb 14, 2013

There's a way to implement it and I will give you some recommendations. May be it's not that you are looking for but with the display tag I'm really doubt that you can implement something don't care to say that word. The display tag is a library created to simplify representation of tabular data on the web and already old enough to use it with the modern technologies like Ajax and component based scripting libraries like jQuery, etc. Even I've seen the display tag ajaxified but I see this is nothing more just adding the complexity to the problematic architecture. Needed to add here the nature of the display tag library it is property based. And it was created with the purpose to simplify usage or the development process that time. Nowadays people doesn't limit themselves with the custom and ugly solutions.

Now looking at the code if I can say that is a code, the tag represents the table with rows and columns given the possibility to customize them with labels, headers, other HTML attributes, even with CSS but this is out of this question.

The attribute name represent a variable with the data structure representing a tabular data. It could be a list of lists or a set of objects doesn't matter. What's the matter it could be iterable and have rows and columns.

The attribute pagesize points to the number of records per page.

The given URI even doesn't remember what it is to feed the data from the server or just for escape.

And near the end of the tag is the banner placement, set with the property. So you even doesn't have to implement any code with it just setting properties for customization.

Now, to represent the table with data in Struts2 there's an

<s:iterator var="row" begin="0" value="#request.navigator.list">

tag, the list is students or something else doesn't matter. The navigator is the object similar to the banner in display tag, but not a component here. The component navigator with the buttons I will describe later.

Inside the iterator you may have any content representing the row. But this out of the question.

The navigator as the component could be given different ways simply for example

<s:property value="#request.navigator.bar" escapeHtml="false"/>  

Then the data in the list are fetched from the database via SQL or HQL or Criteria API doesn't matter. This is done in two steps. First checked the total number of records wishing to return. It gives the possibility to move at the end and not exceed the pagesize. Then return limited number of rows needed to represent on the page. These all done in the action that displays the table. The action to work needs two parameters the first row and pagesize. This is also needed to correct the navigator if the total number of records changed.

That's what I wanted to answer about how to implement the page navigation logic. I tried to not to write many, many code as for it out of question.