Spring MVC learning notes (sorting), springmvc

Source: Internet
Author: User

Spring MVC learning notes (sorting), springmvc
SpringMVC Learning 1. Overview

Spring MVC is a lightweight Web framework that implements the MVC Design Pattern Based on Java. It uses the MVC Architecture Pattern to decouple the web layer, the request-response model helps us simplify the development of daily web systems.

Spring MVC Framework is an MVC framework. The front-end controller is DispatcherServlet, which is mainly used to control the process. The application controller is the Handler Mapping-processor ER for processor management and View Resolver-View Parser for View resolution; the page Controller/Action jump processor is a Controller interface. It supports local parsing and file upload. It provides flexible data verification, formatting, and data binding mechanisms; the contract programming method is better than the configuration.

The Spring MVC framework is centered around the DispatcherServlet core. DispatcherServlet intercepts requests and organizes a series of components to complete request processing.

2. Features of Spring MVC

L easy to design a clean web layer.

L integration with the Spring framework (IOC container, AOP, etc ).

L provide contractual programming support that is better than the configuration.

L faster and simpler unit testing.

L flexible URL-to-controller ing.

L supports the Rest style.

L ...........................

3. Spring MVC Architecture

The Spring MVC framework is also a driver-based MVC Framework. By implementing the Model-View-Controller mode, data, services, and presentation are well separated. Spring Web MVC is designed around the front-end Controller DispatcherServlet. DispatcherSerlvet is responsible for distributing requests to the corresponding Controller according to the ing rules ), process the request according to the configurable Handler ings and go to the corresponding view. The overall processing process of Spring MVC is as follows:

 

Figure 3-1 Spring MVC processing process

 

The general execution process of Spring MVC is as follows:

1. First, the browser sends a request to the front-end controller DispatcherServlet. Based on the request information, DispatcherSerlvet selects an appropriate controller to process the request and delegates the request to it.

2. After receiving the request, the page controller must collect and bind the request parameters to an object (command object) for functional processing, then, the object is delegated to the Business Object for processing (service layer). After the business object is processed, the Controller returns a ModelAndView (model data and logic view name );

3. Based on the returned logic view name, DispatcherServlet selects an appropriate view for rendering (interface display and resource loading) and passes model data into the view for rendering.

4. The front-end controller returns a response to a client browser.

4. helloworld of Spring MVC

1) Prepare the jar package

 

2) file structure

 

3) web. xml

Configure DispatcherServlet in the web. xml file and add the following fragments to the web. xml file:

 

4) Spring configuration file hello-servlet.xml

In this file, you can define various types of beans required by Spring MVC.

5) controller class HelloController

 

First, use the @ Controller annotation to identify the class as a Controller, and then use the @ RequestMapping annotation to specify which requests sayHello processes. In this example, the sayHello method only processes GET/hello requests.

The sayHello method receives a parameter Model of the org. springframework. ui. model type. SpringMVC automatically encapsulates the request parameters into the model. We can simply understand the model as a Map. In the method, we extract the value of the person parameter from the model and print it out. Next, we add an attribute msg to the model with the value "Hello, World! ", And then return the view name hello.

6) configure a view parser in the Spring MVC configuration file.

 

Added the mvc namespace, enabled spring Automatic scanning, and set the default annotation ing support.

The bean in the configuration file must be explained here. It is the most commonly used view parser in Spring MVC. Of course, there are many other types.

The prefix attribute indicates the view prefix, and suffix indicates the view suffix. jsp, we return hello in the Controller Method sayHello, combined with the configuration here, the corresponding complete view is:/WEB-INF/jsp/hello. jsp.

7) hello. jsp

 

 

8) demo

 

5. SSH: Spring MVC + Spring + hibernate

1) Prepare the jar package

Here

2) file structure

 

3) web. xml

 

Character Set Configuration

 

4) spring-bean.xml


Spring-common.xml (5)

 

6) Background implementation

Class implementation in Dao, entity, and manager packages is omitted.

The implementation of UserController. java code is as follows:

7) index. jsp

 

8) bind object classes and database tables through annotations

 

2. spring-common.xml


6. Spring MVC configuration details

 

 

The above four method examples show that a Controller contains different request URLs. You can also use a url to access different methods by using url parameters.

 

In fact, RequestMapping can be viewed as a parent Request url in the Class, while RequestMapping can be viewed as a sub-Request url in the method, the parent-child request url will eventually match the page request url. Therefore, the RequestMapping can also be written as follows:

 

Commonly used annotations in SpringMVC include @ PathVariable, @ RequestParam, and @ PathVariable. The parameters marked by @ PathVariable can be passed through the Request Path. See the following example.

 

7. AJAX + JSON

A) page display

 

B) respond to events

 

C) effect demonstration

 

D) Background implementation

 

E) jar packages

 

Note: The content of this note has been referred to in some online articles. The content of this note has been written for a long time and the specific source has been forgotten. If the original author sees this article as infringing, contact me, I will mount the reference link. Over


How to Learn spring mvc

Www.verycd.com/
Search Shang xuexiang

Learning Struts MVC and Spring MVC

MVC is the spring of Struts, and the integration of Struts using Struts MVC does not seem to mention MVC.

Website CSDN books and other books can also be understood as long as you can understand

Related Article

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.