springMVC过滤静态资源

先看目录结构


springmvc.xml里的主要内容

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="internalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
因为springmvc设置了请求资源定位为:项目名称/jsp/xxxxxx.jsp

所以我们直接这样(<link rel="stylesheet" href="css/bootstrap.min.css">)引入静态资源时就变成了/jsp/css/bootstrap.min.css

所以我们要修改一点内容

①修改springmvc.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: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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                 
    <context:component-scan base-package="com.controller"/> 
    <mvc:default-servlet-handler /> 
    <mvc:annotation-driven />   
    <mvc:resources location="/js/" mapping="/js/**"/>  
    <mvc:resources location="/css/" mapping="/css/**"/>   
    <!-- 定位返回页面 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="internalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

②修改访问路径:

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css">		
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.min.js"></script>


猜你喜欢

转载自blog.csdn.net/m0_37224390/article/details/78310422
今日推荐