3

how to render a datatable based on the list size in jsf using java EL?

gekrish
  • 2,201
  • 11
  • 29
  • 46
  • "Based on list size"? Can you elaborate that? The datatable namely does that by default automatically. See also [Using datatables](http://balusc.blogspot.com/2006/06/using-datatables.html). – BalusC Jun 24 '10 at 00:06
  • like how do i use #{someBean.searchLists.size > 2 } , assuming the searchResults is of type List in someBean. I am adding few records voluntarily at the server side , so I wanted to check the size before i display search results. Right now, I am copying the size to a variable in bean and using it as #{someBean.searchListSize > 1} – gekrish Jun 24 '10 at 00:26

1 Answers1

15

Three ways:

  1. Add an extra getter.

    public int getSearchListSize() {
        return searchList.size();
    }
    

    with

    <h:dataTable rendered="#{bean.searchListSize > 2}">
    
  2. Use JSTL fn:length() function. Install JSTL if not done yet (just drop jstl-1.2.jar in /WEB-INF/lib) and declare fn taglib in top of JSP as follows:

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    

    and use it as follows:

    <h:dataTable rendered="#{fn:length(bean.searchList) > 2}">
    
  3. Use JBoss EL ("enhanced EL") as JSF EL implementation instead. It's backwards compatible with standard JSF EL implementation. Drop jboss-el.jar in /WEB-INF/lib and declare the following in web.xml, assuming you're using Mojarra JSF implementation:

    <context-param>     
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>   
    </context-param>
    

    This way you can access non-getter methods directly:

    <h:dataTable rendered="#{bean.searchList.size() > 2}">
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I haven't tried the 2 and 3 yet. I am using the 1st one. Thanks BalusC. – gekrish Jun 24 '10 at 05:29
  • Related https://stackoverflow.com/questions/7120526/how-to-display-value-of-listsize-in-jsf-el. (Summary, the third technique should work on most modern servers now, without adding a new JAR.) – DavidS Jul 20 '15 at 20:25