tags:

views:

29

answers:

1

I have a Page class implementing java.util.Collection as well as other methods like hasPreviousPage, getTotalPages, etc. Freemarker wraps this class in a SimpleSequence, preventing me to access such methods.

i.e. when I write ${page.getTotalPages()} I got this error:

Expected hash. myPage evaluated instead to freemarker.template.SimpleSequence [...]

How can I tell Freemarker to expose those methods? (Ideally I would like to be able to access those custom methods without loosing "Collection" identity, so I can keep <#list page as item> )

Thanks for any suggestion/reference.

+1  A: 

Two thoughts, although neither one answers your question (which seems quite sensible).

1) I think instead of ${page.totalPages} you could use ${page.getTotalPages()}.

2) Consider whether it makes sense for Page to contain a collection object (so you'd use <#list page.items as item>) rather than implementing Collection, which is a smidgen unusual if you're not actually creating a new collection data structure.

Toper
1) Yes, I should have called getTotalPages(), fixing the question now.2) I agree, but that class is already used elsewhere, so I was considering to try to expose it as-is in the model before start a refactoring.BTW I just found the way FreeMarker can expose any method of the wrapped object. That is using the BeansWrapper wrapper instead of the DefaultObjectWrapper.
Xan