javaweb static resource file (.jpg;, css;.js, etc.) processing

When using SpringMVC, in the web.xml file, we often configure DispatcherServlet like this:
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


Note: <url-pattern>/</url-pattern> , where "/" is configured, so the servlet will match things such as /images/login.jpg, /css/login.css, /css/login.js, etc. These static resources,
even jsp including /jsp/index.jsp will match. However, there is no corresponding Controller defined to handle these resources, so these requests usually cannot be completed.

The first way:
If it is changed to *.do or *.action, etc., it is similar to the url-pattern of the filter of Struts is *.action, so it will not affect the operation of static resource files.
<url-pattern>*.do</url-pattern>

The second way:
enable defaultServlet in front of DispatcherServlet, let defaultServlet intercept the request first, so that the request will not enter Spring, and the performance should be the best.
Add in web.xml:
<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.png</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.gif</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>

Simple writing:
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
		<url-pattern>*.jpeg</url-pattern>
		<url-pattern>*.png</url-pattern>
		<url-pattern>*.gif</url-pattern>
		<url-pattern>*.js</url-pattern>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>

Note: The name of the default Servlet that comes with Tomcat, Jetty, JBoss, and GlassFish -- "default" The name of the default Servlet that comes with
Google App Engine -- "_ah_default" The name of the default Servlet that comes with
Resin -- "resin- file" The name of the default Servlet that comes with
WebLogic -- "FileServlet" The name of the default Servlet that comes with
WebSphere -- "SimpleFileServlet" The


third way:
mvc:resources is provided after spring 3.0.4.
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

or
<mvc:resources mapping="/js/*.js" location="/js/" />

  /images/** is mapped to ResourceHttpRequestHandler for processing, location specifies the location of static resources, which can be in the root directory of the web application or in the jar package, so that the static resources can be compressed into the jar package. cache-period allows static resources to be web cached.
        Using the <mvc:resources /> element, the URI of the mapping will be registered in the urlMap of SimpleUrlHandlerMapping, the key is the URI pattern value of the mapping, and the value is ResourceHttpRequestHandler, so that the access to static resources is cleverly transferred from HandlerMapping to ResourceHttpRequestHandler for processing And return, so it supports the access of static resources in the classpath directory and jar package.

Fourth way:
<mvc:default-servlet-handler/>

<mvc:default-servlet-handler /> will register the "/**" url into the urlMap of SimpleUrlHandlerMapping, and transfer the access to static resources from HandlerMapping to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler for processing and return. DefaultServletHttpRequestHandler uses the default servlet of each servlet container.
        In addition, the default value of the above-mentioned HandlerMapping order:

DefaultAnnotationHandlerMapping: 0
<mvc:resources /> Automatically registered SimpleUrlHandlerMapping: 2147483646
<mvc:default-servlet-handler/> Automatically registered SimpleUrlHandlerMapping: 2147483647
        Spring will execute the order first value is relatively small. When accessing an a.jpg image file, first look for the processor through DefaultAnnotationHandlerMapping, it must not be found, we do not have a Controller called a.jpg. Then search in ascending order of the order value. Since the last SimpleUrlHandlerMapping matches "/**", it will definitely match, and then respond to the picture.

        In Spring MVC, to access an image, you have to go through layers of matching. The performance is definitely not much better. Not only Spring MVC, even Struts, they live in the servlet container after all, as long as the servlet container processes these static resources, these resources must be read into the memory area of ​​the JVM. Therefore, to deal with static resources, we usually add apache or nginx to the front end.

The class that handles static resources is org.springframework.web.servlet.resource.ResourceHttpRequestHandler, and the description of location states that Each location must point to a valid directory. That is, each location must point to a valid directory.

Reference: http://www.cnblogs.com/weidiao/p/5517645.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327081578&siteId=291194637