annotation-driven

annotation-driven

常见错误:

在访问时出现500错误,找不到前端控制器;

nested exceptionis java.lang.IllegalArgumentException: No converter found for return value oftype: class java.util.ArrayList错误

出现这种错误的原因有两个:

一就是没有导入jackson的依赖包,

二就是因为自动注册的方式时支持一些数据绑定的

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

这样有的会不支持的,所以会有错误,

解决的方式就是将这两个注册换成自动注册,下面就是注册的代码:

 

先说明一些两个的区别:

<mvc:annotation-driven>

会自动注册RequestMappingHandlerMappingRequestMappingHandlerAdapter两个Bean,这是Spring MVC@Controller分发请求所必需的

但是它提供了数据绑定支持,@NumberFormatannotation支持,

@DateTimeFormat支持,

@Valid支持读写XML的支持(JAXB

读写JSON的支持(默认Jackson)等功能,所以功能更多。

 

 

下面就是使用该注解后的springmvc-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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<mvc:annotation-driven></mvc:annotation-driven>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>

<context:component-scan base-package="com.www.controller">
</context:component-scan>

</beans>

 版权声明:本文为博主原创文章,未经博主允许不得转载。

猜你喜欢

转载自blog.csdn.net/qq_42112846/article/details/80903646