SpringMVC3 Learning (4) -- access static files such as js, jpg, and css

Source: Internet
Author: User

If your DispatcherServlet intercepts a URL such as *. do, there will be no access to static resources.

If your DispatcherServlet intercepts all "/" requests, the access to *. js, *. jpg will also be blocked.

When developing spring MVC, we will introduce js, img, css and other files on the jsp page.

Most people store these categories under the folder created under the WebRoot file.

In addition, all requests are intercepted in the web. xml file. In this way, the page cannot access files in the js, img, and css folders.

Method 1: Configure defaservlet servlet in web. xml to process static files

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

Method 2: Use mvc: resources in Springmvc

Springmvc's <mvc: resources location = "**" mapping = "**"/> label appears in spring3.0.4 and is mainly used to access static resources.

<! -- Access static resource files --> <mvc: resources location = "/js/" mapping = "/js/**"/> <mvc: resources location = "/img/" mapping = "/img/**"/>
Location specifies the location of the static resource


Method 3: Use <mvc: default-servlet-handler/>

<mvc:default-servlet-handler/> 

Finally, let's talk about how your DispatcherServlet intercepts URLs such as *. do.

Source code download: http://download.csdn.net/detail/itmyhome/7359943


Welcome to the JAVA technology exchange group: 74955800


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.