Spring参考手册 mvc:annotation-driven

本文详细介绍了Spring MVC中注解驱动的功能,包括自动配置DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter,支持数据绑定、格式化、验证等特性,并列举了在不同场景下所需的技术依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to Controllers.
这个标签注册了Spring MVC分发请求到控制器所必须的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例

The tag configures those two beans with sensible defaults based on what is present in your classpath.
标签配置的这2个实例可以根据classpath中的内容默认提供以下功能:

The defaults are:
1. Support for Spring 3's Type ConversionService in addition to JavaBeans PropertyEditors during Data Binding.
A ConversionService instance produced by the org.springframework.format.support.FormattingConversionServiceFactoryBean is used by default.
This can be overriden by setting the conversion-service attribute.
支持spring3的javaBeans属性编辑器数据绑定时的类型转换服务。
类型转换服务实例默认为org.springframework.format.support.FormattingConversionServiceFactoryBean。
可以覆盖conversion-service属性来指定类型转换服务实例类。

2. Support for formatting Number fields using the @NumberFormat annotation
支持@NumberFormat 注解格式化数字类型字段。

3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation, if Joda Time 1.3 or higher is present on the classpath.
@DateTimeFormat注解格式化 Date, Calendar, Long和 Joda Time(如classpath下存在Joda Time 1.3或更高版本)字段

4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
The validation system can be explicitly configured by setting the validator attribute.
支持@Valid注解验证控制器数据,classpath中需JSR-303的**。
可以使用setting明确的配置

5. Support for reading and writing XML, if JAXB is present on the classpath.
支持读写xml,classpath中需JAXB 。

6. Support for reading and writing JSON, if Jackson is present on the classpath.
支持读写json,classpath中需Jackson 。

A typical usage is shown below:
下边是用法:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
</beans>

求上述1-6的使用例子。

当使用<mvc:annotation-driven>标签时,可能会出现以下错误: 1. 找不到org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 3.1或更高版本,则应该使用<mvc:annotation-driven>标签。如果您使用的是Spring 3.或更低版本,则应该使用<context:component-scan>标签。 2. 找不到org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 3.1或更高版本,则应该使用<mvc:annotation-driven>标签。如果您使用的是Spring 3.或更低版本,则应该使用<context:component-scan>标签。 3. 找不到org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter类 这个错误通常是由于Spring版本不兼容导致的。如果您使用的是Spring 5.或更高版本,则应该使用@EnableWebMvc注释。如果您使用的是Spring 4.或更低版本,则应该使用WebMvcConfigurerAdapter类。 4. 找不到org.springframework.web.servlet.DispatcherServlet类 这个错误通常是由于您没有正确配置DispatcherServlet导致的。请确保您已经正确配置了DispatcherServlet,并且在web.xml文件中正确地映射了它。 5. 找不到org.springframework.web.servlet.view.InternalResourceViewResolver类 这个错误通常是由于您没有正确配置InternalResourceViewResolver导致的。请确保您已经正确配置了InternalResourceViewResolver,并且在Spring配置文件中正确地定义了它。 总之,当使用<mvc:annotation-driven>标签时,如果出现错误,请检查您的Spring版本和配置是否正确。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值