Create a custom JSTL tag that returns an object instead of writing to the output stream
I've been searching for a way to create a custom JSTL tag that returns an object or map instead of just us开发者_StackOverflowing out.print
to dump the markup directly from the code.
Specifically, what I'm trying to do:
<c:set var="links">
<util:getLinks cmsComponent="[id of some xml structure]" xPath="[start point]" />
</c:set>
<!-- parse the obtained structure into a nice ul -->
I know I can:
- use my controller to do this and pass the computed object back in the view. Unfortunately the CMS we have to use makes this a lot uglier than it would be to do it in the view - getting the component id in the Java code is a pain. I know it sucks.
- make an EL function that returns the object and do:
<c:set var="links" value="${util:getLinks(componentId, xPath)}" />
I'm curious if I can do this using a custom tag since I couldn't find anything by googling.
Sure, the tag should simply put its result in the PageContext
. An attribute should specify the name under which it will be present. That's the way <c:url />
works, for example - you can specify var
and the result will be accessible by that name.
<util:getLinks cmsComponent=".." xPath=".." var="links" />
精彩评论