Model

 

In Spring, the Model is used to send data from the backend to the front end (usually an HTML page). Think of it like a box where you put your data so it can be displayed on the webpage.

Here's how it works:

  1. Creating a Model in Your Controller: In your Spring controller method, you can use a Model object as a parameter.

  2. Adding Data to the Model: You can put data into the model using model.addAttribute("key", value);. The key is a name you choose for the data, and the value is the actual data you want to send.

  3. Accessing the Data in HTML: The data in the model will be sent to your HTML page. You can use the key to access this data and display it on the webpage.

Example:

Let's say you want to display a welcome message on a webpage.

In your Controller:



In your HTML (welcomePage.html):

                                                     

Here, model.addAttribute("message", "Welcome to our website!"); puts a message in the model, and ${message} in the HTML retrieves it to display on the page "Welcome to our website!".





Comments