SpringMVC, three different URL routing configuration methods (this is not a small problem at all), springmvcurl

Source: Internet
Author: User

SpringMVC, three different URL routing configuration methods (this is not a small problem at all), springmvcurl
Configuring URL interception in SpringMVC is very simple. You can find an example online. However, when I have worked on several Web projects and participated in other Web projects, I found that URL configuration is also very knowledgeable.

1. Let's talk about a common one:

<Servlet> <servlet-name> theDispatcher </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring/spring-mvc-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> theDispatcher </servlet-name> <url-pattern> *. html </url-pattern> </servlet-mapping>
SpringMVC intercepts dynamic requests. js, css, img, and other static resources are directly processed by Web containers without passing through Spring.

If an interceptor is configured, only .html dynamic requests are intercepted.

Static resources do not go through Spring or Interceptor. Of course, the performance is good.

If Nginx is used, configure static resource interception so that Nginx can process access to static resources. Nginx is better than Tomcat and other Web containers in terms of processing static resources.

Disadvantage: This method of intercepting dynamic requests is rigid.

2. I often have a kind of demand, http://FansUnion.cn/news this kind of unspecified .html suffix is actually dynamic request, so I like to use "/" in the configuration of url-pattern, that is, to intercept all the requests. The URL can be flexibly configured, and the problem arises again. Static resources are no longer processed by Tomcat, so you must configure it again in SpringMVC,
<Mvc: resources mapping = "/static/**" location = "/static/"/>
Let SpringMVC also process static resources. Obviously, SpringMVC's performance in processing static resources is not as high as that of Tomcat.
Theoretically, the more requests are transferred, the worse the performance.

I thought everything would be fine. Although the performance of static resources is low, at least the program can run normally. "It's just a mix ".

For further requirements, if an interceptor such as logon is configured in Spring, static resources will also be intercepted.
<Mvc: resources mapping = "/static/**" location = "/static/"/> This URL ing cannot escape the blocker.

How did I find this problem?
Public class BaseLoginInterceptor extends HandlerInterceptorAdapter {
Public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {LoginUtil. setCurrentUser (null); initCurrentUser (request, response); HandlerMethod handlerMethod = (HandlerMethod) handler;
}
In the company's project, the Boss login interceptor is configured as above, "HandlerMethod handlerMethod = (HandlerMethod) handler ;". However, in my own project, I found that this line of code is faulty. If static resources are intercepted, an error is returned:
Java. lang. classCastException: org. springframework. web. servlet. resource. resourceHttpRequestHandler cannot be cast to org. springframework. web. method. handlerMethod can be found through an exception that the Object handler is ResourceHttpRequestHandler rather than HandlerMethod.
Because mvc: resources handed over static resource requests to ResourceHttpRequestHandler for processing, forced conversion is problematic.

In the company project, the bossconfiguration has no problem because only the dynamic request "".html" is intercepted. Therefore, forced conversion is always valid.
---------------------------------------------
We have analyzed the above two situations and found out what are the "fundamental contradictions" and "fundamental needs?
1. Dynamic request URLs should be flexible, And/news/news.html should be regarded as dynamic requests.
2. SpringMVC url-pattern can be configured with/, *. html, or regular expressions, but I do not like to use regular expressions.
3. If possible, SpringMVC is recommended not to intercept static resources so that Tomcat containers can directly handle them better. <Mvc: resources mapping = "/static/**" location = "/static/"/>
This is for performance consideration.
4. If Nginx is configured on the online server, you can enable Nginx to intercept static requests because the processing performance is higher than that of Tomcat.
No code needs to be modified if Nginx is configured locally.

-------------------------------------------------
The disadvantage of the first method mentioned above is that url configuration is not flexible enough.
The disadvantage of the second method is that SpringMVC intercepts static resources, and logging on to the interceptor intercepts static resources. Not only does SpringMVC have poor performance, but the program has to be modified again to determine the actual type of HandlerMethod.

3. Ultimate solution:
Based on the 2nd methods I used to improve:
Remove <mvc: resources mapping = "/static/**" location = "/static/"/> and do not map static resource requests.

Add the following configuration in web. xml:
<Servlet-mapping> <servlet-name> default </servlet-name> <url-pattern>/static/* </url-pattern> </servlet-mapping> <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>

In the preceding configuration, activate Tomcat's defaservlet servlet to process static files.

Note that the above configuration should be prior to SpringMVC interceptor DispatchServlet.
See: http://blog.chinaunix.net/uid-20586655-id-3000946.html Part 7
Questions: http://www.iteye.com/problems/66915,http://www.iteye.com/problems/69983

In this way, SpringMVC no longer responds to static resources, and there is no problem with the login interceptor.

------------------------------------------------------------------------
I have discussed three cases of appeal in detail and concluded that URL configuration is not a simple issue at all.

IT is a high-tech Coder that really needs to be studied. Today, we have implemented some small functions and encountered various problems. After a long period of exploration, we have clarified the context. We have solved the problem, but cannot find the root cause. When we encounter the next problem, will be helpless again.

First: http://fansunion.cn/article/detail/61.html

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.