Archive for November, 2007

Removing an item from a Map using JSP

Friday, November 30th, 2007

A common need for working with Maps in JSP is the ability to remove items from the Map.  With JSTL it is not so obvious how to do this, yet it turns out to be very easy by simply using an EL expression to call the remove(key) method on the map.  Here is an example…

 

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

<html>
<body>

<p>Creating hash map and populate it ..</p>

<jsp:useBean id="map" class="java.util.HashMap"/>
<c:set target="${map}" property="cat" value="brown"/>
<c:set target="${map}" property="dog" value="green"/>
<c:set target="${map}" property="rat" value="black"/>

<p>Remove dog and store its former value in a local variable …</p>

<c:set var="oldDogValue" value="${map.remove(’dog’)}"/>

</body>
</html>

Another great tip from Aaron Freeman at SendThisFile.com.

Handling multiple value parameters in JSP

Friday, November 9th, 2007

In JSP, you cannot simply use an expression like ${param.weekday} to retreive multiple values if the request has multiple values assigned like the form below…

<input name="weekday" value="Sunday" checked>
<input name="weekday" value="Monday" checked>

Instead, use an expression like ${fn:join(paramValues['weekday'], ‘,’)} to retreive a comma delimited list of values for the parameter.

Tip provided by Aaron Freeman from SendThisFile.com.