tags:

views:

19

answers:

1

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

+1  A: 

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}">
    
BalusC
I haven't tried the 2 and 3 yet. I am using the 1st one. Thanks BalusC.
gurupriyan.e
You're welcome, I'm using 2nd way. The 1st way is too much clutter in bean. This is a problem in the view, not in the bean/model. The 3rd way is too much tight coupling to specific implementations. I was using JSTL functions anyway, so the 2nd way is the best option.
BalusC