简体   繁体   中英

Error while integrating Freemarker to Spring mvc

I tried to integrate Freemarker templates to Spring mvc application. I tried it as shows in tutorials.Here is the code, I am using freemarker 2.3.15

servletcontext.xml

 <!-- freemarker config -->
    <beans:bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <beans:property name="templateLoaderPath" value="/WEB-INF/views/"/>
    </beans:bean>

    <!--
      View resolvers can also be configured with ResourceBundles or XML files. If you need
      different view resolving based on Locale, you have to use the resource bundle resolver.
    -->
    <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <beans:property name="cache" value="true"/>
      <beans:property name="prefix" value="/WEB-INF/views/"/>
      <beans:property name="suffix" value=".ftl"/>
    </beans:bean>

It gives to errors:

1. Build path is incomplete. Cannot find class file for org/springframework/ui/freemarker/
 FreeMarkerConfigurationFactory

2.No setter found for property 'templateLoaderPath' in class 
 'org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer' [config set: Warehouse/web-context]

Add the spring-context-support dependency to your project.

In pom.xml,

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>

Reference: ClassNotFoundException with Freemarker

and

Spring Freemarker Configuration, Template Not Found

I'm here years later, having encountered this problem. None of the solutions I found on the web work. I don't know if the FreeMarker or Spring libraries changed or what. I finally struck upon a solution that work, kind of.

I had the following code:

@Autowired
private FreeMarkerConfigurationFactoryBean freeMarkerConfigFactory;

When including this jar in another project, that project could suddenly no longer resolve this dependency. The new project I'm working on doesn't actually need FreeMarker, but I needed to get Spring to resolve the dependency so that my program would run.

I commented out the two lines above, and instead added the following:

private FreeMarkerConfigurationFactoryBean freeMarkerConfigFactory = new FreeMarkerConfigurationFactoryBean();

This allows my program to run because Spring doesn't need to autowire this bean.

Why it stopped working after years of working, I don't know. Also, I didn't test the above code because, again, I don't need FreeMarker. I just needed the autowiring to work.

Maybe this will help someone else (or help me when I come looking for this solution again in five years. Because I already solved this once, but didn't document it in a readme file. I had to find my comments in the code.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM