Relationship and the distinction between SpringMVC the Map, Model, ModelMap, ModelAndView

  First, before understanding these three, you need to know: SpringMVC creates an implicit data model (Model) before calling the method, as the storage container model data, a "hidden model."
That is when the front and back every request that comes in will be a backpack (Model) , whether you do not use this backpack does exist, we request for holding the value of interactive delivery.

  When the front end of the request to the background, Model ModelMap and examples are spirng mvc framework to automatically create and passed as a parameter controller, users do not need to create themselves.

Whether Mode or ModelMap bottom are using BindingAwareModelMap, so basically no difference between the two, we can just take it and use. And then we need to manually ModelAndView new, springMVC will not help us create.

  Since these four classes or interfaces are together contrast, then there must be similarities between them:

  1. The same effects thereof, are used to transfer control method for data stored in the Display Pages (JSP page), via EL or byC tag library to obtain {name}, to be the object key values.

  The difference between them:

  1.Map, Model, ModelMap we do not need to manually create, springmvc framework automatically created and passed as a parameter controller; and ModelAndView need to be created manually.

  2.Model used to transmit data only, and will not be addressed service. ModelAndView service it may be made addressable, static files is set corresponding to the request, static files here refer to similar jsp file.

 

Example. 1: the Model and ModelMap

Examples Model and ModelMap are spirng mvc framework automatically created and passed as a parameter controller, users do not need to create their own.

And the need to return to return to the page specified path.

Code control layer:

 // pass the value of a Method 
    @RequestMapping ( "listCategory2" )
     public String listCategory2 (the Model Model) { 

        // information received query 
        List <the Category> CS2 = categoryService.list ();
         // encapsulates data queries 
        model.addAttribute ( "the Test" , CS2);
         // important! ! Need to give a return path model jump 
        return "listCategory2" ; 
    }

JSP page:

<! - get the value when the first parameter corresponds to the addAttribute! Take the individual named C -> 
    < C: forEach items = "$ {Test}" var = "C" varStatus = "ST" > 
        < TR > 
            < TD > $ {c.ID} </ TD > 
            < TD > $ {c.NAME} </ TD > 
        </ TR > 
    </ C: forEach >

 

Example 2: ModelAndView

Examples ModelAndView that we need new manual, which is a difference and ModelMap of.
Moreover, ModelAndView can own address, only to return to return to its target.

java code:

// value-passing two: ModelAndView
     // when url is the time, by a method for processing the request listCategory 
    @RequestMapping ( "listCategory" )
     public ModelAndView listCategory () {
         // create a model view object 
        ModelAndView MAV = new new ModelAndView () ;
         // Get the data query 
        List <the Category> CS = categoryService.list (); 

        //  // to place data into ModelAndView object in view, the second parameter may be any type java 
        mav.addObject ( "CS" , CS);
         // put jsp path 
        mav.setViewName ( "listCategory" );
          // returns ModelAndView object MAV 
        return MAV; 
    }

JSP code:

 <! - the use of c tag library, data loop through the named c -> 
    < c: forEach items = "$ {CS}" var = "c" varStatus = "ST" > 
        < TR > 
            < TD > $ c.ID} { </ TD > 
            < TD > $ {c.NAME} </ TD > 
        </ TR > 
    </ C: forEach >

These are two different ways the value of traditional values, from the above we can see that there are still some differences between the two, remember that there is a need to return the object, a path needs to be returned is the address.

 

A brief summary:

Map to store the object key native, naturally inside the lot of methods may be used.

Model only a handful method is only suitable for storing data, and to simplify the understanding of the operation novice Model object;

ModelMap inherited LinkedMap, in addition to their own methods to achieve some of the same inherited methods and properties of LinkedMap;

Nature is no big difference between Map, Model, ModelMap.

ModelAndView can store data simultaneously, can be provided to return the logical view, the display layer jump control.

Guess you like

Origin www.cnblogs.com/ethan-37/p/12638427.html