springmvc js/css path problem

①No mapping found for HTTP request with URI[/msm2/css/login2.css] in DispatcherServlet with name 'springMVC'  

webapp

  js

  css

  WEB-INF

    jsp

Add in spring-mvc.xml

<!-- 开启注解 -->
 <mvc:annotation-driven />
 <mvc:resources location="/js/" mapping="/js/**"/>
 <mvc:resources location="/css/" mapping="/css/**"/>
 <mvc:resources location="/images/" mapping="/images/**"/>

Summarize:

          Analysis of the reason: Configure the <url-pattern> tag under <servlet-mapping> in web.xml as follows: <url-pattern>/<url-pattern>. That is, all requests are handed over to spring for processing, which means that all requests are intercepted. All the available request urls are configured using the annotation @RequestMapping() in the controller, so that static resources such as js, css, and jsp cannot be accessed, so they need to be configured. Related configuration can be made in web.xml, but no test is successful; it can also be configured in spring-mvc.xml, which is the method used above.

 

         The <mvc:resources/> configuration specifies the location of static resources through the location attribute, and the spring MVC framework handles the static resources by itself. And allow static resources to be placed anywhere, such as in the WEB-INF directory, in the class path, or even as Jar for access.

Refer to the blog: http://blog.csdn.net/liutengteng130/article/details/42153725

 

②js/css general path problem

  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  <%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  %>

 

  <script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
  <script type="text/javascript" src="<%=basePath %>images/login.js"></script>
  <link href="css/login2.css" rel="stylesheet" type="text/css" />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324431312&siteId=291194637