简体   繁体   English

将Freemarker集成到Spring mvc时出错

[英]Error while integrating Freemarker to Spring mvc

I tried to integrate Freemarker templates to Spring mvc application. 我试图将Freemarker模板集成到Spring mvc应用程序中。 I tried it as shows in tutorials.Here is the code, I am using freemarker 2.3.15 我试过它作为教程中的节目。这是代码,我使用的是freemarker 2.3.15

servletcontext.xml 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. spring-context-support依赖项添加到项目中。

In pom.xml, 在pom.xml中,

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

Reference: ClassNotFoundException with Freemarker 参考: 带Freemarker的ClassNotFoundException

and

Spring Freemarker Configuration, Template Not Found Spring Freemarker配置,找不到模板

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. 我不知道FreeMarker或Spring库是否改变了什么。 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. 将此jar包含在另一个项目中时,该项目可能突然不再解析此依赖项。 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. 我正在研究的新项目实际上并不需要FreeMarker,但我需要让Spring解决依赖关系以便我的程序运行。

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. 这允许我的程序运行,因为Spring不需要自动装配这个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. 另外,我没有测试上面的代码,因为我再次不需要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.) 也许这会帮助别人(或者在我五年后再次寻找这个解决方案时帮助我。因为我已经解决了一次,但没有在自述文件中记录它。我必须在代码中找到我的评论。 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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