Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a pagination in Struts 2

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?

like image 795
edaklij Avatar asked Feb 14 '13 11:02

edaklij


2 Answers

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.

like image 116
Roman C Avatar answered Sep 23 '22 18:09

Roman C


You would need to more or less copy the way display does this.

  1. You need to build a page selector, for this you need to get the number of items in total - see Criteria.setProjection(Projections.rowCount).
  2. You need to provide links on your page that allow a user to send the selected page to the action.
  3. In you action you can then use the page number selected and the page size to determine the offset and the number of results, see Critera.setFirstResult and Criteria.setMaxResults
  4. You would then read these rows and create a collection of row beans.
  5. In your JSP iterate over the beans and build your table using <s:iterator\>.

It would seem that using displaytag may be easier, see its PaginatedList interface which would allow you to paginate in Hibernate and let displaytag do all the hard work.

like image 22
Boris the Spider Avatar answered Sep 22 '22 18:09

Boris the Spider