Link to home
Start Free TrialLog in
Avatar of Eternal_Student
Eternal_StudentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

JSTL back and next buttons

I have the following code that gets out information concerning a certain category and sorts them accordingly.

      
      <gc:clips var="clips" categoryId="<%= request.getParameter("categoryId") %>" sort="<%= request.getParameter("sort") %>" />
                  <c:forEach var="clip" items="${clips}">
                                   <c:out value="${clip.title}" />
                                   <c:out value="${clip.thumnail}" />
                        </c:forEach>

What I need to do is build into this back and next buttons for every 20 results.

What is the best way to do this ?

thanks for your time.
Avatar of bloodredsun
bloodredsun
Flag of Australia image

use varStatus and count to control an if statement

               <c:forEach var="clip" items="${clips}" varStatus="status">
                    <c:if test="${status.count > param.start || status.count <= param.end }">
                                   <c:out value="${clip.title}" />
                                   <c:out value="${clip.thumnail}" />
                    </c:if>
                </c:forEach>

then just set the values as request parameters, mypage.jsp?start=20&end=40 via the links assigned to the buttons
Avatar of Eternal_Student

ASKER

Hi bloodredsun, thanks for your help.

Would you be able to show me a full example please, I am very new to all of this!
I tried to put start=0&end=16 in the url string but it still returns all results.
Ok I changed it to this:

<c:if test="${status.count >= start && status.count < end}">

Which works now if I change the values in the URL I am just not sure how to calculate and display the back and next buttons.
Just subtract or add the paging value to the start and end request parameters in the link

<a href="thispage.jsp?start=<c:out value='${param.start - 20}'/>&end=<c:out value='${param.end - 20}'/>">Back</a>
<a href="thispage.jsp?start=<c:out value='${param.start + 20}'/>&end=<c:out value='${param.end + 20}'/>"> Forward</a>

You'll also want to add some code so that this so that the back button only shows when there is some results to go back to.
I will also need to do the same for the forward button.

I guess I need to store the actual amount of results somewhere ? how would I get this into a variable ?

i think im getting there, thanks blood.
I realise that:

<c:if test="${param.start > 1}">
  // display previous link
</c:if>

Will cover the previous link but im not sure how I would test ${param.end} against the total amount of results ?

How would this be done ?
I guess my question is .. how would I take the total amount of results and store them in a variable ?
It depends on what the variable clips is: a RowSet, an ArrayList etc. You should be able to work this out pretty easily.
Im not sure which it is and even if I did know im not sure how I would put that value into a variable. I know what I need to do but im not familiar with jsp or jstl so it is over my head at this point in time.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, you are the man.

<c:set var="total" value="${status.count}"/>

then:

<c:if test="${param.end < total}">


Sorry, all I needed was a hint or two [or was it three ?] but I am trying to grasp this the best I can looking at examples. If you know of any JSTL tutorials for absolute dummies please point me in the right direction.

thanks again.