Sunday, 3 May 2015

Displaying 'No Pages'

Using JSF's ui:fragment wrapper element is easy to display a 'No pages' message.  This is what I have came up with:

PageDAOBean.java
    [...]

    public int countAllPages(){
        Query q = em.createQuery("SELECT count(p) FROM Page p");
        Number result = (Number) q.getSingleResult();
        return result.intValue();
    }

    [...]


Pages.java (a JSF helper class)
    [...]

    public int countAll(){
        return pb.countAllPages();
    }

    [...]

pages.xhtml (JSF)
    [...]

    <ui:fragment rendered="#{pages.countAll() == 0}" >
        <h:outputText value="No pages yet. Why not add a page now?" />
    </ui:fragment>
                
    <ui:fragment rendered="#{pages.countAll() > 0}" >
        Doing now :-)
    </ui:fragment>

    [...]


I have been using this pattern on each of the list pages. If you know of a better way, feel free to let me know.

No comments:

Post a Comment