Spring MVC <mvc: annotation-driven>, springmvc tutorial
Study <mvc: annotation-driven> In SpringMvc 3.2. If no other parameter is configured, It is roughly equivalent to the following configuration file (refer to from org. springframework. web. servlet. config. annotationDrivenBeanDefinitionParser)
<! -- Configure the media type mapped to the path extension --> <bean name = "pathExtensionContentNegotiationStrategy" class = "org. springframework. web. accept. pathExtensionContentNegotiationStrategy "> <constructor-arg> <props> <! -- If romePresent --> <prop key = "atom"> application/atom + xml </prop> <prop key = "rss"> application/rss + xml </prop> <! -- Endif --> <! -- If jackson2Present | jacksonPresent --> <prop key = "json"> application/json </prop> <! -- Endif --> <! -- If jaxb2Present --> <prop key = "xml"> application/xml </prop> <! -- Endif --> </props> </constructor-arg> </bean> <! -- Configure the ing media type policy --> <bean name = "mvcContentNegotiationManager" class = "org. springframework. web. accept. contentNegotiationManager "> <constructor-arg> <list> <ref bean =" pathExtensionContentNegotiationStrategy "/> </list> </constructor-arg> </bean> <! -- Configure the @ RequestMapping processor at the method level --> <bean name = "requestMappingHandlerMapping" class = "org. springframework. web. servlet. mvc. method. annotation. requestMappingHandlerMapping "> <property name =" order "value =" 0 "/> <property name =" contentNegotiationManager "ref =" mvcContentNegotiationManager "/> </bean> <! -- Configure the data conversion service. By default, the formatted data conversion service can be used to format the date and number --> <bean name = "conversionService" class = "org. springframework. format. support. defaultFormattingConversionService "> <constructor-arg index =" 0 "> <null> </constructor-arg> <constructor-arg index =" 1 "> <value> true </value> </constructor-arg> </bean> <bean name = "validator" class = "org. springframework. validation. beanvalidation. localValidatorFactoryBean "> </bean> <! -- Configure data binding and bind through the conversion service. If jsr303 is included, verification is performed. --> <bean name = "webBindingInitializer" class = "org. springframework. web. bind. support. configurableWebBindingInitializer "> <property name =" conversionService "ref =" conversionService "/> <! -- If jsr303Present --> <property name = "validator" ref = "validator"/> <! -- Endif --> </bean> <bean name = "byteArrayHttpMessageConverter" class = "org. springframework. http. converter. byteArrayHttpMessageConverter "> </bean> <bean name =" stringHttpMessageConverter "class =" org. springframework. http. converter. stringHttpMessageConverter "> <property name =" writeAcceptCharset "value =" false "/> </bean> <bean name =" resourceHttpMessageConverter "class =" org. springframework. http. converter. ResourceHttpMessageConverter "> </bean> <bean name =" sourceHttpMessageConverter "class =" org. springframework. http. converter. xml. sourceHttpMessageConverter "> </bean> <bean name =" allEncompassingFormHttpMessageConverter "class =" org. springframework. http. converter. support. allEncompassingFormHttpMessageConverter "> </bean> <bean name =" atomFeedHttpMessageConverter "class =" org. springframework. http. converter. feed. AtomFeedHttpMessageConverter "> </bean> <bean name =" rssChannelHttpMessageConverter "class =" org. springframework. http. converter. feed. rssChannelHttpMessageConverter "> </bean> <bean name =" jaxb2RootElementHttpMessageConverter "class =" org. springframework. http. converter. xml. jaxb2RootElementHttpMessageConverter "> </bean> <bean name =" mappingJackson2HttpMessageConverter "class =" org. springframework. http. convert Er. json. MappingJackson2HttpMessageConverter "> </bean> <bean name =" mappingJacksonHttpMessageConverter "class =" org. springframework. http. converter. json. MappingJacksonHttpMessageConverter "> </bean>! -- Configure @ RequestBody, @ ResponseBody annotation available converter --> <util: list id = "messageConverters" list-class = "org. springframework. beans. factory. support. managedList. managedList "> <ref bean =" Courier "/> <ref bean =" stringHttpMessageConverter "/> <ref bean =" resourceHttpMessageConverter "/> <ref bean =" sourceHttpMessageConverter "/> <ref bean = "allEncompassingFormHttpMessageConverter"/> <! -- If romePresent --> <ref bean = "atomFeedHttpMessageConverter"/> <ref bean = "rssChannelHttpMessageConverter"/> <! -- Endif --> <! -- If jaxb2Present --> <ref bean = "jaxb2RootElementHttpMessageConverter"/> <! -- Endif --> <! -- If jackson2Present --> <ref bean = "mappingJackson2HttpMessageConverter"/> <! -- Endif --> <! -- If jacksonPresent --> <ref bean = "mappingJacksonHttpMessageConverter"/> <! -- Endif --> </util: list> <! -- Adapt any type of Controller to Handler --> <bean name = "requestMappingHandlerAdapter" class = "org. springframework. web. servlet. mvc. method. annotation. requestMappingHandlerAdapter "> <property name =" contentNegotiationManager "ref =" canonical "/> <property name =" webBindingInitializer "ref =" webBindingInitializer "/> <property name =" messageConverters "ref = "messageConverters"/> </bean> <! -- This interceptor exposes the converter service to make spring: bind and spring: eval labels available --> <bean name = "csInterceptor" class = "org. springframework. web. servlet. handler. conversionServiceExposingInterceptor "> <constructor-arg index =" 0 "> <ref bean =" conversionService "/> </constructor-arg> </bean> <! -- Now all interceptors must set the response path ing --> <bean name = "mappedCsInterceptor" class = "org. springframework. web. servlet. handler. mappedInterceptor "> <constructor-arg index =" 0 "> <null> </constructor-arg> <constructor-arg index =" 1 "> <ref bean = "csInterceptor"/> </constructor-arg> </bean> <! -- Use the @ ExceptionHandler annotation to handle exceptions. The priority is 0 (highest) --> <bean name = "exceptionHandlerExceptionResolver" class = "org. springframework. web. servlet. mvc. method. annotation. predictionhandlerexceptionresolver "> <property name =" contentNegotiationManager "ref =" mvcContentNegotiationManager "/> <property name =" messageConverters "ref =" messageConverters "/> <property name =" order "value = ""0"/> </bean> <! -- If the thrown Exception class carries the @ ResponseStatus annotation, the response returns the Http status code of the Annotation with a priority of 1 --> <bean name = "responseStatusExceptionResolver" class = "org. springframework. web. servlet. mvc. annotation. responseStatusExceptionResolver "> <property name =" order "value =" 1 "/> </bean> <! -- SpringMvc internal Exception Handling --> <bean name = "defaultExceptionResolver" class = "org. springframework. web. servlet. mvc. support. defaultHandlerExceptionResolver "> <property name =" order "value =" 2 "/> </bean>
When spring mvc is added to <mvc: annotation-driven/>, an error is reported when the project is started.
<! -- Start the annotation function of Spring MVC to map requests to annotation POJO -->
<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter">
<Property name = "messageConverters">
<List>
<Bean class = "org. springframework. http. converter. StringHttpMessageConverter">
<Property name = "supportedMediaTypes">
<List>
<Value> text/html; charset = UTF-8 </value>
</List>
</Property>
</Bean>
</List>
</Property>
</Bean>
The configuration is incorrect.
Use the <mvc: annotation-driven/> annotation in Spring to report an error in the test class.
<Mvc: annotation-driven/> indicates the injection of spring mvc components into the container. In this case, the container must use the webApplicationContext sub-container of applicationContext.
Therefore, we recommend that you modify the code ApplicationContext ac = new ClassPa... as follows:
ApplicationContext ac = new FileSystemXmlApplicationContext ("..."); // Replace the ellipsis with the configuration path.
Ac. getBean ("beanId ");