How to do pagination in Freemarker

How to do pagination in freemarker

list.ftl

<html>
<body>
<br/><br/><br/>
<center>
<hr/>
<br/>
<table cellspacing="0" cellpadding="0" border="1" bgcolor="pink">
<tr>
<th>Displaying List
${firstRecord}
to ${lastRecord}
</th>
</tr>

<#list subRoles as x>
<tr>
<td align="center">
<b>${x}</b>
</td>
</tr>
</#list>
</table>
<@ww.url id="idgonext" action="ListDisplaynext" />
<@ww.url id="idgoprevious" action="ListDisplayprevious" />

<br/>
<#if lastRecord+1<roles?size>
<@ww.form name="nextPageForm" action="ListDisplaynext" >
<@ww.submit value="Next Page"/>
</@ww.form>
</#if>

<#if (firstRecord>0)>
<@ww.form name="previousPageForm" action="ListDisplayprevious" >
<@ww.submit value="Previous Page"/>
</@ww.form>
</#if>

<hr/>
</center>
</body>
</html>

index.jsp

<br/><br/>
<hr/>
<center>

<a href="ListDisplaynext.action">See List</a>

</center>
<hr/>

ListDisplayAction.java

package com.test;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.context.scope.RequestContextListener;
import org.springframework.web.filter.RequestContextFilter;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

public class ListDisplayAction extends ActionSupport {

private List roles;
private List subRoles;
private int maxPerPage = 5;
private int pageNumber=-1;
private int lastRecord;
private int firstRecord;

public String goprevious(){
pageNumber--;
System.out.println("come to this previous --------"+pageNumber);
subRoles=new ArrayList();
firstRecord = pageNumber*5;
for(int j=pageNumber*5;j<((pageNumber+1)*5);j++)
{

subRoles.add(roles.get(j));
lastRecord=j;
}

return "ListDisplayprevious";
}
public String gonext(){

pageNumber++;
System.out.println("come to this next--------"+pageNumber);
subRoles=new ArrayList();
firstRecord = pageNumber*5;
for(int j=pageNumber*5;j<((pageNumber+1)*5);j++)
{
if(j>=roles.size())
{
lastRecord=j-1;
break;
}
subRoles.add(roles.get(j));
lastRecord=j;
}

return "ListDisplaynext";
}

public ListDisplayAction() {

System.out.println("come to constructorrrr");
roles = new ArrayList();
subRoles=new ArrayList();

for (int i=0;i<=33;i++)
{
roles.add(i);
}
for(int j=0;j<5;j++)
{
subRoles.add(roles.get(j));
}

}

public List getRoles() {
return roles;
}

public int getMaxPerPage() {
return maxPerPage;
}

public void setMaxPerPage(int maxUsers) {
this.maxPerPage = maxUsers;
}

public int getPageNumber() {
return pageNumber;
}

public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public List getSubRoles() {
return subRoles;
}
public void setSubRoles(List subRoles) {
this.subRoles = subRoles;
}
public void setRoles(List roles) {
this.roles = roles;
}
public int getFirstRecord() {
return firstRecord;
}
public int getLastRecord() {
return lastRecord;
}

}

xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.1.dtd">
<xwork>

<include file="webwork-default.xml"/>

<package name="default" extends="webwork-default">

<action name="ListDisplayprevious" class="ListTest" method="goprevious">
<result name="ListDisplayprevious" type="freemarker">/list.ftl</result>
</action>
<action name="ListDisplaynext" class="ListTest" method="gonext">
<result name="ListDisplaynext" type="freemarker">/list.ftl</result>
</action>
</package>
</xwork>

No comments: