【springMVC靜態資源無法訪問】靜態資源訪問踩坑!
阿新 • • 發佈:2019-01-08
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <meta name=viewport content="width=device-width,maximum-scale=1,minimum-scale=1,initial-scale=1,user-scalable=no,viewport-fit=cover"> <title>app</title> <link href=./static/css/app.99a1cac4de029656ce1420df9f08e6e0.css rel=stylesheet> </head> <body> <div id=app></div> <script type=text/javascript src=./static/js/manifest.3ad1d5771e9b13dbdad2.js></script> <script type=text/javascript src=./static/js/vendor.ad420f8443715d58c383.js></script> <script type=text/javascript src=./static/js/app.b8009791bfe7d82cb400.js></script> </body> </html>
頁面檔案如上所示 卻訪問不到所有的js靜態資源
頁面檔案在webapp/WEB-INF下
js檔案在 webapp/WEB-INF/static/js/下
url為localhost:8080/projectName/app/index下
在springMVC配置檔案中配置為
<mvc:resources location="/WEB-INF/static/css" mapping="/static/css/**"/>
<mvc:resources location="/WEB-INF/static/js" mapping="/static/js/**"/>
發現靜態資源訪問失敗
最後發現是由於這幾個原因導致的!!!
1.由於src為<script type=text/javascript src=./static/js/app.b8009791bfe7d82cb400.js></script>
則靜態資源的訪問連線為localhost:8080/projectName/app/static.js/xxxxx.js 不在根目錄下 所以應該改頁面url為localhost:8080/projectName/index
2.配置檔案中location最後少了個 “/” 導致解析失敗
改為
<mvc:resources location="/WEB-INF/static/css/" mapping="/static/css/**"/> <mvc:resources location="/WEB-INF/static/js/" mapping="/static/js/**"/>
location表示靜態資源地址 mapping表示對映地址
3.spingmvc版本為3.0.0 改為4.0
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
在spingmvc的配置檔案中 應用spring-mvc-4.0.xsm 因為<mvc:resource xxx/>標籤配置支援springmvc3.0.4以上