Reputation: 648
<bean id="beansWrapper" class="freemarker.ext.beans.BeansWrapper">
<property name="exposeFields" value="true" />
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freema rker.FreeMarkerConfigurer">
<property name="objectWrapper">
<ref local="beansWrapper" />
</property>
<property name="templateLoaderPath">
<value>/WEB-INF/views/</value>
</property>
</bean>
That doesn't work, I get an exception:
org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'objectWrapper' of bean class [org.springframework.web.servlet.view.freemarker.Fr eeMarkerConfigurer]: Bean property 'objectWrapper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
setObjectWrapper needs an instance of ObjectWrapper, I thought I was passing one in this configuration.
Based on advice from a reply (which I can't see when editing my question so sorry for lack of attribution), I tried this:
<bean id="beansWrapper" class="freemarker.ext.beans.BeansWrapper">
<property name="exposeFields" value="true" />
</bean>
<bean id="freemarkerAppConfig" class="freemarker.template.Configuration">
<property name="objectWrapper">
<ref local="beansWrapper"/>
</property>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="configuration">
<ref local="freemarkerAppConfig" />
</property>
<property name="templateLoaderPath">
<value>/WEB-INF/views/</value>
</property>
</bean>
But this still doesn't quite work. If I'm not mistaken, by calling setConfiguration, all the other properties of FreeMarkerConfigurer are over ridden by the supplied configuration.
Unfortunately, I can not see a way to easily set my template loading path as the configuration bean expects a Dir object, not a string.
Upvotes: 1
Views: 2922
Reputation: 648
More reading of Spring documentation showed me the way:
<bean id="beansWrapper" class="freemarker.ext.beans.BeansWrapper">
<property name="directoryForTemplateLoading" value="WEB-INF/views/" />
<property name="exposeFields" value="true" />
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerVariables">
<map>
<entry key="objectWrapper" value-ref="beansWrapper" />
</map>
</property>
<property name="templateLoaderPath">
<value>/WEB-INF/views/</value>
</property>
</bean>
In case anyone is wondering, this is done so that FreeMarker can access public properties on objects without needed getters and setters. Maybe not kosher in Java but this plays much nicer with Groovy now.
Upvotes: 3