Re: In VTL, simply reverse a list with java.util.Collection static method
Nathan Bubna <[email protected]>
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CAFyaDjGHsLrx=4ZgXuU21hGrpX8vKh5BzxLyNV8N_XVu=qrvKA@mail.gmail.com> |
Now that is a good question. Have you tried:
<tool class="java.util.Collections"/> ??
I kind of doubt that's instantiable, but it might be worth a try. If
it's not, you might do a hack kind of like:
<tool class="com.hack.CollectionsTool"/>
public class CollectionsTool {
public void setVelocityContext(Context context) {
context.put("Collections", java.util.Collections.class);
}
}
I've never written a self-replacing tool like this, but if you
configure the VelocityViewServlet to let the "user" overwrite tools,
by giving
org.apache.velocity.tools.userCanOverwriteTools a value of true in
your init-params, it should work.
Or a similar tool that just has a:
public List reverse(List list) {
java.util.Collections.reverse(list);
return list;
}
method.
I wish i had a better answer. It would be cool to patch VelocityTools
to support:
<tool static="true" class="java.util.Collections"/>
But i haven't the time to do it. Perhaps you could open a feature
request in our JIRA instance.
On Mon, May 6, 2013 at 12:57 PM, Travis P <[email protected]> wrote:
> On Mon, May 6, 2013 at 2:42 PM, Nathan Bubna <[email protected]> wrote:
>
>>
>> http://velocity.apache.org/engine/devel/developer-guide.html#supportforstaticclasses
>
>
> Yes, I'd found that. I guess how to use that for adding a way to access
> java.util.Collection as $Collection
> within a VTL when using the VelocityViewServlet which supplies me a context
> makes it not obvious how to take that short documentation and translate it
> into my goal.