spring mvc 详解

To enable MVC Java config add the annotation @EnableWebMvc to one of your @Configuration classes:

@Configuration
@EnableWebMvc
public class WebConfig {

}

To achieve the same in XML use the mvc:annotation-driven element in your DispatcherServlet context (or in your root context if you have no DispatcherServlet context defined):


 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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    

The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping@ExceptionHandler, and others.

It also enables the following:

  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 DateCalendarLong, 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:

    1. ByteArrayHttpMessageConverter converts byte arrays.
    2. StringHttpMessageConverter converts strings.
    3. ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
    4. SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
    5. FormHttpMessageConverter converts form data to/from a MultiValueMap.
    6. Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present and Jackson 2 XML extension is not present on the classpath.
    7. MappingJackson2HttpMessageConverter converts to/from JSON — added if Jackson 2 is present on the classpath.
    8. MappingJackson2XmlHttpMessageConverter converts to/from XML — added if Jackson 2 XML extension is present on the classpath.
    9. AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
    10. RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

See Section 22.16.12, “Message Converters” for more information about how to customize these default converters.

Jackson JSON and XML converters are created using ObjectMapper instances created by Jackson2ObjectMapperBuilder in order to provide a better default configuration.

This builder customizes Jackson’s default properties with the following ones:

  1. DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is disabled.
  2. MapperFeature.DEFAULT_VIEW_INCLUSION is disabled.

It also automatically registers the following well-known modules if they are detected on the classpath:

  1. jackson-datatype-jdk7: support for Java 7 types like java.nio.file.Path.
  2. jackson-datatype-joda: support for Joda-Time types.
  3. jackson-datatype-jsr310: support for Java 8 Date & Time API types.
  4. jackson-datatype-jdk8: support for other Java 8 types like Optional.

你可能感兴趣的:(spring,mvc)