Spring MVC accesses static resource issues,. js. jpg. css

Source: Internet
Author: User

If your dispatcherservlet intercepts URLs such as *.do, there is no problem accessing static resources.

If your dispatcherservlet intercepts "/", all requests are intercepted, and access to *.js,*.jpg is intercepted.

Scenario One : Activate Tomcat's defaultservlet to handle static files

XML Code <servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.jpg</url-pattern></servlet-mapping><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.js</url-pattern></servlet-mapping><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.css</url-pattern></servlet-mapping>

To write in front of the Dispatcherservlet, let Defaultservlet first intercept, this will not enter Spring , I think the performance is the best bar.

Tomcat, Jetty, JBoss, and GlassFish the name of the default servlet-the name of the default servlet for the "default" Google App Engine-"_ah_default" Resin default Se Rvlet's name--"Resin-file" WebLogic default servlet name--"Fileservlet" WebSphere default servlet name--"Simplefileservlet"

Scenario Two : The mvc:resources is available after the spring3.0.4 version
How to use Mvc:resources:

XML Code<!--access to static resource files--<mvc:resources mapping= "/images/**" location= "/images/"/>

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/>

XML Code<mvc:default-servlet-handler/>

The "/**" URL will be registered to the Simpleurlhandlermapping UrlMap, and the access to the static resource is transferred from handlermapping to Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler processed 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, we do not call a.jpg action. And then the order value in ascending, because the last simpleurlhandlermapping is a match "/**", so will definitely match, and then respond to the picture.

Finally, how do you dispatcherservlet intercept *.do such a URL, do not save the above problem.

Spring MVC accesses static resource issues,. js. jpg. css

Related Article

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.