Is a framework for building web applications in Java based on the MVC pattern and levaraging features of Spring Core (CI, DI)
A big picture of how the proces flows:
- We have a request coming from the browser which it'll encounter the Spring MVC front controller, which is known as DispatcherServlet.
- The Spring MVC front controller will create a Model and it will delegate the request to a controller.
- The controller contains the business logic.
- The controller will send the model back to the front controller which will pass that model to the view template.
- The view template is basically a HTML page or a JSP page that will take the data and render a response to the browser.
As a developers we have to create:
- Model objects (Green)
- View templates (Red)
- Controller classes (White)
When the front controller has a request it delegates the request to the controller. The controller handles the request (maybe reading a form data), stores o retrieves data from the database or web service and places data in the Model to pass it to the view template.
Contains the data. Data can be any object / collection.
Spring MVC is flaxible, it supports many view templates. The most commons are JSP (Java Server Pages) + JSTL (JSP standard tag library). So the model data comes over the template so the JSP page can read the model data and display it.
Add configurations to file: WEB-INF/web.xml
- Configure Spring MVC Disptcher Servlet
- Set up URL mappings to Spring MVC Dispatcher Servlet.
Add configurations to file: WEB-INF/my-app-servlet.xml
- Add support for Spring component scanning
- Add support for conversion, format and validation
- Configure Spring MVC View Resolver
- Create a controller
Annotate a class with @Controller
-
Define a controller method
-
Add request mapping to controller method
Annotate a class with @RequestMapping("/")
- Return view name
- Develop view page
The model is a container for the application data. In the controller we can put anything in the model (strings, objects, info from database, etc.). Then, the view page (JSP) can access data from model.