Application of 1.SpringMVC frame 1.1 MVC in B/s system
- User sends request to controller
- Controller request model for processing
- Model returns the processing result to the controller
- Controller accepts processing results from model send
- Render the view, populate the model data in the request domain (view)
- Responding to a user's request
1.2 Springmvc Frame
- Initiating a request to a front-end controller (dispatcherservlet)
- Front Controller request Handlermapping Find handler
(Can be found based on XML configuration, annotations)
- Processor Mapper Handlermapping back to the front-end controller Handler
- The front controller invokes the processor adapter (Handleradapter) to perform the handler
- Processor Adapter Execution Handler
- Handler execution completes to adapter return Modelandview
- Processor Adapter returns Modelandview to the front-end controller
Modelandview is an underlying object of the SPRINGMVC framework, including model and view
- The front controller requests the view resolver to parse the view and parse it into a real view (JSP) based on the logical view name.
- View Resolver returns to Front controller view
- Front controller for view rendering, view rendering populates model data (in the Modelandview object) into the request domain
- Front-end controller responds to user results
1.3 Components
- Front Controller Dispatcherservlet (no program Ape development required)
function accept request, response result, equivalent to transponder, CPU
With the Dispatcherservlet, the coupling of other components is reduced.
- Processor Mapper handlermapping (no program Ape development required)
Function: Find handler based on the URL requested
- Processor Adapter Handleradapter (no program Ape development required)
Function: Execute handler According to the specific rules (Handleradapter requirements)
- Processor handler (requires program Ape development)
Function: Write handler in accordance with handleradapter requirements to do, so that the adapter can go to the correct execution handler
- View Resolver views Resolve (no program ape development required)
Function: Perform a view resolution, resolve to a real view based on the logical View name (view)
- Views view (Requires program Ape development)
View is an interface that implements classes that support different view types (JSP, Freemarker, pdf)
2. Development Step 2.1 Requirements 2.2 environment preparation
Need to spring3.2 all jars (must include Spring-webmvc-3.2.0.release.jar)
2.3 Configuring the front-end controller
Configuring the front-end controller in Web. xml
<servlet><Servlet-name>Springmvc</Servlet-name><Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class><Init-param><Param-name>Contextconfiglocation</Param-name><Param-value>Classpath:springmvc.xml</Param-value></Init-param><Load-on-startup>1</Load-on-startup></servlet><servlet-mapping><Servlet-name>Springmvc</Servlet-name><Url-pattern>*.action</Url-pattern></servlet-mapping>
2.4 Configuring the Processor adapter
Configuring the processor adapter in the Springmvc.xml under Classpath
<!---<class= " Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter "/>
2.5 Development Handler
The Controller interface needs to be implemented,
Can be performed by the Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter adapter
Public classItemsController1ImplementsController {@Override PublicModelandview HandleRequest (httpservletrequest request, httpservletresponse response)throwsException {//Call Service Lookup database, query commodity list, use static data simulation hereList<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 (5000F); Items_2.setdetail ("Iphone6 Apple phone!" "); Itemslist.add (Items_1); Itemslist.add (items_2); //back to ModelandviewModelandview Modelandview =NewModelandview (); //equivalent to the request of Setattribut, in the JSP page through itemslist fetching dataModelandview.addobject ("Itemslist", itemslist); //Specify a ViewModelandview.setviewname ("/web-inf/jsp/items/itemslist.jsp"); returnModelandview; }}
2.6 View Writing 2.7 configuration Handler
Will write handler loaded in the Spring container springmvc.xml
<!---<name= "/items1.action" ID = "ItemList1" class = "Cn.itcast.springmvc.controller." ItemsController1"/>
Name= "/items1.action": The processor mapper configured in front of Beannameurlhandlermapping will successfully map to the ITEMLIST1 controller if the requested URL is "context/items1.action".
2.8 Configuring the Processor Mapper
Configuring the Processor mapper in the Spring.xml under Classpath
<!---<!---<class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
2.9 Configuring the View resolver
You need to configure the view parser to parse the JSP.
"SPRINGMVC" Study notes