<mvc:annotation-driven />
2016-08-16 08:08
274 查看
自己看了官方文档,也到网上查了下,目前理解如下:
一开始我在写配置的时候,只写了
mvc-servlet.xml
web.xml
后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了
mvc-servlet.xml
web.xml
是什么原因造成这种区别的呢?为什么一开始没用
回答
最后的配置如果没有
总结:
要使用spring mvc中的@Controller注解,就必须要配置,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。
<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。
<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。
一开始我在写配置的时候,只写了
<context:component-scan/>并没有使用
<mvc:annotation-driven/>servlet拦截*.do,.do请求可以被正确捕捉和处理。代码如下
mvc-servlet.xml
<context:component-scan base-package="com"></context:component-scan>
web.xml
<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>
后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了
<mvc:annotation-driven/>之后,.do请求才又能被正确捕捉和处理。代码如下
mvc-servlet.xml
<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/>
web.xml
<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>/</url-pattern> </servlet-mapping>
是什么原因造成这种区别的呢?为什么一开始没用
<mvc:annotation-driven/>的时候可以,添加了默认servlet之后就不行了呢?
回答
最后的配置如果没有
<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,所有当有请求时候都没有匹配的处理请求类,就都去
<mvc:default-servlet-handler/>即default servlet处理了。添加上
<mvc:annotation-driven/>后,相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。总之没有相应的Controller就会被default servlet处理就ok了。
总结:
要使用spring mvc中的@Controller注解,就必须要配置,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。
相关文章推荐
- <mvc:annotation-driven />注解意义
- <tx :annotation-driven> <mvc: annotation-driven >注解理解
- <mvc:annotation-driven />到底帮我们做了啥
- <mvc:annotation-driven/>的作用,配置Springmvc中的适配器和映射器
- <mvc:annotation-driven/>,<context:annotation-config/>,<context:component-scan/>详解
- <mvc:annotation-driven/>与<mvc:default-servlet-handler/>之间的一个问题
- <context:annotation-config/>,<context:component-scan/>,<mvc:annotation-driven/>区分
- <mvc:annotation-driven />讲解
- <mvc:annotation-driven />注解意义
- <mvc:annotation-driven/>与<mvc:default-servlet-handler/>问题
- <mvc:annotation-driven />注解详解
- spring注解配置<mvc:annotation-driven />与<context:annotation-config />区别
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- <mvc:annotation-driven/>
- <mvc:annotation-driven/>注解意义
- SpringMVC——静态资源访问之<mvc:annotation-driven/>与<mvc:default-servlet-handler/>
- <mvc:annotation-driven>
- <context:component-scan>扫描@Component @Controller@Service注解的的类 <mvc:annotation-driven />启用注解
- <mvc:annotation-driven />