springmvc 請求經過controller后靜態資源無法訪問的問題


經過RequestMapping(“xx”)后 轉發請求時會在url里面附帶地址,

導致訪問靜態資源文件失敗,

 

解決辦法是在 spring-mvc.xml文件中加上

<mvc:default-servlet-handler/>

<!-- 由於在web.xml中定義的url攔截形式為“/”表示攔截所有的url請求,
包括靜態資源例如css、js等。所以需要在springmvc.xml中添加資源映射標 -->
<mvc:resources location="/static/js/" mapping="/*/static/js/**"/>
<mvc:resources location="/static/css/" mapping="/*/static/css/**"/>
<mvc:resources location="/static/fonts/" mapping="/*/static/fonts/**"/>
<mvc:resources location="/static/images/" mapping="/*/static/images/**"/>
<mvc:resources location="/static/lib/" mapping="/*/static/lib/**"/>



以及在web.xml里加上
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM