It is very simple to configure URL interception in Springmvc. Find an example online and you can pass. However, when I made several Web projects and participated in other people-led Web projects, I found that the URL configuration was also very learned.
1. Let's talk about a more common:
<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>
If an interceptor is configured, it will only intercept. HTML dynamic requests.
Static resources do not go spring, also do not go interceptors, the performance is certainly better.
If Nginx is used, configure static resource interception to allow Nginx to handle access to static resources. Nginx is stronger than web containers such as Tomcat in dealing with static resources.
Cons: This method of intercepting dynamic requests is more rigid.
2. I myself often have a need to,http://fansunion.cn/news this unspecified. html suffix is actually a dynamic request, so I'm configuring Url-pattern like "/", That is to intercept all requests. URL is flexible configuration, the problem comes again, static resources are no longer processed by Tomcat, so must be configured in Springmvc,
<mvc: Resources mapping= "/static/**" location= "/static/"/>
Let SPRINGMVC also deal with static resources. Obviously, having SPRINGMVC handle static resource performance is not as high as Tomcat direct processing.
in theory, the more times the request is brokered, the worse the performance, .
thought it was all right, although the performance of static resources is low, at least the program can run normally, "anyway is mixed."
Further requirements, if the login and other interceptors are configured in spring, this time the static resources will be blocked in.
<mvc:resources mapping= "/static/**" location= "/static/"/> This URL mapping, also cannot escape the claws of the Interceptor.
How did I find out about 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;
}
The company's project, Boss's login blocker is configured as above, "Handlermethod Handlermethod = (handlermethod) handler;". But I found this line of code problematic in my own project. If a static resource is intercepted, an error occurs:
Java.lang.ClassCastException:org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot is cast to Org.springframework.web.method.HandlerMethod can be found through anomalies,Object Handler isResourcehttprequesthandler, but notHandlermethod.
because mvc:resources the static resource request to theResourcehttprequesthandlerprocessing, so the cast is problematic.
The boss configuration in the company project is not problematic because he is configured to intercept only ". html" dynamic requests, so the cast is always true.
---------------------------------------------
We analyzed the above two cases, found that "fundamental contradiction" "fundamental needs" is what?
1. Dynamically requested URLs should be very flexible and/news/news.html should count as dynamic requests.
The Url-pattern of 2.SpringMVC can be configured/, *.html, or regular expressions, but I don't like to use regular expressions.
3. If possible, SPRINGMVC should not intercept static resources, so that the Tomcat container can be handled better directly. <mvc:resources mapping= "/static/**" location= "/static/"/>
This is for performance reasons.
4. If the online server is configured with Nginx, I can choose to have nginx intercept the static request because it performs better than Tomcat.
If you configure Nginx locally, you do not need to change any code.
-------------------------------------------------
The drawback of the first method mentioned above is that the URL configuration is not flexible enough.
The disadvantage of the second method is that SPRINGMVC to intercept static resources, and the login interceptor will also intercept static resources, not only poor performance, the program has to be modified again to determine the actual type of Handlermethod.
3. Ultimate Solution:
Based on the 2nd method I'm accustomed to, further improvements:
Remove <mvc:resources mapping= "/static/**" location= "/static/"/>, 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-mapping><servlet-name>default</servlet-name><url-pattern>*.css</url-pattern></servlet-mapping>
The above configuration, Activates Tomcat's defaultservlet to handle static files.
It is important to note that the above configuration should be preceded by the SPRINGMVC interceptor Dispatchservlet.
referenced by:Http://blog.chinaunix.net/uid-20586655-id-3000946.html part Seventh
User question: http://www.iteye.com/problems/66915,http://www.iteye.com/problems/69983
this way the SPRINGMVC no longer responds to static resources, and there is no problem logging in to the interceptor.
------------------------------------------------------------------------
The 3 cases of appeal are discussed in detail, and my conclusion is that URL configuration is not a simple problem at all.
It is a high-tech technology coder, really need to study ah. Today, a little function, encountered a variety of problems, explored for a long time, only to clarify the context, and simply to solve the problem, and can not find the root cause, in the face of the next problem, will be helpless again.
Starting from the original: Http://fansunion.cn/article/detail/61.html
springmvc,3 different URL routing configuration methods (this is not a small problem at all)