Explanation on spring mvc ModelMap

public String addStudent( @ModelAttribute("HelloWeb")Student student, ModelMap model){
}

      

Can someone explain how this works?

what use ModelMap model

?

+3


source to share


2 answers


The architectural pattern used in web applications is MVC, which stands for Model, View and Controller. The model stores data. The view is your display and the controller controls the flow.

The model is usually a simple object that can be displayed on the screen. For example, simple custom update settings would contain data that could be contained in a domain or model object called a user.

However, when views and interactions become complex, a simple object may not be sufficient. And a complex object is needed. This somewhat complex object contains several other objects. For example, a page such as a user's news dashboard might need to store data;



  • user (name, etc.) stored in some custom object
  • user preferences, stored in a userpreference object, which in turn can be part of the user
  • Some other objects that may not be entirely appropriate for the content in the user object itself.

In principle, all of these objects can make the model somewhat complex. Therefore, for better organization, they can be stored as pairs of name values โ€‹โ€‹and packaged inside one Map. This way, the page can reference the required keys and get the object to render.

ModelMap is a container object

+2


source


ModelMap

subclasses LinkedHashMap

.

public class ModelMap extends LinkedHashMap{

}

      

Model

is an interface. ModelMap

represents an implementation Model interface

.

Basically, when you have data in your code and you want to make that data available for your page to render jsp

, you need to put that data somewhere in order for it to be available. For this you need Model

. To store this data obtained in code. It's just a glorified card.



So I have a form with a backing object with data. The form has three drop-down lists, which are also required to fill in the drop-down lists. These are 4 different objects. 3 Lists for dropdowns and one object for shape support. If my code gets all this data, I put it in the map Model

and now I can use it in my page jsp

.

refer to this for more information.

also see here for details.

+4


source







All Articles