I have a relatively simple problem but I can't find out the solution.
I have an array what includes numbers. I would like to list 10 elements/page. But if the actual page is the second I can't open the third. Only the previous link works.
NumbersBean
public class NumbersBean {
List<Integer> numbers;
int actPage;
int maxPage = 10;
HtmlDataTable dataTable;
String pagingDirection;
public NumbersBean() {
}
public List<Integer> getNumbers() {
numbers = new LinkedList<Integer>();
for (int i = 0; i < 100; i++) {
numbers.add(i);
}
int from = actPage * maxPage;
int to = from + maxPage;
if (to >= numbers.size()) {
to = numbers.size();
}
return numbers.subList(from, to);
}
public void setNumbers(List numbers) {
this.numbers = numbers;
}
protected void addMessage(String message) {
FacesMessage msg = new FacesMessage(message);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public int getActPage() {
return actPage;
}
protected int calculatePageCount() {
return (numbers.size() / maxPage) + ((numbers.size() % maxPage) != 0 ? 1 : 0);
}
public int getPageCount() {
return calculatePageCount();
}
public String getPagingDirection() {
return pagingDirection;
}
public void setPagingDirection(String pagingDirection) {
this.pagingDirection = pagingDirection;
}
public String page() {
if (pagingDirection.equals("prev") && actPage != 0) {
actPage--;
} else if (pagingDirection.equals("next") && actPage + 1 < calculatePageCount()) {
actPage++;
}
return null;
}
public HtmlDataTable getDataTable() {
return dataTable;
}
public void setDataTable(HtmlDataTable dataTable) {
this.dataTable = dataTable;
}
}
main.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Numbers</title>
</head>
<body>
<f:view>
<h:form>
<h:dataTable value="#{NumbersBean.numbers}" var="numbers"
styleClass="table" rowClasses="row1, row2"
binding="#{NumbersBean.dataTable}">
<h:column>
<h:outputText value="#{numbers}" />
</h:column>
</h:dataTable>
<h:commandLink value="< " action="#{NumbersBean.page}" styleClass="pad"
rendered="#{NumbersBean.actPage>0}">
<f:setPropertyActionListener value="prev" target="#{NumbersBean.pagingDirection}" />
</h:commandLink>
<h:commandLink value=" >" action="#{NumbersBean.page}" styleClass="pad"
rendered="#{NumbersBean.actPage+1<NumbersBean.pageCount}">
<f:setPropertyActionListener value="next" target="#{NumbersBean.pagingDirection}" />
</h:commandLink>
<h:messages />
</h:form>
</f:view>
</body>
</html>