springMVC 請求404錯誤或靜態資源沒法訪問問題:springMVC 請求404錯誤或靜態資源沒法訪問。 在web.xml中咱們通常這樣配置:html
spring3web
org.springframework.web.servlet.DispatcherServletspring
1app
spring3jsp
/網站
使用url
/spa
能夠實現rest風格,可是會攔截了全部的請求致使靜態資源沒法訪問(解決方法後面說), 固然,若是你使用*.do、*.xhtml等就不會出現靜態資源沒法訪問問題。 靜態資源沒法訪問解決方案: 1,在spring3-servlet.xml中添加一下配置:rest
使用元素,把mapping的URI註冊到SimpleUrlHandlerMapping的urlMap中, key爲mapping的URI pattern值,而value爲ResourceHttpRequestHandler, 這樣就巧妙的把對靜態資源的訪問由HandlerMapping轉到ResourceHttpRequestHandler處理並返回,因此就支持classpath目錄,jar包內靜態資源的訪問. 另外須要注意的一點是,不要對SimpleUrlHandlerMapping設置defaultHandler.由於對static uri的defaultHandler就是ResourceHttpRequestHandler, 不然沒法處理static resources request. 若是加上以上配置仍是不成功,檢查一下是否是沒有配置:2,在spring3-servlet.xml中添加一下配置:會把"/**" url,註冊到SimpleUrlHandlerMapping的urlMap中,把對靜態資源的訪問由HandlerMapping轉到 org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler 處理並返回. DefaultServletHttpRequestHandler使用就是各個Servlet容器本身的默認Servlet. 推薦使用*.do或方案2,若是你訪問一個網站的默認主頁(也就是web.xml中配置的welcome-file)index.jsp,通常咱們能夠相似這樣訪問:http://localhost:8080/apprecommend-web/ 會自動跳轉到http://localhost:8080/apprecommend-web/index.jsp,若是使用方案1,就不能這樣訪問了,由於spring會攔截全部的請求,可是攔截到的/apprecommend-web/沒有匹配的處理action,並且就直接 返回404,再也不處理了,若是使用方案2,還會由DefaultServletHttpRequestHandler去調用Servlet容器本身的默認Servlet進行處理,而後返回。