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>
Let Springmvc refers to the interception of dynamic requests, JS, CSS, IMG and other static resources do not go through spring, directly let the Web container processing.
If an interceptor is configured, it will only intercept. HTML dynamic requests.
Static resources do not go spring, also do not go interceptors, performance is certainly better.
If Nginx is used, configure static resource interception to allow Nginx to handle static resource access. 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 often have a need, http://FansUnion.cn/news this is not specified. The HTML suffix is actually a dynamic request, so I am configuring url-pattern like to use "/", that is, to intercept all requests. URLs can be flexibly configured, and the problem comes again, static resources are no longer handled by Tomcat, so it must be configured again 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.
This thought is 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, the static resources will be intercepted in this time.
<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;
}
Company's project, Boss's login interceptor 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 through the exception can be found that the Object handler is Resourcehttprequesthandler, not handlermethod.
Because Mvc:resources has given the static resource request to Resourcehttprequesthandler processing, 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/"/>, 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-mapping><servlet-name> Default</servlet-name><url-pattern>*.css</url-pattern></servlet-mapping>
Above configuration, activate Tomcat's defaultservlet to handle static files.
It is important to note that the above configuration should be preceded by the SPRINGMVC interceptor Dispatchservlet.
Reference: 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.
springmvc,3 different URL routing configuration methods [go]