Perfect solution Springmvc Static resources cannot be found (no mapping found for HTTP request with URI) problem

Source: Internet
Author: User


There is a rather new problem today, but it should be one that you often encounter with the spring MVC framework, and then write it out to share.


Problem Description: The project started normally, you can access the page, but cannot find static resource files, such as Css,js file resources.

Browser Console error message:


Idea background error message:


February 07, 2017 11:27:35 a.m. Org.springframework.web.servlet.PageNotFound nohandlerfound
Warning: No mapping found for HTTP requ EST with URI [/myboss-web/js/jquery-1.8.3.js] in Dispatcherservlet with Name ' Mvc-dispatcher '
February 07, 2017 11:27:35 a.m. Org.springframework.web.servlet.PageNotFound nohandlerfound
Warning: No mapping found for HTTP request with URI [/ Myboss-web/js/fun.base.js] in Dispatcherservlet with name ' Mvc-dispatcher '

Because the project used spring security, I once suspected that it is not to set access to static files, and then confirmed that the access to static resources have been set without authorization, also checked the MVC interceptor configuration information, did not find any obvious anomalies, and then puzzled. After looking up some information on the Internet, it turns out that the MVC interceptor is really haunting.


My previous Interceptor configuration information:

  <!--configuration Interceptor-->
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    < Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
    <init-param >
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/spring/ applicationcontext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</ load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher </servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


From configuration analysis, this configuration will intercept all requests and give spring to handle them. All of the URLs for spring's requests are marked with the annotation @requestmapping in controller, so access to static resources is not accessible in such cases.

Here are three solutions that you choose to use

Method 1: Simply and rudely tell the server that these resources do not need to be intercepted.

It is configured directly in the Web.xml file as follows:

 <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-patter n> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <ur l-pattern>*.xml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>defau lt</servlet-name> <url-pattern>*.swf</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.zip</url-pattern> </servlet- mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif&lt ;/url-pattern> </servlet-mapping> <servlet-mapping> &LT;SERVLET-NAME&GT;DEFAULT&LT;/SERVLET-NAME&G
    T <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name&gt ;d efault</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <ser Vlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>

If you think this is too much trouble, too low, please go to the following two ways.


Method 2: Just tell spring, that's all you have to do.

Add a resource file mapping to the MVC configuration file.

  <resources mapping= "/resources/**" location= "/resources/"/> <resources mapping=
  "/images/**" location= "/images/"/>
  <resources mapping= "/js/**" location= "/js/"/>


Annotations:
Mapping: Mapping
Location: Local resource path, note must be a path under the WebApp root directory.
Two *, which represents all URLs under the map resources/, including subpath (that is, multiple/)
There may be a package after reading this will come back to say: "Bo master what to write, according to write or useless, I definitely did not knock typo." I still copy the past ... ”
If there is such a man, do not tell me, or I am afraid I will not help to take out my 30-meter broadsword.
Please change the path yourself ...
Of course, there's one more point to note:
The configuration of the location must be Web-app root directory, if you put the resource directory, placed under Web-app/web-inf, congratulations, access failed. 404 Not FOUND


Method 3: Use the default for all, and don't be a mind-damn.

<mvc:default-servlet-handler/>

Compare lazy child shoes can be directly in the MVC configuration file plus such a sentence, this is really convenient, but the disadvantage is that he will send your request all according to the default processing.
If you post this sentence directly, I am afraid that the cheese will say how the error, MVC label error.
Dude, if you can't solve this kind of mistake, you should not look at this blog, first to see what is called Namespace bar. Still have time to treat.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.