ssm项目静态资源访问问题
1 为什么访问不了静态资源
原因:1 没有设置静态文件目录。默认情况下,tomcat会将所有请求交给dispatchservlet处理。可以通过<mvc:resources location = "" mapping = "">设置。如
<mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/editor/" mapping="/editor/**"/>
PS:mvc:resource 理所当然导包。
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
2 路径写错。静态资源默认访问方式是相对路径,如果在文件在上一层记得加../,如果在路径前面加一个/,这表示根目录的意思,重新生成的url相当于从tomcat下的webapps目录开始。
3 静态资源的访问是在当前url上添加相对路径形成的新url访问,那么请求转发的url是不变的,所以直接使用相对路径会出错。解决方式为在jsp页面添加如下代码,每次访问静态资源都会当项目的根目录开始访问。
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <base href="<%=basePath%>" ></base>

浙公网安备 33010602011771号