The previous section first explains the configuration of the front-end controller dispatcherservlet, including loading SPRINGMVC files, blocking what requests, etc.
There are also two components: Adapter and Mapper (plus one additional set)
Non-annotated Processor Mapper Processor mapper: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping another mapper: Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Add: Multiple mappers can coexist, the front controller determines which mapper the URL can map, and let the correct mapper handle it.
Non-annotated processor adapter: Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter requires written handler to implement the Controller interface. Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter requires written handler implementation Httprequesthandler interface.
Someone may not have configured the above found programs to run. Then the knowledge points came:
The first knowledge point in this chapter is a configuration file within the SPRINGMVC framework in a configuration file, and its role is to use a set of adapters and mappers by default when no configuration is configured on the above adapter and processor framework.
Let's take a brief look at the contents of this file:
First look at the following translation: # Default Implementation Classes forDispatcherservlet ' s strategy interfaces.# used as fallback when no matching beans is found in the Dispatcherservlet context. # Not meant to being customized by Application developers. Org.springframework.web.servlet.LocaleResolver=Org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver Org.springframework.web.servlet.ThemeResolver=Org.springframework.web.servlet.theme.FixedThemeResolver This is the default loaded processor mapper has two: the first one is based on the name of the Bean mapper URL, The second is the default annotation processor mapper (which is then annotated) org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.beannameurlhandlermapping,\ Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping This is the default loaded processor adapter has three: HTTP request-related, Simple processor Adapter, default note processor adapter (and so on, will speak of annotations) Org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.httprequesthandleradapter,\ Org.springframework.web.servlet.mvc.simplecontrollerhandleradapter,\ Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter Org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.annotationmethodhandlerexceptionresolver,\ Org.springframework.web.servlet.mvc.annotation.responsestatusexceptionresolver,\ Org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver Org.springframework.web.servlet.RequestToViewNameTranslator=Org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator Org.springframework.web.servlet.ViewResolver=Org.springframework.web.servlet.view.InternalResourceViewResolver Org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.sessionflashmapmanager
The components in the above file that involve two annotations are:
Processor Adapter for annotated processor mapper org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping annotations Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter The two were used before the spring3.1 version, and after 3.1, the two components associated with the HTTP request were: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping annotation Mapper. Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter annotation Adapter.
Program configuration: Both of the annotations must appear in pairs, either all appear or do not appear
class class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
Real-time Programs:
@Controller Public classItemList3 {@RequestMapping ("/queryitem") PublicModelandview Queryitem () {//Product ListList<items> itemslist =NewArraylist<items>(); Items items_2=NewItems (); Items_2.setname ("Apple Phone"); Items_2.setprice (6088f); Items_2.setdetail ("Iphone6s Apple phone!" "); Itemslist.add (items_2); //Create Modelandview: Populate the data, set the viewModelandview Modelandview =NewModelandview (); //Populating DataModelandview.addobject ("Itemslist", itemslist);//similar to Request.setattribute ("", "" ")Modelandview.addobject ("message", "Hello world!"); //View: Logical nameModelandview.setviewname ("/itemslist");//request.getrequestdispatcher ("url"). Forward (Request,//response); returnModelandview; }}
Of course, the light configuration of the above two annotation related components is not enough why? Because the spring container does not know which classes are processors, a configuration is required to give the processor to the container management.
Knowledge Point Four:
<!--component scanners: Can scan @controller, @Service, @Repository, and more
<context:component-scan base-package= "Com.springapp.web.controller"/>
Then maybe a little lazy to say that the configuration of the two annotations related components is not convenient, or to write so long. The framework also helps us to think about it.
Knowledge Point five: real-world Development use the following configuration to replace the two annotated components configured above
<!--SPRINGMVC Use <mvc:annotation-driven>-<!-- Automatic loading of requestmappinghandlermapping and Requestmappinghandleradapter, <!--can be used in Springmvc.xml configuration files using <mvc: Annotation-driven> overrides the configuration of the annotation processor and adapter. -<mvc:annotation-driven/>
In this case, the configuration file will be configured as follows:
There is no configuration file when using annotations:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3 .2.xsd "> <!--Configuring a simple controller processing adapter - <!--Simplecontrollerhandleradapter: Simple controller processing Adapter, - <!--all beans that implement the Org.springframework.web.servlet.mvc.Controller interface are the back-end controllers of the SPRINGMVC. - <Beanclass= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <Beanclass= "Org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" /> <!--Configuring the Beannameurl processor Mapper - <!--beannameurlhandlermapping: Represents the name of the bean being defined as the URL of the request and requires that the controller being written be configured in the Spring container . - <!--the name of the specified bean is the requested URL and must end with an. Action. - <Beanclass= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <Beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" /> <!--Controller Configuration - <!--name= "/items1.action": The processor mapper configured in front is beannameurlhandlermapping, - <!--If the requested URL is "context/items1.action", it will be successfully mapped to the ITEMLIST1 controller. - <Beanname= "/items1.action"ID= "ItemList1"class= "Com.springapp.web.controller.ItemList1" /> <Beanname= "/items2.action"ID= "ItemList2"class= "Com.springapp.web.controller.ItemList2" /> <!--Configuring the View resolver - <!--Internalresourceviewresolver: Support for JSP view parsing - <!--Viewclass:jstlview indicates that the JSP template page needs to use the JSTL tag library, so the classpath must contain JSTL related jar packages; - <!--prefix and suffix: find the prefix and suffix of the view page, where the final view is: - <!--prefix + logical view name + suffix, logical view name needs to return Modelandview specified in controller, such as logical view named Hello, - <!--The JSP view address "web-inf/jsp/hello.jsp" is eventually returned - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView" /> < Propertyname= "prefix"value= "/web-inf/jsp/" /> < Propertyname= "suffix"value= ". jsp" /> </Bean></Beans>
Use the profile after annotations:
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/s Pring-mvc-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema /aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd "> <!--====================== component Scanner ====================== - <Context:component-scanBase-package= "Com.springapp.web.controller" /> <!--SPRINGMVC Use <mvc:annotation-driven> - <!--automatic loading of requestmappinghandlermapping and Requestmappinghandleradapter, - <!--use <mvc:annotation-driven> to override the configuration of the annotations processor and adapter. - <Mvc:annotation-driven/> <!--Configuring the View resolver - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView" /> < Propertyname= "prefix"value= "/web-inf/jsp/" /> < Propertyname= "suffix"value= ". jsp" /> </Bean></Beans>
Hansonq
Links: http://www.imooc.com/article/4293
Source: MU-Class Network
Springmvc from getting started to mastering the third chapter