Spring MVC Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 16: Line 16:
=Controller=
=Controller=


A controller is a class that handles requests and responds with information of some sort. In case of a browser-facing application, a controller responds by optionally populating model data and passing the request to a view that produces HTML to be returned to the browser. The view is indicated by its [[#View_Logical_Name|logica name]], which is returned by the method.
A controller is a class that handles requests and responds with information of some sort. In case of a browser-facing application, a controller responds by optionally populating model data and passing the request to a view that produces HTML to be returned to the browser. The view is indicated by its [[#View_Logical_Name|logical name]], which is returned by the method.


[[@Controller]] [[@GetMapping]]
[[@Controller]] [[@GetMapping]]

Revision as of 00:58, 11 October 2018

Internal

To Process

TO PROCESS:

Controller

A controller is a class that handles requests and responds with information of some sort. In case of a browser-facing application, a controller responds by optionally populating model data and passing the request to a view that produces HTML to be returned to the browser. The view is indicated by its logical name, which is returned by the method.

@Controller @GetMapping

@Controller
public class HomeController {

    @GetMapping("/")
    public String home() {

        // returns the view name
        return "home";
    }
}

View

View Logical Name

Project Directory Layout

src/main/resource/static
src/main/resource/static/images

Testing MVC Applications

@WebMvcTest

MockMvc

REST Clients

RestTemplate

TO PROCESS: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#webmvc-resttemplate

POSTing Resource. Data

This overloaded version allows you to receive the newly created resource as a domain model object:

RestTemplate restTemplate = new RestTemplate();

MyResource model = new MyResource(...);

MyResource created = restTemplate.postForObject("http://localhost:8080/myresource", model, MyResource.class);