1. Introduction to the experiment
--------------------------------------------------------------------------------------------------------------- --------------
1.1 Experimental environment
- Tomcat 7.0.72
- JDK 1.8
- IntelliJ Idea 2017.2
- Firefox
1.2 Experimental Knowledge points
- Spring MVC Execution Process
- Annotations
- Parameter binding
- Data binding
- Hibernate-validator
--------------------------------------------------------------------------------------------------------------- --------------
2. Experiment Introduction to 2.1Spring MVC execution process
1, Dispatcherservlet
Front-end controller, all requests are distributed uniformly, and requests are assigned to the corresponding handler
2. Handlermapping (Processor mapper)
Parse the request link and find the class that executed the request based on the request connection
3, Handleraddapter (processor adapter)
Call a specific method to process a request from a user
4. Controller
The controller processes the user request and the controller finishes processing the user request, returning the Modelandview object to the Dispatcherservlet front-end controller.
From a macro point of view, Dispatcherservlet is the controller of the entire WEB application, and from the microscopic perspective, the controller is a single Http request during the process of processing controllers.
5. Viewresolver (View parser)
Parses the logical view name in Modelmdoelandview into a real view object and takes the Model out of the Mdoelandview
2.2 Common Annotations @controller
In spring MVC, the controller Controoller is responsible for handling requests for dispatcherservlet distribution, which encapsulates the data requested by the user into a model after processing it through the business processing layer, and then returns the model to the corresponding view for display.
2.3 Common Annotations @requestmapping
@RequestMapping annotations are used to process request address mappings, indicating which class or method spring uses to process requests
Common Properties:
Value: Specifies the actual address of the request, that is, the Value property maps the URL to the method
@RequestMapping (value= "/register") @RequestMapping ("/register")
Method: Specifies the type of method to be requested, that is, the way in which HTTP requests can be processed by the methods, such as Get,post
@RequestMapping ("/register", Method=requestmethod.post)
Consumes: Specifies the commit content type (Content-type) for processing the request, such as Aoolication/json
@RequestMapping ("/register", method=requestmethod.post,consumes= "Application/json")
Produces: Specifies the type of content to return, only if the request type contains the specified type.
@RequestMapping ("/register", method=requestmethod.post,produces= "Application/json")
Params: Specifies the parameter values that must be included in the request
@RequestMapping ("/register", method=requestmethod.post,params= "Myparam=value")
Header: Specifies that the specified header value must be included in the request
@RequestMapping ("/register", method=requestmethod.post,headers= "referer=http://www.shiyanlou.com")
2.4 Common School Check notes
Here is a description of the main validation notes for Hibernate-validator:
--------------------------------------------------------------------------------------------------------------- --------------
3. Experimental process 3.1 new project engineering
Under Web-inf, create the Lib folder, download the jar package to the Lib folder, right-click Add as Libiary
3.2 Configuring the Web. xml file
3.4springmvc-servlet.xml file
Create a new spring MVC configuration file under the web/web-inf/directory
3.5 Creating an Entity class
In the project directory src package com. Springmvc.entity the new class User.java, including the username,password,age and email attributes, the code is as follows:
1 Packagecom. springmvc.entity;2 3 Public classUser {4 PrivateString username;5 PrivateString password;6 Private intAge ;7 PrivateString Email;8 9 PublicUser () {}Ten PublicUser (string Username, string password,intAge , String email) { One This. Username =username; A This. Password =password; - This. Age =Age ; - This. email =email; the } - - PublicString GetUserName () { - returnusername; + } - + Public voidSetusername (String username) { A This. Username =username; at } - - PublicString GetPassword () { - returnpassword; - } - in Public voidSetPassword (String password) { - This. Password =password; to } + - Public intGetage () { the returnAge ; * } $ Panax Notoginseng Public voidSetage (intAge ) { - This. Age =Age ; the } + A PublicString Getemail () { the returnemail; + } - $ Public voidsetemail (String email) { $ This. email =email; - } -}
Implementation of the 3.6Controller class
In the project directory src package com. Springmvc.controller the new class Usercontroller.java, the code is as follows
"Code Learning" Spring MVC implements the user registration function