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:
Creating a
Modelin Your Controller: In your Spring controller method, you can use aModelobject as a parameter.Adding Data to the
Model: You can put data into the model usingmodel.addAttribute("key", value);. Thekeyis a name you choose for the data, and thevalueis the actual data you want to send.Accessing the Data in HTML: The data in the model will be sent to your HTML page. You can use the
keyto 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 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
Post a Comment