SpringMVC - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki
- California
- EDG
Spring MVC is based on the huge ecology of spring technology. With the help of MVC programming concept, it implements java servlet technical specifications.
The problem with many applications is that there is a tight coupling between the objects that handle the business data and the views that display the business data. Generally, the command to update business objects is initiated from the view itself, which makes the view highly sensitive to any business object changes. Furthermore, there is no flexibility when multiple views depend on the same business object. Spring web MVC is a lightweight web framework based on Java that implements the request driven type of Web MVC design pattern, that is, using the idea of MVC architecture pattern to decouple the responsibilities of the web layer. Request driven means using the request response model. The purpose of the framework is to help us simplify development. Spring web MVC is also to simplify our daily web development.
- Spring MVC implements the core concept of ready to use MVC. It provides a large number of functions related to this mode for controllers and handlers. When adding inversion of control (IOC) to MVC, it decouples the application program and provides the flexibility of changing components dynamically through simple configuration changes. Spring MVC gives you complete control over all aspects of your application.
- Spring's Web MVC module is designed around the dispatcher servlet. Dispatcherservlet dispatches requests to handlers, performs view parsing, handles locale and topic parsing, and provides support for uploading files.
- The dispatcher servlet uses handler mapping to determine which handler should handle incoming requests. Handler mapping is only used to identify which handler is used to process the mapping of a specific URL pattern. The handler is an implementation of the controller interface with only one method, modelandview handlerequest (request, response). Spring also has some advanced handler implementations available; one of the important advanced handler implementations is simpleformcontroller, which provides functions such as binding command objects to forms and performing validation on them.
- Clear role division: dispatcher servlet, request to processor mapping, handler adapter, viewresolver, controller, validator, command object The object to which the request parameter is bound is called the command object, and the form object (the object provided by the form object for the form display and submission is called the form object).
- The division of labor is clear, and the expansion point is quite flexible, which can be easily expanded, although it is almost unnecessary;
- Because the command object is a POJO, it can be used as a business object directly without inheriting the framework specific API;
- Seamless integration with other spring frameworks is not available in other web frameworks;
- It can be adapted to support any class as a processor through handleradapter;
- Customizability, handlermapping, viewresolver, etc. can be very simple customization;
- Powerful data validation, format and binding mechanism;
- The mock object provided by spring can be used for simple unit test of web layer;
- The support of localization and theme resolution makes it easier for us to switch between internationalization and theme.
- Powerful JSP tag library makes JSP writing easier.
In addition, there are restful style support, simple file upload, contractual programming support with convention greater than configuration, zero configuration support based on annotation, etc.
The following content is writtern by Group California.
SpringMVC is a member of the Spring family. And Spring is a framework that combines the components that are popular in development now! It is used in the development layer based on MVC, similar to struts2 framework.
MVC model: It is an architectural model, which does not introduce new functions itself, but only helps us to organize the developed structure more reasonably, so as to separate the presentation from the model, process control logic, business logic invocation and presentation logic.
M stands for ModelWhat is the model? Model is data, it is dao, bean
V stands for ViewWhat is the view? Is the web page, JSP, used to display the data in the model
C stands for controllerWhat is the controller? The role of the controller is to display different data (Model) on different views (View), Servlet plays such a role.
A JavaEE web component technology is a web component executed on the server side, used to receive and process web user requests, and finally generate a response to the user dynamically. But each request only generates one thread (and there is a thread pool), lightweight. And can use many JavaEE technologies (such as JDBC, etc.). The essence is to output html stream in java code. But the presentation logic, control logic, and business logic are mixed.(Here we simply describe the core process)
User sends request
$ Requests are handled by the core controller
$ The core controller finds the mapper, and the mapper looks at the request path
$ The core controller then finds the adapter and sees which classes implement the Controller interface or the corresponding bean objectConvert, format, etc. the brought data
$ Find our controller Action and return a ModelAndView object after processing the business
Finally, use the view resolver to parse the ModelAndViewJump to the corresponding JSP / html page
In the early development of Java Web, the operations of the display layer, control layer, and data layer were all handed over to JSP or JavaBean for processing. We call it Model1:
I think this is a good question, every coin has two sides. So SpringMVC must have something which is very useful. Below we make a comparison. For these reasons, and now SpringMVC in the industry has gradually replaced Struts2 ... so we learn SpringMVC on the one hand can let us keep up with the industry's trend framework, on the one hand SpringMVC is really very easy to use!It can be said that what Struts2 can do, SpringMVC can also do ...
In order to solve the programming of database transactions that have not been processed in the persistence layer, and in order to cater to the strong rise of NoSQL, Spring MVC provides a solution:The traditional model layer is split into a business layer (Service) and a data access layer (DAO, Data Access Object). Under the Service, you can operate the data access layer through Spring's declarative transactions, and the business layer also allows us to access NoSQL, which can meet the sudden use of NoSQL, which can greatly improve the performance of Internet systems.
Features:
The structure is loose, almost all kinds of views can be used in Spring MVCLoosely coupled, separate modulesSeamless integration with Spring
This picture can clearly show the whole Framwork and how to work.
Reprint, please indicate the source: This article address: http://elf8848.iteye.com/blog/875830
1.dispatcher servlet: front-end controller, core role: receive request, response results, equivalent to forwarder, central processor, reducing the coupling between components. The user sends the request to dispatcherservlet, which is the center of the whole process control. The dispatcherservlet calls other components to process the user request and distributes it to the specific corresponding controller, so as to obtain the required business data model, which is then passed to view through dispatcherservlet to complete the page presentation; the existence of dispatcherservlet reduces the coupling between components 。
-
Handlermapping: processor mapper function: find the corresponding handler according to the request URL, help dispatcherservlet find the corresponding controllerhandlermapping, which is responsible for finding the handler or processor according to the user request. Spring MVC provides a variety of different mappers to implement different mapping methods, such as: configuration file method, implementation interface method, annotation method, etc.
-
Handlerinterceptor: the handlerinterceptor before and after the execution of the handler handlerinterceptor is an interface, which contains three methods: prehandle, posthandle and aftercompletion, which are executed before, during and after the execution of the handler.
-
Handlerexecutionchain: handlermapping the execution chain returned to the dispatcherservlet handlermapping is returned to the dispatcherservlet There are not only handlers, but also handlerinterceptorprehandle - > controllermethod - > posthandle - > aftercompletion. How is this chain implemented? The reflection mechanism of Java is used: reflection
-
handleradapter: processor adapter function: adapts various controllers to the handler that can be used by dispatcherservlet, executes the handler through specific rules (the rules required by handleradapter), and executes the handler through handleradapter. This is the application of adapter mode, and more types can be implemented through extension adapter To execute.
-
Handler: processor (need to be developed by engineers). Note: when writing a handler, you need to do what the handleradapter requires, so that the handleradapter can correctly execute. The handlerhandler is the background controller following the dispatcherservlet front-end controller, and processes user requests under the control of the dispatcherservlet. The handler involves business requirements, so engineers need to focus on users Requirements are developed, and finally business data is returned.
-
Modelandview: a representation of model in spring MVC. There are models and maps in spring MVC, but spring MVC will convert them into modelandview. Both models and maps are the concrete representation of modelandview.
-
Viewresolver: View resolver function: perform view resolution, and resolve them into real viewviewres according to the logical view name The resolver is responsible for generating view view from the processing results. The viewresolver first resolves the processing results into specific page address according to the logical view name, then renders the view, and displays the processing results to the user through the page. Spring MVC provides many types of view views, including jstlview, freemarkerview, pdfview, JSP, HTML, etc.
-
View: view (requires engineers to develop JSP and HTML) view is an interface, and implementation classes support different types (JSP, HTML, FreeMarker, PDF, etc.)