Spring MVC 1-0-1

In spring MVC, the model is a simple map encapsulated in the Model or ModelAndView classes of Spring MVC. It can come from a database, files, external services, and so on. It is up to you to define how to fetch the data and put it into the model. The recommended way of interacting with the data layer is through Spring Data libraries: Spring Data JPA, Spring Data MongoDB, and so on. There are a dozen projects related to Spring Data and I encourage you to take a look at http://projects.spring.io/spring-data.

The controller side of Spring MVC is handled through the use of the @Controller annotation. In a web application, the controller's role is to respond to HTTP requests. Classes annotated with the @Controller annotation will be picked up by Spring and given a chance to handle upcoming requests.

Via the @RequestMapping annotation, Controllers declare handling specific requests based on their HTTP method (GET or POST methods, for instance) and their URLs. The Controller then decides to either write content directly in the web response or route the application to a view and inject properties into that view.

A pure RESTful application would choose the first approach and expose a JSON or XML representation of the model directly in the HTTP response with the @ResponseBody annotation. In the case of a web application, this type of architecture is often associated with a frontend JavaScript framework such as Backbone.js, AngularJS, or React. In this case, the Spring application would then only handle the Model layer of the MVC model. We will study this kind of architecture in Chapter 3, File Upload and Error Handling.

With the second approach, the Model is passed to the View, which is rendered by a templating engine and then written to the response.

The view is often associated with a templating dialect, which will allow navigation inside the model. Popular dialects for templating are JSPs, FreeMarker, or Thymeleaf.

Hybrid approaches may take advantage of the templating engine to interact with some aspects of the application and then delegate the view layer to a frontend framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset