About the two kinds of mapper in SPRINGMVC cannot coexist

Source: Internet
Author: User

First of all, we all know that Springmvc has two mappers:

Org.springframework.web.servlet.handler. beannameurlhandlermapping

And

Org.springframework.web.servlet.handler. simpleurlhandlermapping

And the first "name URL mapper" is used by default.

There are two types of mappers that can coexist .

But when the project was built today, when the two mappers were enabled,beannameurlhandlermapping failed.

Here is my XML configuration:

    <BeanID= "Hello"name= "/hello.action"class= "servlet." Hello "></Bean>        <BeanID= "Yaha"class= "servlet." Yaha "></Bean>        <BeanID= "Update"class= "servlet." Update "></Bean>    <Beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        < Propertyname= "Mappings">            <Props>                <propKey= "/update.action">Update</prop>                <propKey= "/yaha.action">Yaha</prop>            </Props>                </ Property>    </Bean>

Operation result :

Access to Update.action ———— Normal

Access to Yaha.action ———— Normal

Visit Hello.action ———— 404

It is obvious that the "Name URL" Mapper failed to match.

Analysis :

Failure is just the default one, and usually we use the default mapper is often not to write the mapper's <bean> declaration , and today declare a mapper, then all the mappings may be all to the declaration of the one. So the default mapper is no longer called.

Test :

Add the following declaration to the XML:

    <class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></ Bean >

Access to Update.action ———— Normal

Access to Yaha.action ———— Normal

Access to Hello.action ———— Normal

Conclusion :

do not forget to declare the default beannameurlhandlermapping when it is necessary to use both mappers . (It is best to declare it even if you use default only)

About the two kinds of mapper in SPRINGMVC cannot coexist

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.