我正在尝试将 Freemarker与Spring MVC 集成,但是当我在Tomcat上运行我的应用程序时,我收到以下异常:
org.springframework.context.ApplicationContextException:必须在此Web应用程序上下文中定义单个FreeMarkerConfig bean(可以继承):FreeMarkerConfigurer是常用的实现。这个bean可以任意命名。嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[org.springframework.web.servlet.view.freemarker.FreeMarkerConfig]类型的限定bean org.springframework.web.servlet.view.freemarker.FreeMarkerView.autodetectConfiguration(FreeMarkerView.java:177) org.springframework.web.servlet.view.freemarker.FreeMarkerView.initServletContext(FreeMarkerView.java:149)
。 。
这很奇怪,因为我在我的应用程序上下文中定义了一个FreeMarkerConfig bean。这是我的web.xml和mvc-config.xml文件:
的web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Practic</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
MVC-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="pl.zimny.spring" />
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".ftl"/>
</bean>
</beans>
有谁有想法解决这个问题?