I. Introduction to Spring WEB MVC 1.1, MVC pattern
*m-model model
The role of model is to be responsible for business logic. Consists of two tiers: business data and business processing logic. For example, entity class, DAO, service all belong to model layer.
*v-view View
View is responsible for displaying the interface and user interaction (collecting user information). A component that belongs to a view is a JSP that does not contain business logic and control logic.
*c-controller Controller
A controller is a bridge between the model layer m and the view layer V for controlling processes such as single controller Actionservlet in a servlet project.
1.2. What is Spring Web MVC
Spring Web MVC is a very important functional module of the spring Framework. The MVC structure is implemented to facilitate the simple and rapid development of the MVC structure of the Web program. The API provided by Spring Web MVC encapsulates the functionality commonly used in web development, simplifying the web process.
1.3. Core components of Spring WEB MVC
Spring WEB MVC provides the main implementation components related to M, V, and C, as follows
*dispatcherservlet (Controller, request entry)
*handlermapping (Controller, request dispatch)
*controller (Controller, request processing flow)
*modelandview (model, encapsulating Business processing results and views)
*veiwresolver (view, view display processor)
1.4. Spring WEB MVC processing Flow
The main processing flow for Spring Web MVC is as follows
* The browser makes a request to spring and requests it to the front controller dispatcherservlet processing.
* The controller handles the request through handlermapping to find the appropriate controller component.
* The Execute Controller component contract method processes the request, and the contract method invokes the model component to complete the business processing. The contract method can return a Modelandview object that encapsulates the processing result data and the view name information.
* After the controller receives Modelandview, call V I resolve let myself, position the view (JSP) and pass the data information, generate the response interface result.
Second, the MVC application based on XML configuration 2.1, build the spring WEB MVC Environment
The main steps to build the spring WEB MVC development environment are as follows
* Create Web project, import Spring Web MVC related development package
such as the Spring API, Web, WEBMVC and other development packages
* Add spring XML configuration file under SRC
Names can be customized, such as Spring-mvc.xml
* Configuring the Dispatcherservlet front-segment controller component in Web. xml
The Dispatcherservlet component is already available in spring MVC and only needs to be configured.
When configuring Dispatcherservlet, specify an XML configuration file as well.
2.2, Dispatcherservlet Controller configuration
2.3. handlermapping Components
The Handlermapping component allows the Dispatcherservlet controller to map client HTTP requests to controller components.
*simpleurlhandlermapping
maintains an HTTP request and Controller mapping relationship list (map), invoking the controller based on the list correspondence.
*requestmappinghandlermapping
Requestmappinghandleradapter
The controller classes and methods use the @requestmapping annotation to specify the corresponding client HTTP request (which is described in the subsequent Annotation configuration section).
2.4. Controller Components
The controller component is responsible for performing specific business processes, invoking components such as DAO, and requiring controller excuses and agreed methods when writing.
2.5. Modelandview Components
The Handlermapping method that is contracted by the controller component returns a Modelandview object that encapsulates the model data and the view name response information. The Modelandview constructor is as follows
Modelandview (String viewName)
Modelandview (String Viewname,map model)
ViewName is the name of the JSP page
The model data is stored in the attribute of the request
2.6. Viewresolver Components
All controller components return a Modelandview instance that encapsulates the view name. The view in spring is identified by a name, and the view resolver Viewresolver parses the view by its name.
Spring provides a variety of view parsers, as follows
Examples of internalresourceviewresolver use are as follows
such as: View name hello through the above configuration can be mapped to/web-inf/jsp/hello.jsp
Application of MVC application 3.1, @RequestMapping annotation based on annotation configuration
@RequestMapping can be used on class definitions and method definitions.
@RequestMapping tag This class or method corresponds to which customer request
@Controller @requestmapping ("Day01")publicclass hellocontroller{ @ Requestmapping ("/hello.form") public String execute () threws exeception{ return "Hello"; } }
* Open the @requestmapping annotation map, You need to define the requestmappinghandlermapping (before class definition) and the requestmappinghandlermapping (before the method definition) two bean components in the spring XML configuration file.
Requestmappinghandlermapping and requestmappinghandleradapter Two bean component definition examples
Starting with the spring3.2 version, you can use the following XML configuration to simplify
Requestmappinghandlermapping and Requestmappinghandleradapter definitions
< Mvc:annotation-driven />
3.2. @Controller Annotation Application
It is recommended to declare the controller component using the @controller annotation, which makes the controller definition more flexible, without implementing the Controller interface, and the method of request processing can be flexibly defined.
@Controller @requestmapping ("/day01") Public class hellocontroller{ @RequestMapping ("/hello.form") publicthrows exception{ return "Hello"; } }
For the @controller annotation to take effect, you need to turn on the component scan definition in the spring XML configuration file and specify the package that contains the controller component.
<base-package= "Com.tarena.controller"/>
Spring Web MVC Basics