Springmvc3+Spring3+Freemarker整合异常

xiaoxiao2021-02-27  291

Springmvc3+Spring3+Freemarker整合异常

提示缺少ui等类,

 

主要异常信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean 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 ServletContext resource [/WEB-INF/spring-servlet.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 ServletContext resource [/WEB-INF/spring-servlet.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory  

  Error loading class [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer] for bean with name 'freeMarkerConfig' defined in class path resource [springmvc-servlet.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfigurationFactory

解决方法:

增加Jar包: org.springframework.context.support-3.1.1.RELEASE.jar

是少jar包的原因。ui.freemarker等几个类是在spring-context-support.jar包中的,同时在spring中集成freemarker是需要导入freemarder.jar包的

起用JSON转换功能

启动spring MVC的注解功能,完成请求和注解POJO的映射

<bean         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">         <property name="messageConverters">             <util:list id="beanList">                 <ref bean="mappingJacksonHttpMessageConverter" />             </util:list>         </property>     </bean>     <bean id="mappingJacksonHttpMessageConverter"

        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

MappingJacksonHttpMessageConverter能够将POJO对象自动转换为JSON对象

MappingJacksonHttpMessageConverter能够将POJO对象自动转换为JSON对象

@ResponseBody的作用是把返回值直接写到HTTP response body里

 java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

是因为Spring高版本中使用的将转换Json的类MappingJackson2HttpMessageConverter使用的是jackson2.x的版本

Java code ? 1 2 <!-- 用于将对象转化为JSON --> <bean id= "jsonConverter"  class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" ></bean> 需要导入依赖 Java code ? 1 2 3 4 5 6 <!--jackson-->          <dependency>              <groupId>com.fasterxml.jackson.core</groupId>              <artifactId>jackson-databind</artifactId>              <version> 2.8 . 7 </version>          </dependency>

转载请注明原文地址: https://www.6miu.com/read-3640.html

最新回复(0)