springmvc配置之mvc:annotation-driven

为了简化springmvc配置,spring同时引入了mvc namespace, 配置了

<mvc:annotation-driven/>

spring会默认注册a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver 三个bean, 来支持使用注解(@RequestMapping、@ExceptionHandler、@Controller等)修饰的controller处理请求。同时还有以下一些特性:

1、 Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.
2、 Support for formatting Number fields using the @NumberFormat annotation through the ConversionService.
3、 Support for formatting Date, Calendar, Long, and Joda-Time fields using the @DateTimeFormat annotation.
4、 Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
5、 HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:

     a.   ByteArrayHttpMessageConverter converts byte arrays.
     b.  StringHttpMessageConverter converts strings.
     c.  ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
     d.   SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
     e.  FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
     f.  Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present and Jackson 2 XML extension is not present on the classpath.
     g.  MappingJackson2HttpMessageConverter converts to/from JSON — added if Jackson 2 is present on the classpath.
     h.  MappingJackson2XmlHttpMessageConverter converts to/from XML — added if Jackson 2 XML extension is present on the classpath.
     i.  AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
     j.  RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

配置示例:

 <bean name="fastJson" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        <property name="features">
            <list>
                <value>AllowSingleQuotes</value>
            </list>
        </property>
        <property name="serializerFeatures">
            <list>
                <value>QuoteFieldNames</value>
                <value>WriteEnumUsingToString</value>
                <value>WriteEnumUsingName</value>
                <value>WriteNullNumberAsZero</value>
                <value>SkipTransientField</value>
                <value>WriteDateUseDateFormat</value>
            </list>
        </property>
    </bean>

    <!--默认的mvc注解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                        <value>text/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="fastJsonConfig" ref="fastJson"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

引用:
1、 springdoc
2、what‘s annotation-driven do

原文地址:https://www.cnblogs.com/graph/p/8998697.html

时间: 2024-10-13 15:23:55

springmvc配置之mvc:annotation-driven的相关文章

springmvc 配置详解

今天用springmvc搭了一个框架,将步骤以及配置记录下来. jar包网盘地址为:http://yun.baidu.com/share/link?shareid=1109438387&uk=2836507213 项目网盘地址为: http://yun.baidu.com/share/link?shareid=3913316871&uk=2836507213 1.需要导入网盘所有的jar包 2.修改web.xml文件 (加入springmvc的配置) <?xml version=&q

SpringMVC配置流程

1.配置web.xml1.1.项目工程中的web.xml配置一个servlet,该servlet配置项指定的servlet class是DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) 设置初始化载入spring-mvc.xml1.2.对应的<servlet-mapping>配置mapping的url(就是拦截哪些url) <!-- Spring MVC servlet --> <serv

Spring3.X 配置----Spring MVC 配置

导论: 什么是Spring MVC? Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架.通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如  JavaServer Pages(JSP)技术.Velocity.

springMVC配置详解(转)

现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

springMVC 配置和使用

springMVC相对于Struts2学习难度较为简单,并且更加灵活轻便. 第一步:导入jar包 spring.jar.spring-webmvc.jar.commons-logging.jar.spring-aop.jar.spring-beans.jar.spring-core.jar.spring-context.jar 第二步:配置web.xml文件 <!--configure the setting of springmvcDispatcherServlet and configure

SpringMVC配置及使用

SpringMVC基本配置 SpringMVC是基本请求响应模式的框架. 在项目中集成SpringMVC框架首先需要导入相关的jar包,所需包具体如下: commons-dbcp.jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-logging-1.1.1.jar jackson-annotations-2.2.3.jar jackson-core-2.2.3.jar jackson-databind-2.2.3.jar

SpringMVC 配置多视图解析器(velocity,jsp)

1.自定义视图解析器 package com.zhaochao.controller; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework

SpringMVC配置XML

核心架构的具体流程步骤如下: 1. 首先用户发送请求——>DispatcherServlet,前端控制器收到请求后自己不进行处理,而是委托给其他的解析器进行处理,作为统一访问点,进行全局的流程控制: 2. DispatcherServlet——>HandlerMapping, HandlerMapping将会把请求映射为HandlerExecutionChain对象(包含一个Handler处理器(页面控制器)对象.多个HandlerInterceptor拦截器)对象,通过这种策略模式,很容易添

springmvc 配置过程及详解

加入jar包 在web.xml中 添加spring监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 添加spring容器(父容器)配置文件: <context-param> <param-name>contextConfigLocation</param