mvc:annotation-driven与annotation-config区别

本文介绍了Spring 3.0中引入的mvc:annotation-driven标签的功能,包括支持类型转换、数据格式化、验证、消息体解析等功能。同时,还解释了context:annotation-config的作用,并对比了二者之间的区别。

mvc:annotation-driven is a tag added in Spring 3.0 which does the following:

  • Configures the Spring 3 Type ConversionService (alternative to PropertyEditors)
  • Adds support for formatting Number fields with @NumberFormat
  • Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  • Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  • Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody)
  • Adds support for reading and writing JSON, if Jackson is o n the classpath (along the same lines as #5)

context:annotation-config Looks for annotations on beans in the same application context it is defined and declares support for all the general annotations like @Autowired, @Resource, @Required, @PostConstruct etc etc.

**

<mvc:annotation-driven /> declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declarative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.

个人理解就是,mvc:annotation-driven,是给spring-mvc增添了额外可用的注解,如@responseBody 等,并且显示声明了基础注解,如@RequestMapping 等(虽然这些注解是默认的)

<?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: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/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:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/> </bean> </property> <property name="modules"> <list> <bean class="com.fasterxml.jackson.datatype.jsr310.JavaTimeModule"/> </list> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="com.example.tingyu.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/static/**" location="/static/"/> <mvc:view-controller path="/showMainPage" view-name="main"/> <!-- <mvc:exclude-mapping path="/login"/> --> <!-- <mvc:exclude-mapping path="/admin/userLogin"/> --> <!-- <mvc:exclude-mapping path="/static/**"/>--> <mvc:view-controller path="/login" view-name="login"/> <mvc:view-controller path="/" view-name="login"/> </beans> 无法解析modules
最新发布
05-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值