測試的時候發現:如果直接訪問web項目的html等靜態資源,不能訪問
原因如下: 當web.xml中url-pattern配置為"/"時,會導致系統中的靜態資源被攔截
如何解決:
1)修改url-pattern
2)進行靜態資源映射
3)開放tomcat的defaultServlet
下面是測試步驟
配置web.xml
<!-- springmvc核心 --> <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:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
配置springmvc.xml
有兩種方式 :
1) 映射靜態資源
<!-- 解決靜態資源問題 mapping:映射 location:本地資源路徑,注意必須是webapp根目錄下的路徑。兩個*,它表示映射resources/下所有的URL,包括子路徑(即接多個/) --> <mvc:resources location="/upload/**" mapping="/upload/"/> <mvc:resources location="/js/*" mapping="/js/*"/> <mvc:resources location="/css/**" mapping="/css/**"/>
或者
2)開放tomcat的defaultServlet,只需要添加一行配置,是不是很方便
<mvc:default-servlet-handler/>
參考:http://www.cnblogs.com/yank/p/4477204.html