Using the @controller annotation Why do you want to configure

Source: Internet
Author: User
I read the official document, but also looked up the Internet, the current understanding is as follows:

<mvc:annotation-driven/> is equivalent to registering two beans for defaultannotationhandlermapping and Annotationmethodhandleradapter, Configure some messageconverter. That is to solve the use of the @controller annotation of the premise configuration.

<context:annotation-config/> scans packages to implement annotation-driven bean definitions while automatically injecting the beans into the container for use. This solves the injection and use of the bean of the class identified by @controller.

At first I wrote the <context:component-scan/&gt when I was writing the configuration, and did not use <mvc:annotation-driven/>,servlet to intercept *.do. A do request can be properly captured and processed. The code is as follows
Mvc-servlet.xml
Java code <context:component-scan base-package= "com" ></context:component-scan>

Xml
Java Code <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework .web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do< /url-pattern> </servlet-mapping>


Later, in order to resolve the problem of static resource access, the servlet was changed to intercept all requests, i.e./, and the default servlet was added, at which point the *.do request could not be captured by the controller, and the page error was 404. Until the <mvc:annotation-driven/> is added, the. Do request is correctly captured and processed. The code is as follows
Mvc-servlet.xml
Java code <context:component-scan base-package= "com" ></context:component-scan> <mvc:annotation-driven /> <mvc:resources mapping= "/styles/**" location= "/web-inf/resource/styles/"/> <mvc: Default-servlet-handler/>

Xml
Java Code <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework .web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</ur L-pattern> </servlet-mapping>

What is the cause of this difference? Why is it possible to start out with a <mvc:annotation-driven/>, adding a default servlet?

answer The final configuration if there is no <mvc:annotation-driven/>, then all controller may not be resolved , all when there is no matching processing request class, all go to < Mvc:default-servlet-handler/> is processed by the default servlet. When <mvc:annotation-driven/> is added, the corresponding do request is controller, and the static resource is processed by the default servlet because there is no corresponding controller. In short, no corresponding controller will be handled by the default servlet is OK.


------------------------------------------------


This tag registers the "Defaultannotationhandlermapping and Annotationmethodhandleradapter beans" are for required NG MVC to dispatch requests to controllers.
This tag registers the defaultannotationhandlermapping and Annotationmethodhandleradapter instances necessary for the Spring MVC distribution request to the Controller

The tag configures those two beans with sensible defaults based on what are present in your.
These 2 instances of the label configuration can provide the following functionality by default based on content in 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 is overriden by setting the Conversion-service attribute.
A type conversion service that supports Spring3 's JavaBeans property editor data binding.
The Type conversion service instance defaults to Org.springframework.format.support.FormattingConversionServiceFactoryBean.
You can override the Conversion-service property to specify a type conversion service instance class.

2. Support for formatting number fields using the @NumberFormat annotation
Supports @numberformat annotations to format numeric type fields.

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 annotations Format Date, Calendar, Long and Joda time (for example, Joda time 1.3 or later) fields under Classpath

4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider was present on the classpath.
The validation system can is explicitly configured by setting the validator attribute.
Support @valid Note Validation controller data, classpath in need of JSR-303.
You can use the setting explicit configuration

5. Support for reading and writing XML, if JAXB are present on the classpath.
JAXB is required to support read-write Xml,classpath.

6. Support for reading and writing JSON, if Jackson are present on the classpath.
Jackson is required to support reading and writing Json,classpath.

A typical usage is shown below:
Below is the usage:

<?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 is detected on CLASSPATH and enabled automatically-->
<mvc:annotation-driven/>
</beans>

Please use the above 1-6 examples.


Summarize:

To use the @controller annotation in spring MVC, you must configure <mvc:annotation-driven/> Otherwise org.springframework.web.servlet.DispatcherServlet cannot locate the controller and distribute the request to the controller.


Turn from: http://www.iteye.com/problems/66133

Http://www.noday.net/articles/2011/08/27/1314458126911.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.