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. That will hide the methods not part of java.util.Collection.
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 both the methods to iterate over the Collection, so that code such <#list page as item> will keep on working, and the custom ones?
Thanks for any suggestion/reference.
If you can’t or shouldn’t change that class… you can extend the DefaultObjectWrapper or BeansWrapper so that it treats some of the classes specially. DefaultObjectWrapper itself is an example of doing that (it wraps DOM objects and Jython objects specially). You could warp the object with BeanModel, but add an extra sub-variable called “items” which returns the same object but wrapped into SimpleSeqence. Then you just configure FreeMarker to use your custom ObjectWrapper globally (Configuration.setObjectWrapper) and you don’t have to touch the application code anywhere.