how to render a datatable based on the list size in jsf using java EL?
Asked
Active
Viewed 1.6k times
3
-
"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 Answers
15
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}">
-
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