We store HTML markup inside XML documents and unmarshall using JiBX. We also use Spring and when one of these objects is added to the model, we can access it in the JSP via EL.
${model.bookshelf.columnList[0].linkList[0].htmlMarkup}
Nothing groundbreaking. But what if we want to store EL expressions in the HTML markup? For example, what if we want to store the following link?
<a href="/${localePath}/important">Locale-specific link</a>
… and display it in a JSP where LocalePath is a request attribute.
Or even more interesting, what if we want to store the following?
The link ${link.href} is in column ${column.name}.
… and display it inside a nested JSP forEach…
<c:forEach var="column" items="${bookshelf.columnList}">
<c:forEach var="link" items="${column.linkList}">
${link.htmlMarkup}
</c:forEach>
</c:forEach>
These EL expressions in request attributes are never evaluated. Is there a way get them to evaluate? Something like an “eval” tag? Token replacement works in the first example, but not in the second and isn’t very robust.
If I understand it right: you are looking for a way to evaluate an EL expression at runtime that is stored inside another value provided by an EL expression – something like recursive EL evaluation.
As I could not find any existing tag for this, I quickly put together a proof-of-concept for such an EvalTag:
Related TLD:
Usage:
Note:
I’ve tested it using Tomcat 7.0.x / JSP 2.1, but as you can see in the source code, there is no special error handling etc., because it’s just some proof-of-concept.
In my tests it worked for session variables using
${sessionScope.attrName.prop1}and also for request parameters using${param.urlPar1}but as it uses the current JSP’s expression evaluator I suppose it should work for all other “normal” EL expressions, too.