jsp中引入jquery报错:Failed to load resource: the server responded with a status of 404 (Not Found)

问题描述:jquery

  今天本身在搭建spring、springMVC、hibernate框架,搭建完成后,在引入jquery时,发现jquery无论用。个人解决顺序是:web

一、检查路径,发现路径没错,另外须要注意的是,非rest风格的代码下,js资源通常为静态资源,不要放在web-inf下,不然会加载不上,另外注意js编码是否和当前页面编码一致,此处我设置utf-8编码。spring

<script src="${pageContext.request.contextPath }/js/jquery-1.4.2.js"  charset="utf-8"></script>

二、排错,我将jquery中代码直接复制到个人script里,发现是管用的,这至少说明,jquery没有问题。mvc

三、控制台输出发现了问题所在:app

14:57:55,021 DEBUG DispatcherServlet:823 - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/SSH_TEST/js/jquery-1.4.2.js]
14:57:55,022 DEBUG RequestMappingHandlerMapping:209 - Looking up handler method for path /js/jquery-1.4.2.js
14:57:55,023 DEBUG RequestMappingHandlerMapping:219 - Did not find handler method for [/js/jquery-1.4.2.js]

  第一句:DispatcherServlet 以get方式请求[/SSH_TEST/js/jquery-1.4.2.js]框架

  第三句:没有找到编码

4.解决方法:url

  个人web.xml文件部分以下spa

<!-- springMVC核心控制器 -->
  <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>
  </servlet>
<!-- map all request to the dispatcherServlet for handling -->
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

  注意,黄色部分属于rest风格,即dispatcherServlet将处理全部web容器的请求,当请求加载静态资源jquery.js时,控制器类中没有写,所以会提示【Failed to load resource: the server responded with a status of 404 (Not Found)】,若是是传统的风格,非rest,通常静态资源时web容器本身就加载了。hibernate

  处理方法:在springMVC.xml文件添加以下。

<mvc:default-servlet-handler/>
相关文章
相关标签/搜索