Error 404 when accessing pictures in the project directory

The premise is that the picture directory has been placed outside the WEB-INF, and the Error 404 is still reported. If it is a JSP resource, it can be parsed correctly. Later, it was found that it was caused by the spring MVC configuration file

, added in the configuration file

<mvc:default-servlet-handler />

If you visit again, you will no longer report errors

The landlord checked the function of this configuration

when looking for pages, the servlet follows the route of dispatcher servlet. If it cannot be found, it will report 404

when this default servlet is added, the servlet will look for static content when it cannot be found

How can your dispatcher servlet intercept URLs such as *. Do?There is no problem that you can't access static resources. If your dispatcher servlet intercepts "/", all requests are intercepted, and the access to *. JS and *. JPG is also intercepted


purpose: you can access the static file normally. Don’t report 404 if you can’t find the static file

scheme 1: activate Tomcat’s defaultservlet to process static files

</servlet-mapping> To configure multiple, one for each file

To write in front of the dispatcher servlet, let   The defaultservlet is intercepted first, and this will not enter spring


Tomcat, Jetty, JBoss, and GlassFish   Name of default servlet — “default”
name of default servlet of Google App Engine — “_ah_Default ”
name of the default servlet –” reset file ”
name of the Weblogic default Servlet   — ” FileServlet”
WebSphere   The name of the default servlet — “simplefileservlet”


scheme 2, use < mvc:default-servlet-handler/>

The “/ * *” URL will be registered in the urlmap of simpleurlhandlermapping, and the access to static resources will be transferred from handlermapping to org.springframework.web.servlet.resource.defaultservlethttprequesthandler for processing and return. Defaultservlethttprequesthandler is the default servlet of each servlet container

Supplementary note: the execution sequence of multiple handlermapping:

The order attribute value of defaultannotationhandlermapping is: 0
< mvc:resources/  > Automatically registered   The order property value of simpleurlhandlermapping is:   2147483646

< mvc:default-servlet-handler/> automatic logon   Simpleurlhandlermapping for   The value of the order attribute is:   2147483647

Spring will execute the with a small order value first. When accessing a a.jpg image file, first   DefaultAnnotationHandlerMapping   We can’t find the processor. We don’t have an action called a.jpg. Then find it in ascending order of order value, because the last one   SimpleUrlHandlerMapping   It’s a horse  “/**” Yes, so it will match and respond to the picture


Similar Posts: