1. 程式人生 > >配置了還是不能訪問靜態資源

配置了還是不能訪問靜態資源

解決方法:
今天遇到這個問題,最終發現是路徑中有跟檢視名相同的部分。
比如說:我有個檢視名叫index,然後我想訪問webapp/index.html
就會出現訪問不到靜態資源的情況。
我建了一個webapp/static/index.html的資料夾和檔案,
訪問:webapp/static/index.html就可以了

猜想原因:

  • 靜態資源不能存放在webapp這個路徑下,否則訪問webapp/index.html會轉換到webapp/WEB-INF/views/index.jsp
  • 檢視名和靜態資源存放的路徑不應該有一致的部分,否則如何確定先對映動態還是靜態資源,比如
    • 檢視對映:/index/*
    • 靜態路徑:webapp/index/index.html
    • 這個時候訪問/index/index.html會轉訪問webapp/WEB-INF/views/index.jsp
    • 所以/*要慎用
    • 檢視對映為:/index,訪問上述路徑並不會出現問題

web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>springmvcdemo</display-name> <!-- 解決中文亂碼:統一編碼UTF-8 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class
>
<init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvcdemo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 注意這裡不能為 /* --> <servlet-name>springmvcdemo</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

springmvcdemo-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

    <!--掃描web層的@Controller-->
    <context:component-scan base-package="springmvcdemo"/>

    <!-- 解除對靜態資源訪問的限制,但是記住靜態資源的路徑不能有檢視名  -->
    <mvc:default-servlet-handler/>

    <!-- 主頁對映 -->
    <mvc:view-controller path="/" view-name="home"/>

    <mvc:annotation-driven enable-matrix-variables="true">
        <!-- 全域性設定響應內容為UTF-8編碼 -->
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"></constructor-arg>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 頁面匹配 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>