Help me please. My web-application uses Spring MVC and Freemarker 2.3.23. When I start my application, I have: "WARNING: Custom EL functions won't be loaded because no ObjectWrapper was specified" . I have tried to specify ObjectWrapper in several ways like these:
@Configuration
public class CustomFreemarkerConfiguration extends WebMvcConfigurerAdapter {
@Bean
@Autowired
public TaglibFactory taglibFactory(ServletContext servletContext) throws
IOException, TemplateException {
DefaultObjectWrapperBuilder owb = new
DefaultObjectWrapperBuilder(freemarker.template.
Configuration.VERSION_2_3_23);
owb.setForceLegacyNonListCollections(false);
owb.setDefaultDateType(TemplateDateModel.DATETIME);
DefaultObjectWrapper build = owb.build();
TaglibFactory taglibFactory = new TaglibFactory(servletContext);
taglibFactory.setObjectWrapper(build);
return taglibFactory;
@Configuration
public class CustomFreemarkerConfiguration extends WebMvcConfigurerAdapter {
@Bean
public DefaultObjectWrapper getDefaultObjectWrapper() {
DefaultObjectWrapperBuilder defaultObjectWrapperBuilder = new
DefaultObjectWrapperBuilder(freemarker.template.
Configuration.VERSION_2_3_23);
defaultObjectWrapperBuilder.setExposeFields(true);
DefaultObjectWrapper defaultObjectWrapper =
defaultObjectWrapperBuilder.build();
return defaultObjectWrapper;
@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;
but have not succeeded. Maybe someone has working example code to specify ObjectWrapper?
–
–
–
–
Look at the source code of FreeMarkerConfigurer
: it recreates the TaglibFactory
when it receives a ServletContext
, but doesn't set its ObjectWrapper
. So you need to override that method, and call taglibFactory.setObjectWrapper(configuration.getObjectWrapper())
. Note that I assume that configuration
is always final there; I haven't checked though. (I do not recommend using an other ObjectWrapper
, but the one that Configuration
contains. So you need the final Configuration
.)
Create an instance of your FreeMarkerConfigurer
subclass, as a @Bean
, and let's see if Spring MVC picks it up. (I don't know, I haven't tried.)
If it works, then this fix should be reported in the Spring Jira.
@EnableWebMvc
@ComponentScan(basePackages = "aquaplant")
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
public FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
public DefaultObjectWrapper defaultObjectWrapper;
@Bean
public ViewResolver getViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setOrder(1);
resolver.setSuffix(".ftl");
resolver.setPrefix("");
resolver.setExposeSpringMacroHelpers(true);
resolver.setExposeRequestAttributes(true);
resolver.setExposeSessionAttributes(true);
resolver.setRequestContextAttribute("request");
return resolver;
@Bean
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths("/", "/WEB-INF/views");
return configurer;
@Bean
public DefaultObjectWrapper defaultObjectWrapper(){
DefaultObjectWrapperBuilder owb=new DefaultObjectWrapperBuilder
(freemarker.template.Configuration.VERSION_2_3_23);
owb.setForceLegacyNonListCollections(false);
DefaultObjectWrapper objectWrapper = owb.build();
return objectWrapper;
@Bean
@Autowired
public TaglibFactory taglibFactory(ServletContext servletContext) throws
IOException, TemplateException {
freeMarkerConfigurer.setServletContext(servletContext);
TaglibFactory taglibFactory = freeMarkerConfigurer.getTaglibFactory();
taglibFactory.setObjectWrapper(defaultObjectWrapper);
return taglibFactory;
and I don't have any "WARNING", but on my registration page I can't get errorMessages. This cod don't work:
<div class="form-group">
<@spring.bind "userForm"/>
<#if spring.status.error>
<div class="alert alert-danger" role="alert">
<#list spring.status.errorMessages as error>
<li>${error}</li>
</#list>
<label for="username">Username</label>
<input type="text" class="form-control" id="username"
placeholder="Username" name="username">
Where did I make a mistake?
–
import freemarker.ext.jsp.TaglibFactory;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
public class DefaultFreeMarkerConfigurer extends FreeMarkerConfigurer{
@Override
public TaglibFactory getTaglibFactory() {
TaglibFactory tagLibFactory = super.getTaglibFactory();
if (tagLibFactory.getObjectWrapper()==null) {
tagLibFactory.setObjectWrapper(super.getConfiguration().getObjectWrapper());
return tagLibFactory;
It seems to be working.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2019.10.11.35165