Analysis of the failure reason of Spring MVC annotation automatic scanning _java

Source: Internet
Author: User

With regard to spring automatic scanning, in the control layer, the annotation configuration @controller, the project can start successfully, and no error. But in the page jump, did not carry out the corresponding interception, the entire interface can only be in the default interface, jump reported 404, because the landlord first attempt, in a large circle around, the initial confirmation is in the scan when the MVC controller, did not succeed, details see code

<!--open Controller annotation support-->
<context:component-scan base-package= "Com.cjw.test.controller" Use-default-filters= "false" >

However, when the page jumps, always report 404, because the controller scan does not distinguish the control layer from the rest of the note, the modified code is as follows:

<!--open Controller annotation support-->
<context:component-scan base-package= "Com.cjw.test.controller" Use-default-filters= "false" >
<context:include-filter type= "annotation"
expression= " Org.springframework.stereotype.Controller "/>

At the same time, in order to find the simplest configuration items, the landlord to remove the corresponding configuration, to observe the whole function of the start or jump to have any effect, in the removal

 
 

After discovering also cannot jump. Finally, the reasons for this are as follows:

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 request is processed by controller, and static resources are 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.

The Web.xml configuration is as follows, using static page jumps:

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</ Url-pattern>
</servlet-mapping>

The above is a small series to introduce the spring MVC annotation automatic scanning failure reason analysis, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.