Ant Path matching rule

Source: Internet
Author: User

Ant Path Matching principleThe path matching principle (path Matching) in Spring MVC is more flexible than the standard web.xml. The default policy implements Org.springframework.util.AntPathMatcher, as the name suggests, and the path mode uses the Apache ant style path, Apache The ant style path has three wildcard matching methods (listed in the table below)
These can combine a variety of flexible path patterns
Table Ant Wildcard Characters
Wildcard Description
? Match any single word character
* Match 0 or any number of characters
** Match 0 or more directories

Table Example Ant-style Path Patterns

Path Description
/app/*.x Match (matches) all the. x files under the app path
/app/p?ttern Matching (matches)/app/pattern and/app/pxttern, but excluding/app/pttern
/**/example Matching (matches)/app/example,/app/foo/example, and/example
/app/**/dir/file. Matching (matches)/app/dir/file.jsp,/app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, and/app/dir/file.java
/**/*.jsp Match any of the. jsp files (matches)

Handlermapping

A Web application basically relies on the mapping between the URL and the request processor, and spring MVC is the same, but spring MVC is just like everything Spring does (translator: flexible, configurable, but also a lot of complexity) There's definitely not one way to map the relationship between URLs and controller, and in fact, allow yourself to create mapping rules and implementations, not just URL mappings.
Spring provides map processing for URLs, and the order and method of processing path patterns are not limited to any interfaces. The default implementation org.springframework.web.servlet.handler of Spring MVC. Abstracturlhandlermapping, will match a path with the longest path pattern.
For example, given that a request is/app/dir/file.jsp and there are now two path-matching patterns/**/*.jsp and/app/dir/*.jsp, then the URL will match that pattern. The final pattern,/app/dir/*.jsp, is matched by the principle of the longest (has more characters), which has more characters than/**/*.jsp. Note that this principle is not specified in any high-level interface, but it is an implementation detail.
!! Let's take a look at the default two handlermapping
Beannameurlhandlermapping
The default mapping policy implementation is org.springframework.web. Servlet.handler.BeanNameUrlHandlerMapping class. This class requires any bean that is likely to process the request to have a name or alias that starts with a backslash (/) character, which can be a URL-compliant name or alias. Any name in the path matching principle.

Listing a Controller mapped by a Bean Name

XML code <bean name= "/home" class= "Com.apress.expertspringmvc.flight.web.HomeController" > <property name= "Fli" Ghtservice "ref=" Flightservice "/> </bean>


Note: You cannot use the id attribute to specify any URL path, because in the XML specification the (/) character is forbidden to specify the ID of the XML, but you can have both the ID and the name attribute in a bean definition
The disadvantage of beannameurlhandlermapping
It is very easy to use, but also has some shortcomings. It cannot allude to a prototype bean. In other words, when using beannameurlhandlermapping, all request-processing classes can only be a single instance (singletons). In general, the controllers are based on a single example, so this is not a serious problem. Also, I will introduce controller in later chapters, and there will only be a few types of controller need to be prototypes.


Note: The Prototype Bean is a non-single instance (Non-singleton) bean. Any time a call to the ApplicationContext Getbean () method returns a new Bean instance more information please refer to <<pro spring>>.


Another problem with Beannameurlhandlermapping is the application of interception (Interceptor) in your application. Because the mapping of the requested mapping (handler mapping) and the Bean (beans imapping) are bound together, there is no separation, thus creating a complex relationship between the controller and the interception. If you have complex request mapping requirements, you can use simpleurlhandlermapping instead of beannameurlhandlermapping. Simpleurlhandlermapping as another option for beannameurlhandlermapping, Simpleurlhandlermapping pointed to Beannameurlhandlermapping's two major weaknesses. It can map the prototype type of request processor, and it allows you to processor (handlers) and interceptors ( Interceptors) creates complex and flexible mappings between
The mapping algorithm for the path is using the same underlying component as beannameurlhandlermapping, so this pattern is still mapped to the requesting processor (request handler), and you simply declare in ApplicationContext , you can use the simpleurlhandlermapping. Dispatcherservlet is found by type and does not create a default mapper (beannameurlhandlermapping). Meaning, if you want both, Then it is necessary to declare it all in ApplicationContext.
Hint Dispatcherservlet will be chained to handle the request (translator: In turn processing the request, you can refer to the filter and GOF in the responsibility chain mode), so as to allow you to deal with the mapping, mapping processor (Handler mappings) implemented the Orderedr interface.

Original address: http://www.blogjava.net/supercrsky/articles/293728.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.