"Springmvc from Introduction to unbridled" V. SPRINGMVC Profile Development (processor adapter)

Source: Internet
Author: User

In the previous article we roughly explained the Processor mapper processing process and followed the implementation of the source code process. Let's look at the processor adapter today.

First, the adapter mode

In Dr. Shanhong's book Java and Patterns, this describes the adapter (Adapter) Pattern: The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that would otherwise not work together because of an interface mismatch can work together.
Using electrical appliances As an example, the laptop plug is generally three-phase, that is, in addition to the anode, cathode, there is a polar. And in some places the power outlet is only bipolar, no polar. A power outlet that does not match the power plug of the laptop makes it unusable for the laptop computer. At this point a three-phase converter (adapter) can solve this problem, which is just what this pattern does. Of course, this model is not studied in depth here. Interested students can refer to the online very good article:
Https://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

Second, processor adapter (simplecontrollerhandleradapter)
The central scheduler obtains the processor execution chain (Handlerexecutionchain) through the handlermapping and then passes the processor in the processor execution chain into the Gethandleradapter ( Mappedhandler.gethandler ()); method, the following source code: (The source code in the central scheduler of the Dodispatch () method)

// determine handler adapter for the current request. Handleradapter ha = Gethandleradapter (Mappedhandler.gethandler ());

1. In the Gethandleradapter () method we can see a foreach loop for all processor adapters, and then each call to Ha.supports (handler) method to determine whether the current processor is using the current adapter (in other words, whether the current adapter interface is implemented), or False if it is true. When True is returned, the current adapter is returned to the Dodispatch () method. As follows:

protectedHandleradapter Gethandleradapter (Object handler)throwsservletexception { for(Handleradapter ha: This. Handleradapters) {        if(logger.istraceenabled ()) {Logger.trace ("Testing Handler Adapter [" + Ha + "]"); }        if(Ha.supports (handler)) {returnha; }    }    Throw NewServletexception ("No Adapter for Handler [" + Handler + "]: The Dispatcherservlet configuration needs to includ e a handleradapter that supports this handler ");}

2, Ha.supports (handler) method, with Simplecontrollerhandleradapter for example, as follows

@Override  Public Boolean supports (Object handler) {    returninstanceof  Controller);}

3. In the Dodispatch () method, call the Ha.handle () method and get the Modelandview object, as follows

// actually invoke the handler. MV = Ha.handle (processedrequest, Response, Mappedhandler.gethandler ());

Interested students can continue to explore the source code. This space is limited to explain the necessary code in the tutorial only. Of course, the two ways you need to remember here are the Ha.supports () method and the Ha.handle () method.

"Springmvc from Introduction to unbridled" V. SPRINGMVC Profile Development (processor adapter)

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.