500初始化问题
来源:6-1 SpringMVC整合Freemarker
yonghao
2020-04-26 05:54:19
我发现我在applicationContext.xml加上这部分就会初始化不成功,我好郁闷...
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl"/> <!-- <property name="templateLoaderPath"><value>/WEB-INF/ftl/</value></property>--> <property name="freemarkerSettings"> <props> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean>
报错信息太多,我找了一部分
25-Apr-2020 17:50:07.647 警告 [http-nio-9000-exec-1] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer] for bean with name 'freemarkerConfig' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer] for bean with name 'freemarkerConfig' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory
25-Apr-2020 17:50:07.647 严重 [http-nio-9000-exec-1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer] for bean with name 'freemarkerConfig' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer] for bean with name 'freemarkerConfig' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory
其它applicationContext.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
context:component-scan标签作用
在Spring IOC初始化过程中,自动创建管理com.imooc.springmvc及子包中拥有一下注解的对象
@Repository DAO一般与数据发生直接交互的类
@Service 放在service上面的
@Controller 这是mvc的控制器
@Component 组件的意思,不好区分类型的时候,用这个
-->
<context:component-scan base-package="com.imooc.springmvc"></context:component-scan>
<!--启动Spring mvc的注解开发模式-->
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!--response.setContentType("text/html;charset=utf-8")-->
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--将图片/JS/CSS等静态资源排除在外,可提高执行效率=》不需熬dispatch拦截所有,这些静态资源直接放过-->
<mvc:default-servlet-handler/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.imooc.springmvc.Converter.MyDateConverter"/>
</set>
</property>
</bean>
<!--这个value是向客户端输出内容的时候设置为utf-8-->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=utf-8"/>
<property name="suffix" value=".ftl"/>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl"/>
<!-- <property name="templateLoaderPath"><value>/WEB-INF/ftl/</value></property>-->
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>
</beans>2回答
好帮手慕柯南
2020-04-26
同学你好!
首先检查一下你是否所有的jar都添加在项目中了

如果没有,首先添加一下


如果都添加了,检查一下jar是否和老师的版本一致,也可能是版本不兼容导致的
祝学习愉快~
好帮手慕柯南
2020-04-26
同学你好!
根据报错信息是找不到相应的类

你应该是漏掉了以下依赖

祝学习愉快~
相似问题