Question about the use of "/" to dispatcherservlet URL-Pattern

Source: Internet
Author: User

I used *. Do all the time, but *. Do is ugly, so I used "/" to configure it:

 
<Servlet> <servlet-Name> dispatcherservlet </servlet-Name> <servlet-class> Org. springframework. web. servlet. dispatcherservlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-Name> dispatcherservlet </ servlet-Name> <URL-pattern>/</url-pattern> </servlet-mapping>

But the problem isHow can I access static files, such as JPG, JS, and CSS?

If your dispatcherservlet intercepts a URL with a suffix such as "*. Do", you will not be able to access static resources.

If your dispatcherservlet intercepts "/" and intercepts all requests in order to implement the REST style, access to static files such as *. js and *. jpg will also be blocked.

We need to solve this problem.

 

Purpose: To access a static file normally. If the static file cannot be found, the Error 404 is returned.

Solution 1: Activate Tomcat's defaservlet servlet to process static files

 
<Servlet-mapping> <servlet-Name> default </servlet-Name> <URL-pattern> *. JPG </url-pattern> </servlet-mapping> <servlet-Name> default </servlet-Name> <URL-pattern> *. JS </url-pattern> </servlet-mapping> <servlet-Name> default </servlet-Name> <URL-pattern> *. CSS </url-pattern> </servlet-mapping>

Before dispatcherservlet, let DefaultServlet intercepts the request first so that the request will not enter spring. I think the performance is the best.

 

Tomcat, Jetty, JBoss, and glassfish default servlet name -- "default"
Google App EngineBuilt-in Default servlet name -- "_ ah_default"
ResinBuilt-in Default servlet name -- "Resin-file"
WebLogic Built-in Default servlet name -- "fileservlet"
WebSphereBuilt-in Default servlet name -- "simplefileservlet"

There are two other solutions, you can refer to: http://blog.csdn.net/this_super/article/details/7884383

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.