问题
My web-application uses Spring MVC and Freemarker. When it must first render a web page (using Freemarker 2.3.22) after starting up, it logs a warning message:
Custom EL functions won't be loaded because no ObjectWarpper was specified
(Yes, ObjectWarpper
; the error message has typo)
However, I think I do have an ObjectWrapper
specified in my configuration bean:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="object_wrapper">DefaultObjectWrapper(2.3.22)</prop>
</props>
</property>
</bean>
So what must I do to correctly set up an ObjectWrapper
and prevent this warning message being logged?
I have the same problem if I use a default
object_wrapper
.
回答1:
This problem also bothered me, I checked the source code of the org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer class and found the taglibFactory inited without ObjectWrapper being set.
So I extend the FreeMarkerConfigurer class and override the getTaglibFactory() method and replace the spring mvc config. Here is my code:
public class DefaultFreeMarkerConfigurer extends FreeMarkerConfigurer {
@Override
public TaglibFactory getTaglibFactory() {
TaglibFactory tagLibFactory = super.getTaglibFactory();
if (tagLibFactory.getObjectWrapper()==null) {
tagLibFactory.setObjectWrapper(super.getConfiguration().getObjectWrapper());
}
return tagLibFactory;
}
}
回答2:
The TagLibFactory
doesn't start out with an Objectwrapper
. This seems to work:
@Configuration
public class CustomFreemarkerConfiguration extends WebMvcConfigurerAdapter {
...
@Bean
@Autowired
public TaglibFactory taglibFactory(ServletContext servletContext) throws IOException, TemplateException {
FreeMarkerConfigurer freemarkerConfig = configFreeMarkerConfigurer(servletContext);
TaglibFactory taglibFactory = freemarkerConfig.getTaglibFactory();
taglibFactory.setObjectWrapper(freemarker.template.Configuration.getDefaultObjectWrapper(freemarker.template.Configuration.getVersion()));
return taglibFactory;
}
...
}
回答3:
That warning message means that no ObjectWrapper
was specified to the freemarker.ext.jsp.TaglibFactory
. Certainty Spring instantiates TaglibFactory
itself, but doesn't call TaglibFactory.setObjectWrapper
, which was only added in FreeMarker 2.3.22, together with the EL function support.
来源:https://stackoverflow.com/questions/33891693/custom-el-functions-wont-be-loaded-because-no-objectwarpper-was-specified