Spring MVC中配置了加载静态支援后url请求报错404!!!

Spring MVC 中配置了加载静态支援后url请求报错404!!!

在Spring-servlet.xml 中配置防止静态资源被拦截

  

可以访问所有根目录下.html/.js/.css/.png/.gif 后缀文件
	<mvc:resources location="/" mapping="/**/*.html"/>  
	<mvc:resources location="/" mapping="/**/*.js"/>  
	<mvc:resources location="/" mapping="/**/*.css"/>  
	<mvc:resources location="/" mapping="/**/*.png"/>  
	<mvc:resources location="/" mapping="/**/*.gif"/>

配置完后访问url访问404 报错解决

	<mvc:annotation-driven/>  
	<mvc:resources location="/" mapping="/**/*.html"/>  
	<mvc:resources location="/" mapping="/**/*.js"/>  
	<mvc:resources location="/" mapping="/**/*.css"/>  
	<mvc:resources location="/" mapping="/**/*.png"/>  
	<mvc:resources location="/" mapping="/**/*.gif"/>
	<mvc:default-servlet-handler/> 

原因:

在web.xml 配置了DispatcherServlet后并未<mvc:annotation-driven />无法找到控制器且无法将请求其送入控制器

如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,所有当有请求时候都没有匹配的处理请求类,就都去<mvc:default-servlet-handler/>即default servlet处理了。添加上<mvc:annotation-driven/>后,相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。总之没有相应的Controller就会被default servlet处理就ok了。

<context:component-scan base-package="com.controller"/>配置项其实也包含
<context:annotation-config/> 
了自动注入上述的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。扫描包下
@Controller 自动注入bean

参考链接:

http://blog.csdn.net/jbgtwang/article/details/7359592

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

 
 


猜你喜欢

转载自blog.csdn.net/lin1094201572/article/details/79644033
今日推荐