springmvc,3 different ways to configure URL routes

Source: Internet
Author: User

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 only intercept 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 you must configure them again in SPRINGMVC

<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.

<mapping= "/static/**"  location= "/static/"/>
This URL mapping also cannot escape the talons of the Interceptor. How did I find out about this problem?
 Public class extends Handlerinterceptoradapter {         publicboolean  prehandle (httpservletrequest request ,             throws  Exception {                loginutil.setcurrentuser (null);        Initcurrentuser (request, response);         = (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:

Cannot is cast to Org.springframework.web.method.HandlerMethod
With the exception you can see 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>

Activates Tomcat's defaultservlet to handle static files.

This way the SPRINGMVC no longer responds to static resources, and there is no problem logging in to the interceptor.

springmvc,3 different ways to configure URL routes

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.