This may have been covered somewhere but I'm having trouble forming the question for search engine and no goods leads thus far.
I'm working on a page that acts as entity view. Lots of results come from database and only a handful are displayed at a time. So you can imagine that I want to build a list of links that take user to another page of entities. This is all my code - no PrimeFaces or any other front-end nifty pagination solutions. At least for now.
To the code:
@Named
@SessionScoped
public class ArticleIndexBean {
List<Article> articleList=new ArrayList<>();
List<Article> articleSubList=new ArrayList<>();
@PostConstruct
public void loadScreenSupport() {
search();
toEntityPage(1);
}
protected void search() {
// this method sets articleList which is the full list fetched from the database
}
public void toEntityPage(int pageNumber) {
// this method sets articleSubList which is a subset of articleList
}
Each page link needs to call toEntiyPage(n)
. I am aware of commandLink but I want to avoid a POST request. Also, the bean is currently session scoped and I will try to make it conversation scoped later. It will certainly NOT be request scoped, as I don't want to do a full db search each time a user wants to jump to another page. So @PostConstruct won't help, either.
So with a menu like this: 1 * 2 * 3 * 4 * 5
how do I code an outputLink or any other type of link that will call my ArticleIndexBean.toEntityPage(int)
via a GET request?
Solution
Based on input from Laurent, I added a currentEntityPageNumber
property and a toCurrentEntityPage()
method to my bean. The toCurrentEntityPage()
simply calls toEntityPage(getCurrentEntityPageNumber())
.
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<f:metadata>
<f:viewParam name="pn" value="#{articleIndexBean.currentEntityPageNumber}" />
<f:event type="preRenderView" listener="#{articleIndexBean.toCurrentEntityPage()}" />
</f:metadata>
<c:forEach var="pageNumber" begin="1" end="${articleIndexBean.getEntityPageCount()}">
<h:outputLink value="ar_index.xhtml">
<h:outputText value="${pageNumber}" />
<f:param name="pn" value="${pageNumber}" />
</h:outputLink>
</c:forEach>
It would certainly be better if we could call toEntityPage(pageNumber)
directly but this works fine.