springmvc静态资源访问不到问题

Failed to load resource: the server responded with a status of 404 (Not Found)
浏览器里的报错信息。

如果你的springmvc没有配置好,你可以再检查一遍看看有没有遗漏的。

一般静态资源报错,是被springmvc拦截了,我们把静态资源设置放行就可以了,在这里我展示一下我自己的配置供大家参考:
springmvc.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!--开启注解的扫描-->
    <context:component-scan base-package="cn.test"></context:component-scan>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--前端控制器,那些静态资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>      <!--设置样式资源不拦截-->
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources> <!--设置图片资源拦截-->
    <mvc:resources location="/js/**" mapping="/js/**"></mvc:resources>        <!--设置JavaScript资源不拦截-->

    <!--开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven enable-matrix-variables="true"/>

    <!--使用原型容器找到静态资源-->
    <mvc:default-servlet-handler/>
</beans>

web.xml配置

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!--配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置解决中文乱码的过滤器-->
    <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>
</web-app>

配置的时候需要注意的是:

 <!--前端控制器,那些静态资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>      <!--设置样式资源不拦截-->
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources> <!--设置图片资源拦截-->
    <mvc:resources location="/js/**" mapping="/js/**"></mvc:resources>        <!--设置JavaScript资源不拦截-->

    <!--使用原型容器找到静态资源-->
    <mvc:default-servlet-handler/>

配置完了之后,能解决大部分问题了
得到帮助的小伙伴记得点赞!!!

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/112465934