添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
茫然的开心果  ·  服务容器 - Azure ...·  3 月前    · 
豪情万千的皮带  ·  Vue 3.X ...·  1 年前    · 
骑白马的乒乓球  ·  使用C++ ...·  1 年前    · 
高大的凉茶  ·  database - Delphi ...·  1 年前    · 

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?

Are you sure that Spring Boot even tries to inject those kind of beans into "itself"? I have seen that others define a org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer bean instead... so that's probably at least used by it. It's also the class that's broken in Spring; it should create and set up the TaglibFactory when both the TemplateContext and the Configuration is ready, and then it should call taglibFactory.setObjectWrapper(configuration.getObjectWrapper()). – ddekany Feb 23 '18 at 21:43 I tried to do it in my FreeMarkerConfigurer : @Bean public FreeMarkerConfigurer getFreeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPaths("/", "/WEB-INF/views"); TaglibFactory taglibFactory = new TaglibFactory(servletContext); taglibFactory.setObjectWrapper(defaultObjectWrapper); return configurer;} but it doesn't work. I still have the WARNING. Could you give me an example of the code, please. – Roman Feb 24 '18 at 12:20 OK, see my guess as answer... But the code in the above comment surely doesn't work as you do not pass the TaglibFactory you have created to anything, so you are just feeding the GC with it. – ddekany Feb 24 '18 at 17:11 BTW, FreeMarker 2.3.23 is old; upgrade it. It's not related to your question though. – ddekany Feb 24 '18 at 17:15

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?

I don't know offhand, I'm not familiar with Spring MVC forms. But this is a new question, so open a new one. And you will have to provide much more concrete details (I guess your Controller method is relevant, and if you get an error message, etc.). – ddekany Feb 25 '18 at 1:26
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