Dabio together to learn the second back of-SPRINGMVC (Controller, adapter description)

Source: Internet
Author: User

Briefly:in the previous article, we learned how to build a simple SPRINGMVC HelloWorld program, this article mainly introduces someCommonly used controllers, adapters learn and function.First, handlermappingProcessor Mapperhandlermappingreturns one to the front controllerHandlerexecutionchainobject that contains aHandler(back-end Controller) objects, multipleHandlerinterceptorInterceptor) object. 1).beannameurlhandlermapping Bean alias path mapper

Beannameurl, what do we find by naming names of classes? Yes, that's right, when you configure the bean, the bean's name is used as the path to its corresponding controller. Generally can be used in conjunction with Simplecontrollerhandleradapter, if you put the example in the previous article written all over, you will find that we are using both of them to achieve. (in the source code, is the implementation of its parent class abstractdetectingurlhandlermapping Determineurlsforhandler method, in the method according to the Bean name Register processor)

Springmvc-servlet.xml can generally be configured to use it:

<span style= "White-space:pre" ></span><!--through/helloworld.action  can access to specific processing methods--><bean name= "/helloworld.action" class= "Com.billstudy.springmvc.controller.HelloWorldController"/>

2). simpleurlhandlermapping Simple path processing mapper

Simpleurl, this is a more interesting mapper, is to map the access path directly to the ID of the specific controller class, by looking at the source code we will find that there is actually a map in the class, to store the injected path and the information about the bean, Then through its parent registration processor processing related requests, see the source code can be better understood:

public class Simpleurlhandlermapping extends Abstracturlhandlermapping {//internal uses a map as the map for storing access paths and Beand private final map UrlMap = new HashMap ();//Since this method is available here, we can inject our data public void setmappings (Properties mappings) through mappings in the config file { This.urlMap.putAll (mappings);} Omit partially unrelated methods ....
</pre><pre name= "code" class= "java" >public void Initapplicationcontext () throws Beansexception { Super.initapplicationcontext (); registerhandlers (This.urlmap);} protected void Registerhandlers (Map urlmap) throws Beansexception {if (Urlmap.isempty ()) {Logger.warn ("neither ' UrlMap ') Nor ' mappings ' set on Simpleurlhandlermapping ");} else {Iterator it = Urlmap.keyset (). Iterator (); while (It.hasnext ()) {string url = (String) it.next (); Object handler = URLM Ap.get (URL);//If it is not the start of the slash, then bring the slash if (!url.startswith ("/")) {URL = "/" + URL;} If it is a string, then dispose of the space, this good, small details are also worth learning, if (handler instanceof string) {handler = (string) handler). Trim (); Call the parent class abstracturlhandlermapping method, make URL path to register processor Registerhandler (URL, handler);}}}
Springmvc-servlet.xml can typically be configured to use:

<!-Simple URL mapping, you can put the path of the project unified here configuration, more convenient, multiple range path can be mapped to a controller, similar to map (key-value)--><bean class= " Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "><property name=" mappings "><props ><!--Key: Access path   value:bean ID--><prop key= "/hello1.action" >hello_controller</prop>< Prop key= "/hello2.action" >hello_controller</prop></props></property></bean>


second, Handleradapter processor adapterThe Handleradapter will wrap the back-end controller as an adapter that supports multiple types of controller development, using the adapter design pattern. 1).Simplecontrollerhandleradapter Simple Controller processor adapter Simple Controller Processor adapter all beans that implement the Org.springframework.web.servlet.mvc.Controller interface as the back-end controller of the SPRINGMVC Analysis of the source code we can better find this.
public class Simplecontrollerhandleradapter implements Handleradapter {//determines if the target class implements the Controller interface, For the further execution of the handle method, make a pre-judgment public Boolean supports (Object handler) {return (handler instanceof Controller); Processing method public Modelandview handle (httpservletrequest request, httpservletresponse response, Object handler) throws Exception {//using a simple, rude way of directly upward transformation executes the HandleRequest method within the class bean that we implement, which is the only implementation method within the controller. Return (Controller) handler. HandleRequest (request, Response);}}
Generally in the configuration file, directly configure the class to support the implementation of the Controller interface class normal use
2).Httprequesthandleradapter HTTP Request Processor Adapter

The HTTP request processor adapter encapsulates HTTP requests as Httpservletresquest and HttpServletResponse objects, similar to the servlet interface. Through the source will find this and the above Simplecontroller processing a bit similar, above is the strong switch controller, here is the strong Httprequesthandler object, so we want to use this adapter, what should be done? You may have discovered through comparative learning that, yes, it would be nice to write a bean to implement the Httprequesthandler interface. Relatively simple, interested students to try to write their own first, and then want to look further inside how to achieve the words can read the source Oh.

OK, today these are the content, mainly is the above mapper and adapter introduction. Big Puma Here is simply a list of several, in fact Springmvc gives us a lot of mappers and adapters, if the small partners have more time to learn more about these, you can continue to look at other class implementations, such as:


Recommend a learning framework tips, you can look at the framework's API document to find several major classes or interfaces, and then understand the overall framework model and the basic architecture (SPRINGMVC component architecture to see the first time), and then through these major classes, and interfaces to learn. In fact, like this relatively mature open source framework, the naming and annotation of the class is quite normative, as well as the specific use of some design patterns, code philosophy is very worthy of our learning. Write a few small examples, run, hit a breakpoint followed the frame a few times, it was cool. These are sometimes more than the actual bite of the book, of course, as it people, technical books are indispensable, good pull. Next article intends to write, SPRINGMVC inside Focus: The use of annotations.


A simple description of what the mapper is, the adapter:

Mapper: is based on Dispatcherservlet brought it here. the user requests a part of the path to find the processor (which is the specific method we wrote) and some interceptors

Adapter: The object that executes the processor specifically, returns the Modelandview to Dispatcherservlet renders the view with the


Here is love life, Love technology, like to make friends of the big Puma.


Dabio together to learn the second back of-SPRINGMVC (Controller, adapter description)

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.