spring-methods for accessing static resource files

Source: Internet
Author: User

transferred from: http://blog.163.com/zhangmihuo_2007/blog/static/27011075201453044959574?suggestedreadingIf 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. With the above Url-pattern configuration, all URL requests will be intercepted by spring MVC's dispatcherservlet.  Such a configuration, will cause JS files, CSS files, image files and other static resources can not be accessed. For example, your JSP page references a picture and juqery:
<body> <h3> image access </h3> <img src="image/1.jpg"> </body>
Then, I'm sorry, the operation will prompt 404 error, cannot find the picture resource, because Springmvc "image/1.jpg" also as a request connection, but this request connection we did not. Workaround:

1. spring.3.0.4 later versions are available <mvc:resources/> (recommended in this way)

a) first, <mvc:resources/> allows static resources to be placed anywhere, such as the Web-inf directory, under the Classpath, and you can even hit a static file such as JavaScript into the jar package. The Location property specifies the position of the static resource, because the Site property is a resource type, so you can specify the resource location by using a resources prefix such as "classpath:". The static resources of a traditional web container can only be placed under the root path of the Web container, and <mvc:resources/> completely breaks the limit. B) Secondly, <mvc:resources/> optimizes static resources based on the current well-known browser optimization principles of page speed and YSlow. You can use the Cacheseconds property to specify the cache time for a static resource on the browser side, which can typically be set to one year to take advantage of browser-side caching. When you output a static resource, the expires and Cache-control values of the message headers are configured to respond to the configuration. When receiving a request for a static resource, the last-modified value of the request header is checked, and if the static resource does not change, it returns 303 corresponding status code, prompting the client to use the data cached by the browser rather than outputting the contents of the static resource to the client. To fully conserve bandwidth and improve program performance. In Spring-servlet.xml, add the following configuration:
<mvc:resources location ="/image/" mapping="/image/**"/>
Or: tbody> /tbody>
<mvc:resources location= "/,classpath:/meta-inf/image/"  mapping=< Span style= "font-style:italic; Color: #2a00ff; " > "/image/**"/>&NBSP;&NBSP;
The above configuration maps the/meta-inf/image/directory under the Web root path "/" and the Classpath to the/image path. Suppose the Web root path has images, JS these two resources directory, under images there are bg.gif pictures, under JS test.js file, you can/images/1.jpg this static resources. Assuming that Webroot also has images/bg1.gif and js/test1.js, it can also be referenced in Web pages through/resources/images/bg1.gif and/resources/js/test1.js. You can also simply configure Web-inf/spring-servlet.xml as follows:
<!--static resource access --& Lt mvc:resources  location= "/image/"  mapping= "/image/**" Span style= "font-style:normal; Color: #008080; " >/> & Lt mvc:resources  location= "/js/"  mapping= "/js/**" /> <mvc:resources location="/css/" mapping="/css/**"/>
It is also important to note that using this method requires importing the Org.springframework.web.servlet-3.1.4.release.jar package into the Web/lib directory. And the version is greater than 3.0.4, otherwise, in the development environment will produce to the resources element did not find the situation:Cvc-complex-type.2.4.c:the matching wildcard is strict, but no declaration can be found for element ' mvc:resources '.
Must be in a running environment without this problem, this is a bug in version 3.0.4, the spring-mvc-3.0.4.xsd will be updated in 3.0.5.

2. Activate Tomcat's defaultservlet to handle 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> to configure multiple, each file configuration one to write in front of the Dispatcherservlet, let Defaultservlet first intercept, this will not enter spring, I think performance is the best. 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 the name of the default servlet--the name of the "Fileservlet" WebSphere default servlet--"simplefileservlet" finally explains how to Your dispatcherservlet intercepts URLs such as *.do, so there is no such problem.

spring-methods for accessing static resource files

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.