解决方式如下:
首先,我在项目中使用了SPring框架,Web.xml中配置了拦截器(出现这个问题的很多人都描述,当删除web.xml中的拦截器,就能够加载出图片),同时在applicationContext.xml中配置各种Spring的配置。
实践中发现,如果web.xml中的拦截器配置了拦截"/",则在applicationcontext.xml中必须设置防止静态文件被拦截的代码;如果您web.xml中配置的拦截器不是拦截的"/",则不会出现webroot下的静态文件被拦截。
因此,假如您在web.xml中配置了“/”的拦截器:
<servlet>
<servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
那么,在applicationContext.xml中必须添加如下的配置:
<!-- 处理静态资源被拦截问题,如果web.xml中如果配置了/则此处需要配置,否则不需要 -->
<mvc:default-servlet-handler />