Summary of Mappers and adapters in "Springmvc learning 03" SPRINGMVC in annotations and non-annotated ways

Source: Internet
Author: User
Tags lenovo

As already seen in the previous article Springmvc, the configuration of the Springmvc.xml in the mapper and adapter is used in a non-annotated way to configure, this is a non-annotated way, here to review:

1. Non-annotated mode 1.1 Processor Adapter

The processor adapter used in the previous section is: Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter. That

The Simplecontrollerhandleradapter adapter can perform the handler that implements the Controller interface, so the handler we write needs to implement the Controller interface.
In addition to this adapter, there is another adapter called Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter. That can be configured:

The Httprequesthandleradapter adapter can perform the handler that implements the Httprequesthandler interface, so the handler we write needs to implement the Httprequesthandler interface, This is a little bit different from implementing the Controller interface, such as we write a handler:

 Public  class ItemsController2 implements Httprequesthandler {    @Override     Public void HandleRequest(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {//In practice, call the service lookup database and query the product list, where static data is used directly to simulateList<items> itemslist =NewArraylist<items> ();//Populating the list with static dataItems Items_1 =NewItems (); Items_1.setname ("Lenovo Notebook"); Items_1.setprice (6000f); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2 =NewItems (); Items_2.setname ("Apple Phone"); Items_2.setprice ( thef); Items_2.setdetail ("IPhone6 Apple phone!" ");        Itemslist.add (Items_1); Itemslist.add (items_2);//Set model dataRequest.setattribute ("Itemslist", itemslist);//Set up a forwarded viewRequest.getrequestdispatcher ("/web-inf/jsp/items/itemslist.jsp"). Forward (request, response);//Use this method to set the data format of the response, such as in response to JSON data, by modifying the response        /* response.setcharacterencoding ("Utf-8");        Response.setcontenttype ("Application/json;charset=utf-8"); Response.getwriter (). Write ("JSON string"); * /}}

The previous fetch data is identical, after go to the view part and implement controller interface is different, implement Httprequesthandler interface, finally here processing and the original servlet is the same, to the request domain to deposit data, Then jump to the new JSP view page. However, this method can also modify the response, set the response data format, and so on.
With that adapter, the same thing is: there can only be one method in each handler, because to inherit a controller or Httprequesthandler, you need to rewrite the corresponding method to handle the logic in that method. In other words, a handler can not have two methods or more than two to deal with different logic, which is the disadvantage, but also has its advantages, limit the programmer's programming, comparative specifications.
These two adapters can be used at the same time, I tested it, do not conflict, I am equipped with two adapters, wrote two handler, respectively to implement controller and Httprequesthandler interface, can be executed.

1.2 Processor Mapper

The mapper used in the previous section is: Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping. That

This mapper can also be seen by name, and is based on the Bean's name as a URL, so when configuring the handler bean, configure the Name property, set to the URL to be requested to correctly map.
There is also a processor mapper called a simple mapper: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping. The mapping method is directly based on the URL, as follows:

This can be directly with the URL, and then the corresponding URL to specify a handler, using the handler corresponding to the bean's ID, so long as the ID in the bean, and then the URL and the ID corresponding to the.
After testing, these two mappers can also be used together, not conflicting, which to match to which.

2. How to Annotate 2.1 processor Adapters and Mappers

To use before spring3.1:

Note Mapper: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
Note adapter: Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

Used after spring3.1:

Note Mapper: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
Note adapter: Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

There is no doubt that it is now more than version 4, there must be the latter, if you use the annotated way of the adapter and mapper, there is no need for the previous non-annotated adapter and mapper, so only two lines of code can be:

We can see that there is a handler on it, so what about the URL mapping? How do I know which handler to match? No hurry, these are all given to the annotations! Here's how to write this handler:

@Controller Public  class ItemsController3 {    //Commodity query list    //@RequestMapping Implement mapping of Queryitems methods and URLs, one method corresponds to a URL    //General recommendation to write URLs and methods as    @RequestMapping("/queryitems") PublicModelandviewQueryitems()throwsException {//In practice, call the service lookup database and query the product list, where static data is used directly to simulateList<items> itemslist =NewArraylist<items> ();//Populating the list with static dataItems Items_1 =NewItems (); Items_1.setname ("Lenovo Notebook"); Items_1.setprice (6000f); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2 =NewItems (); Items_2.setname ("Apple Phone"); Items_2.setprice ( thef); Items_2.setdetail ("IPhone6 Apple phone!" ");        Itemslist.add (Items_1); Itemslist.add (items_2);//Return to ModelandviewModelandview Modelandview =NewModelandview ();//equivalent to request setattribute, fetching data through Itemslist in JSP pagesModelandview.addobject ("Itemslist", itemslist);//Specify ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp");returnModelandview; }@RequestMapping("/QUERYITEMS2") PublicModelandviewQUERYITEMS2()throwsException {//In practice, call the service lookup database and query the product list, where static data is used directly to simulateList<items> itemslist =NewArraylist<items> ();//Populating the list with static dataItems Items_1 =NewItems (); Items_1.setname ("Lenovo Notebook 2"); Items_1.setprice (6000f); Items_1.setdetail ("ThinkPad T430 Lenovo notebook computer! "); Items items_2 =NewItems (); Items_2.setname ("Apple Phone 2"); Items_2.setprice ( thef); Items_2.setdetail ("IPhone6 Apple phone!" ");        Itemslist.add (Items_1); Itemslist.add (items_2);//Return to ModelandviewModelandview Modelandview =NewModelandview ();//equivalent to request setattribute, fetching data through Itemslist in JSP pagesModelandview.addobject ("Itemslist", itemslist);//Specify ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp");returnModelandview; }}

From the above code can be seen, first of all, I can write a handler several methods, now is two, the logic is exactly the same, is the name of the item changed a little bit good for testing. With the @controller annotation above the class, you can specify the name of the bean or not, and the default is the first letter of the class name lowercase. Then the URL mapping is given to the @requestmapping class processing, you can specify the URL, a URL corresponding to a method.
This is very clear, the foreground passed a URL in, the mapper to find handler based on this URL, and then choose an adapter according to Handler to execute, here all with the annotations, so that the URL and the handler to connect together, Also the URL and specific processing methods linked together, this shows that the use of annotations in the development of a more convenient way. The way in which annotations are commonly used in development.

2.2 Continue to refine annotations

Note One problem with annotations is that the annotations adapter and mapper must be paired, that is, not one with annotations and one with non-annotations. Use it together, or you don't have to. In fact, there are more simple annotations in SPRINGMVC:

As long as this one annotation, the adapter and mapper can omit the unworthy. In addition, if the controller is a lot of, we have to match the controller with a number of beans, also inconvenient, so SPRINGMVC also provides a way to configure the scan package, as follows:

So in the end, using annotations, the configuration of adapters, mappers, controllers, and so on, adds two lines of code! Get! Of course, the view parser is the same as the previous section, and you can match it yourself.
This basically summarizes the use of non-annotated and annotated SPRINGMVC in the adapter and mapper, it is obvious that in development we use the most behind the note configuration, very convenient.
  

Related reading: http://blog.csdn.net/column/details/spring-mvc.html
Learning Note Source: https://github.com/eson15/SpringMVC_Study/releases/tag/version1.0

-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15

Summary of Mappers and adapters in "Springmvc learning 03" SPRINGMVC in annotations and non-annotated ways

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.