
Originally Posted by
NicolasPeeters
using a "fn:contains" function of JSTL. I realize this last trick might be a discussion point...
Under JSP 2, it's trivial to create an EL function to test for collection containment. I use referenceData to populate the model with all of the choices and a CustomCollectionEditor to bind the multiselect/checkbox group. Then, a simple
Code:
${coll:contains(refItems,curItem)}
is all it takes to determine if an item should be selected/checked at render time.
I was surprised at just how easy. Here's the code for the contains function:
Code:
public class CollectionUtil {
public static boolean contains(Collection coll, Object item) {
return coll.contains(item);
}
private CollectionUtil() {
super();
}
}
And the TLD:
Code:
<taglib
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>Java collection support tag library</description>
<tlib-version>0.1</tlib-version>
<short-name>coll</short-name>
<uri>http://www.orthogony.com/ns/taglib/collections</uri>
<function>
<description>
Check for the existence of an object in a collection.
</description>
<name>contains</name>
<function-class>
com.orthogony.web.jsp.taglib.CollectionUtil
</function-class>
<function-signature>
boolean contains(java.util.Collection, java.lang.Object)
</function-signature>
</function>
</taglib>
-dub