Announcement Announcement Module
Collapse
No announcement yet.
Accessing model from view, use ${model.xyz} or ${xyz} Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing model from view, use ${model.xyz} or ${xyz}

    Hi,

    I'm struggling to understand the difference between the following ways returning a model to a view from my controllers:

    Code:
    Map<String, Object> model = new HashMap<String, Object>();
    model.put(key, value);
    return new ModelAndView("aview", "model", model);
    When I use the above, I have to access the model from my JSP views as follows:
    Code:
    Value = ${model.key}
    I can also use:

    Code:
    Map<String, Object> model = new HashMap<String, Object>();
    model.put(key, value);
    ModelAndView mav = new ModelAndView("aview");
    mav.getModel().putAll(model);
    return mav;
    When I use the above, I have to access the model from my JSP views as follows (i.e. model. is not necessary):
    Code:
    Value = ${key}
    Why is this different? I know that in the second example I don't give a name to the model for instance. Does JSP lookup key/values in some sort of 'default' model that gets populated in the second example?

    What is the preferred method?

    Many thanks,
    Nes

  • #2
    Originally posted by nesyarug View Post
    Hi,

    I'm struggling to understand the difference between the following ways returning a model to a view from my controllers:

    Code:
    Map<String, Object> model = new HashMap<String, Object>();
    model.put(key, value);
    return new ModelAndView("aview", "model", model);
    When I use the above, I have to access the model from my JSP views as follows:
    Code:
    Value = ${model.key}
    What you've done here is create one entry in Spring's model map. That single entry is also a map. That's why you need to specify model (to access your map from Spring's map) then .key (to access an entry in your map).

    I can also use:

    Code:
    Map<String, Object> model = new HashMap<String, Object>();
    model.put(key, value);
    ModelAndView mav = new ModelAndView("aview");
    mav.getModel().putAll(model);
    return mav;
    When I use the above, I have to access the model from my JSP views as follows (i.e. model. is not necessary):
    Code:
    Value = ${key}
    This is because putAll() copies entries from your map into Spring's model map. The model (Spring's model map) now contains as many entries as your map has.

    Why is this different? I know that in the second example I don't give a name to the model for instance. Does JSP lookup key/values in some sort of 'default' model that gets populated in the second example?

    What is the preferred method?

    Many thanks,
    Nes
    There's no default model - just a simple map nested within a map in the first example and entries in a map in the second example.

    The preferred method AFAIK is the second one. There's no need to have an extra level of indirection through another map.

    HTH

    PUK

    Comment

    Working...
    X