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>
<!--配置springMVC的核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置servlet的初始化参数,读取SpringMVC的配置文件,创建Spring容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--配置servlet启动时加载对象-->
<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>
Spring MVC由于配置了前端拦截器拦截了所有请求,在引入js jquery文件时也会去服务器请求文件。所以我们需要在xml配置文件中告诉拦截器哪些资源不用拦截。
<mvc:resources location="/js/" mapping="/js/**" ></mvc:resources>
jsp中引入js文件时的写法
<script src="/js/jquery.min.js"></script>
然后前端就给我报错
response.jsp:7 GET http://localhost:8080/js/jquery.min.js net::ERR_ABORTED 404 (Not Found)
response.jsp:10 Uncaught ReferenceError: $ is not defined at response.jsp:10
感觉是引入文件的路径写法错了,进行改进
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
测试后发现可以了,使用热部署记得重新部署然后清理缓存。