I have a List of beans, each of which has a property which itself is a List of email addresses.
<c:forEach items="${upcomingSchedule}" var="conf">
<div class='scheduled' title="${conf.subject}" id="scheduled<c:out value="${conf.id}"/>">
...
</div>
</c:forEach>
This renders one <div>
per bean in the List.
For the sublist, what I'd like to be able to do is to concatenate each of the entries in the list to form a single String, to be displayed as a part of the <div>
's title
attribute. Why? Because we are using a javascript library (mootools) to turn this <div>
into a floating tool tip, and the library turns the title
into the text of the tooltip.
So, if ${conf.subject}
was "Subject", ultimately I'd like the title
of the <div>
to be "Subject: blah@blah.com, blah2@blah2.com, etc.", containing all of the email addresses of the sub-list.
How can I do this using JSP EL? I'm trying to stay away from putting scriptlet blocks in the jsp file.
The "clean" way to do this would be to use a function. As the JSTL
join
function won't work on aCollection
, you can write your own without too much trouble, and reuse it all over the place instead of cut-and-pasting a large chunk of loop code.You need the function implementation, and a TLD to let your web application know where to find it. Put these together in a JAR and drop it into your WEB-INF/lib directory.
Here's an outline:
com/x/taglib/core/StringUtil.java
META-INF/x-c.tld:
While the TLD is a little verbose, knowing your way around one is a good skill for any developer working with JSP. And, since you've chosen a standard like JSP for presentation, there's a good chance you have tools that will help you out.
This approach has many advantages over the alternative of adding more methods to the underlying model. This function can be written once, and reused in any project. It works with a closed-source, third-party library. Different delimiters can be supported in different contexts, without polluting a model API with a new method for each.
Most importantly, it supports a separation of view and model-controller development roles. Tasks in these two areas are often performed by different people at different times. Maintaining a loose coupling between these layers minimizes complexity and maintenance costs. When even a trivial change like using a different delimiter in the presentation requires a programmer to modify a library, you have a very expensive and cumbersome system.
The
StringUtil
class is the same whether its exposed as a EL function or not. The only "extra" necessary is the TLD, which is trivial; a tool could easily generate it.Could you use this? Seems like it wants an array instead of a list..
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/join.fn.html
Just put the string byside the var from server, like this:
Too late!!!
Figured out a somewhat dirty way to do this:
I just use
<c:set>
repeatedly, referencing it's own value, to append/concatenate the strings.The way tag libraries are implemented seems to have moved on considerably since this answer was originally posted so I ended up making some drastic changes to get things working. My final result was:
Tag Library File:
JoinInts.java
To use it:
If your sublist is an ArrayList and you do this:
you obtain almost what you need.
The only difference is that the title will be: "Subject: [blah@blah.com, blah2@blah2.com, etc.]".
Maybe can be good enough for you.