SpringMVC访问静态资源
鸣谢:http://blog.163.com/koko_qiang/blog/static/207213184201382091154584/
---------------------------------------------------------------------------------
如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg的访问也就被拦截了,所以很可能出现访问不到静态资源,报404的问题。如:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
以上配置的DispatcherServlet拦截了所有的请求,因此在springMVC配置静态资源的访问路径(dispatcher-servlet.xml),如:
<!-- 静态文件的访问 --> <mvc:resources location="/ExtJS4.2/" mapping="/ExtJS4.2/**"/> <mvc:resources location="/demo/image/" mapping="/demo/image/**"/> <mvc:resources location="/demo/js/" mapping="/demo/js/**"/> <mvc:resources location="/demo/config/" mapping="/demo/config/**"/>
那么在页面中写静态文件的路径就按以上映射(mapping)的地址。如:
<script type="text/javascript" src="/demo/js/localXHR.js"></script>
store: new Ext.data.TreeStore({ proxy: { type: 'ajax', url: '/demo/config/tree.txt'// 'demo/config/tree.txt'也可以。与resource中的dispatcher-servlet.xml静态资源映射地址一致。 }, root: { expand: true, text: '我是根' } }),
<img alt="思考者日记网|束洋洋个人博客" src="/demo/image/shuyangyang_01.jpg">