Spring MVC 页面加载 js文件的时候,Chrome 浏览器的console提示
意思是说,本来浏览器请求的是Script文件,即js文件,但是服务器返回的是文本文件
Resource interpreted as Script but transferred with MIME type application/octet-stream:
具体截图

尽管不影响功能,但是有这个提示总感觉别扭。
然后还有一个规律,就是第一次访问的时候会提示,刷新页面也就是第二次访问这个提示就没有了。清除浏览器缓存访问则再次出现。
最后比对了一个没有这个提示的项目,发现这个和SpringMVC 中对静态文件设置的代码段的位置有关。
mvc-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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:orm="http://www.springframework.org/schema/orm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!-- 处理静态资源 -->
<!-- <mvc:resources mapping="/resources/**" location="/resources/"/> -->
<mvc:resources mapping="/js/**" location="/js/"/>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 定义默认访问 -->
<mvc:view-controller path="/" view-name="forward:home"/>
<!-- 视图解析器注册bean -jsp视图解析 http://tool.oschina.net/apidocs/apidoc?api=Spring-3.1.1-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描bean -->
<context:component-scan base-package="com.bestcxx.mavenstu.mvc.controller"/>
</beans>
之后改为如下(注意,“处理静态资源”这个注释下代码段的位置)
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:orm="http://www.springframework.org/schema/orm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 定义默认访问 -->
<mvc:view-controller path="/" view-name="forward:home"/>
<!-- 视图解析器注册bean -jsp视图解析 http://tool.oschina.net/apidocs/apidoc?api=Spring-3.1.1-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描bean -->
<context:component-scan base-package="com.bestcxx.mavenstu.mvc.controller"/>
<!-- 处理静态资源 -->
<!-- <mvc:resources mapping="/resources/**" location="/resources/"/> -->
<mvc:resources mapping="/js/**" location="/js/"/>
</beans>
就是,静态js文件的声明放到的底部浏览器就可以正常访问了,具体原因还是不得而知,但是这说明配置文件中是有先后顺序之分的。
本文探讨了SpringMVC项目中JS文件加载时出现的MIME类型错误问题,通过调整mvc-servlet.xml文件中静态资源配置的位置解决了Chrome浏览器提示的问题。
1万+

被折叠的 条评论
为什么被折叠?



