In restful style SPRINGMVC will be configured as follows
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>
<init-param>
< Param-name>contextconfiglocation</param-name>
<param-value>/web-inf/classes/conf/spring/ mvc-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
< Url-pattern>/</url-pattern>
</servlet-mapping>
Obviously, the servlet corresponding to the url-pattern is defined as/, so the servlet will match these static resources such as/images/a.jpg,/css/hello.css, and even/jsp/stock/index.jsp these The JSP will also match. However, the corresponding Controller is not defined to handle these resources, so these requests are usually not completed.
Very troublesome. No Struts convenient. This is bad, so the url-pattern that you define its core filter in Struts is *.action, and of course it does not affect the handling of JSP and static resource operations. If Spring MVC also defines similar url-pattern, there is no problem.
Speaking of which, we should think of a problem. In Tomcat, only the servlet can handle the request, and even the JSP will be compiled into a servlet. Even if we use Struts, we define *.action's Url-pattern, that is, CSS, *.GFI and so on who will deal with these static resources ... Don't take it for granted that I didn't enter the path to the picture. For example,/images/a/b/c.gif. Note that in a servlet container, only the servlet takes processing resources.
These resources are handled by the servlet that's for sure. However, different servlet container/application servers have a small name for the servlet that handles these static resources:
Tomcat, Jetty, JBoss, and GlassFish: The default Servlet name is "Defaults"
Google App Engine: The default Servlet name is "_ah_default"
Resin: The default Servlet name is "Resin-file"
WebLogic: The default Servlet name is "Fileservlet"
WebSphere: The default Servlet name is "Simplefileservlet"
◇ Scenario One: Activate Tomcat's defaultservlet to handle static resources
<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>
Each type of static resource needs to be configured with a servlet-mapping, and at the same time, written in front of Dispatcherservlet, let Defaultservlet intercept first.
However, there will be a problem, that is, unable to access the resource files under Classpath, look at the configuration of Tomcat Defaultservlet, there seems to be no place to specify the directory.
◇ Scenario Two: Spring 3.0.4 later version provides <mvc:resources/>
<!--handle static resources--<!--uploaded picture cache for 1 months, other js,css,img resources cached one year--<mvc:resou RCEs mapping= "/res/**" location= "/res/" cache-period= "2592000"/> <mvc:resources mapping= "/resources/**" l ocation= "/resources/" cache-period= "31536000"/> <mvc:resources mapping= "/css/**" location= "/css/" cache-p
eriod= "31536000"/> <mvc:resources mapping= "/js/**" location= "/js/" cache-period= "31536000"/> <mvc:resources mapping= "/img/**" location= "/img/" cache-period= "31536000"/> <mvc:resources mapping= "/im ages/** "location="/images/"cache-period=" 31536000 "/>
mapping map to Resourcehttprequesthandler for processing, location specifies the position of the static resource, which can be the Web application root directory, Jar package, so that static resources can be compressed into the jar package. Cache-period can make a static resource Web cache. &NBSP
using the <mvc:resources/> element, the URI of the mapping is registered to In Simpleurlhandlermapping's UrlMap, key is the URI pattern value of mapping, and value is Resourcehttprequesthandler, which subtly accesses the static resource by the Han Dlermapping goes to Resourcehttprequesthandler processing and returns, so it supports access to static resources within the Classpath directory, the jar package.
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 '
◇ Plan Three: use <mvc:default-servlet-handler/>
<mvc:default-servlet-handler/> will register the "/**" URL to the UrlMap of simpleurlhandlermapping, transferring access to the static resource from handlermapping to org . Springframework.web.servlet.resource.DefaultServletHttpRequestHandler processing and returning. Defaultservlethttprequesthandler use is the default servlet for each servlet container.
The default value for the order of the handlermapping mentioned above is supplemented by the following instructions:
defaultannotationhandlermapping: 0
<mvc:resources/> Auto-registered simpleurlhandlermapping: 2147483646
<mvc:default-servlet-handler/> Autoenrollment of simpleurlhandlermapping: 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 did not call A.jpg Controller. And then the order value in ascending, because the last simpleurlhandlermapping is a match "/**", so will definitely match, and then respond to the picture.
In Spring MVC, a picture is accessed, and layers are matched. Performance is certainly not where to go. Not only Spring MVC, but Struts, after all, survive in the servlet container, so long as the servlet container handles these static resources, it is necessary to read these resources into the JVM's memory area. So, when dealing with static resources, we usually add Apache or Nginx to the front end.
In addition, the best performance should be the direct use of the container defaultservlet, let it first intercept the static resource requests, so that the subsequent forwarding and other operations, improve performance, but unable to access the resource files under Classpath. The mvc:resources tag can be easily configured to match rules and resource file path, it should be said to be the simplest and quickest way, of course, this is probably the original idea of MVC namespace design.
In addition, if you want to combine the two, you can try to write a servlet or filter to deal with, but estimated to be difficult and troublesome.
---add 2016-04-19
Recently thought of a way to add an HTTP server to the Java server to handle static resources, the Java server only handles dynamic resources, so it can.
Another good blog post: http://www.cnblogs.com/yank/p/4477204.html