SpringMVC配置了拦截器(interceptors)却显示不出css、js样式的解决办法

首先因为在web.xml里面配置了

 

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

导致所有的连接都会经过DispatcherServlet,会过滤掉css、js等样式,导致页面无法渲染成功

因此需要在springmvc配置文件中放行静态资源

<!--过滤静态资源-->
    <mvc:resources mapping="/common/**" location="/common/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/json/**" location="/json/"/>
    <mvc:resources mapping="/jsplug/**" location="/jsplug/"/>

如果配置了拦截器,在拦截器中放行静态资源

<!--配置拦截器-->
   <mvc:interceptors>
       <mvc:interceptor>
           <!-- 拦截所有mvc控制器 -->
           <mvc:mapping path="/**"/>
           <!-- mvc:exclude-mapping是另外一种拦截,它可以在你后来的测试中对某个页面进行不拦截,这样就不用在
               LoginInterceptor的preHandler方法里面获取不拦截的请求uri地址了(优选) -->
           <!--以下资源不进行拦截-->
           <mvc:exclude-mapping path="/user/toLogin" />
           <mvc:exclude-mapping path="/user/login" />
           <mvc:exclude-mapping path="/common/**"/>
           <mvc:exclude-mapping path="/css/**"/>
           <mvc:exclude-mapping path="/images/**"/>
           <mvc:exclude-mapping path="/js/**"/>
           <mvc:exclude-mapping path="/json/**"/>
           <mvc:exclude-mapping path="/jsplug/**"/>
           <bean class="com.lintrondata.fyds.interceptor.LoginInterceptor"></bean>
       </mvc:interceptor>
   </mvc:interceptors>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值