Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

Sunday, 27 January 2013

Check a collection size with JSTL

Leave a Comment

length( java.lang.Object) - Returns the number of items in a collection, or the number of characters in a string.
put this at the top of the page to allow the fn namespace
 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
and use like this in your jsp page
<p>The length of the companies collection is : ${fn:length(companies)}</p>
(from memory)
So to test with length of a collection:
<c:if test="${fn:length(companies) gt 0}">
   <p>It is greater than 0</p>
</c:if>
Read More...

How to iterate HashMap using JSTL forEach loop

Leave a Comment

Iterate a List
//Java
List<String> cityList = new ArrayList<String>();
cityList.add("Washington DC");
cityList.add("Delhi");
cityList.add("Berlin");
cityList.add("Paris");
cityList.add("Rome");
 
request.setAttribute("cityList", cityList);
 
 
//JSP
<c:forEach var="city" items="cityList">
    <b> ${city} </b>
</c:forEach>

Iterate a Hashmap
//Java
Map<String, String> countryCapitalList = new HashMap<String, String>();
countryCapitalList.put("United States", "Washington DC");
countryCapitalList.put("India", "Delhi");
countryCapitalList.put("Germany", "Berlin");
countryCapitalList.put("France", "Paris");
countryCapitalList.put("Italy", "Rome");
         
request.setAttribute("capitalList", countryCapitalList);
 
//JSP
<c:forEach var="country" items="${capitalList}">
    Country: ${country.key}  - Capital: ${country.value}
</c:forEach>
Read More...