Java Web static resource access configuration three ways

Source: Internet
Author: User

Scenario One: Tomcat's defaultservlet to handle static files


put it in front of the Dispatcherservlet and let Defaultservlet intercept the request so that the request does not enter spring.

Scenario two: After the spring3.0.4 version provided the Mvc:resource

The/images/** maps to Resourcehttprequesthandler for processing, and location specifies the position of the static resource. Can be a Web application root directory, inside the jar package, so that the static resources can be compressed into the jar package. Cache-period allows static resources to be Web cache

If the following error occurs, there may be a reason for not configuring <mvc:annotation-driven/>.
Error warning:no mapping found for HTTP request with URI [/mvc/user/finduser/lisi/770] in Dispatcherservlet with Name ' Spring MVC '

Using the <mvc:resources/> element, register the URI of the mapping in the UrlMap of Simpleurlhandlermapping,
Key is the URI pattern value of mapping, and value is Resourcehttprequesthandler,
In this way, the access to static resources is cleverly transferred from handlermapping to Resourcehttprequesthandler processing and returned, so it supports the Classpath directory and the access of static resources within the JAR package.
It is also important to note that you should not set DefaultHandler for simpleurlhandlermapping. Because the DefaultHandler to the static URI is Resourcehttprequesthandler,
Otherwise, the static resources request cannot be processed.


Scenario three, using <mvc:default-servlet-handler/>

  1. < mvc:default-servlet-handler />     

The "/**" URL will be registered in the UrlMap of Simpleurlhandlermapping, The access to the static resource is transferred from handlermapping to Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler processing and returned.
Defaultservlethttprequesthandler use is the default servlet for each servlet container.

Additional note: The order of execution of multiple handlermapping issues:

The Defaultannotationhandlermapping Order property value is: 0

<mvc:resources/> Auto-registered Simpleurlhandlermapping's Order property value is: 2147483646

The value of the Order property for the <mvc:default-servlet-handler/> autoenrollment simpleurlhandlermapping is: 2147483647

Spring executes a smaller order value first. When access to a a.jpg picture file, first through the defaultannotationhandlermapping to find the processor, must not be found, because we did not call a.jpg action. And then by the order value in ascending search, because the last simpleurlhandlermapping is matching "/**", so it will match, you can respond to the picture.



There is no such problem with how your dispatcherservlet intercepts URL suffixes such as *.do,.action.

Generally, the choice is to see the situation.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Web static resource access configuration three ways

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.