及解決No mapping found for HTTP request with URI [/role/getRole] in DispatcherServlet with name 'springmvc-config'


1:什么時候使用<context:annotation-config>

當你使用@Autowired,@Required,@Resource,@PostConstruct,@PreDestroy等注解時會選擇使用,

但也可以手動配置Bean來使用這些注解,但是會使spring的配置文件比較笨拙

一般都會使用<context:annotation-config>,由spring提供,隱式的向容器注冊了AutowiredAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor這四個bean。

但是使用注解的時候,我們都會用<xontext:component-scan/>來將bean掃描進容器,此時,可以將<context:annotation-config/>省去

2:什么時候使用<mvc:annotation-drive/n>

當使用了@Controller時

而且是必須使用,如果不使用的話,org.Springframework.web.servlet.DispatcherServlet無法找到控制器並把請求分發到控制器

<mvc:annotation-driven/>注冊了DefaultAnnotationHandleMapping和AnnotationMethodHandleAdapter兩個bean(該兩個bean解決了@Controller注解的使用前提配置)

 

我之前是一個了@Controller注解而沒有使用<mvc:annotation-driven/>

報錯:

 No mapping found for HTTP request with URI [/role/ getRole] in DispatcherServlet with name 'springmvc-config'
解決:添加<mvc:annotation-driven/>且必須放在配置文件的第一行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM