Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...