how to render a datatable based on the list size in jsf using java EL?
+1
A:
Three ways:
Add an extra getter.
public int getSearchListSize() { return searchList.size(); }
with
<h:dataTable rendered="#{bean.searchListSize > 2}">
Use JSTL
fn:length()
function. Install JSTL if not done yet (just drop jstl-1.2.jar in/WEB-INF/lib
) and declarefn
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}">
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 inweb.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
2010-06-24 02:54:08
I haven't tried the 2 and 3 yet. I am using the 1st one. Thanks BalusC.
gurupriyan.e
2010-06-24 05:29:57
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
2010-06-24 13:11:20