In Spring, ModelAndView is like a combined package that helps you send both data and the view (page) name from your controller to the front end. It wraps together:
- The View Name: The name of the page (like
homePage) where you want to display the data. - The Model Data: The data you want to send to that page.
How It Works:
- Creating
ModelAndView: You create an object ofModelAndViewin your controller. - Setting the View: You set the page name (view) you want to display using
setViewName(). - Adding Data: You add data using
addObject(), which works likeaddAttribute()inModel.
Example:
Let's say you want to display a welcome message on a page called welcomePage.
In the Controller:
In the HTML (
welcomePage.html):


Comments
Post a Comment