AndreaNobili
AndreaNobili

Reputation: 42967

Some doubts related to Spring MVC model object

I am studying Spring MVC and I have some doubts related the concept of model.

So if I have a controller like this:

@Controller
public class RewardController {

    private RewardLookupService lookupService;

    @Autowired
    public RewardController(RewardLookupService svc) {
        this.lookupService = svc;
    }

    @RequestMapping("/reward/show")
    public String show(@RequestParam("id") long id, Model model) {
        Reward reward = lookupService.lookupReward(id);
        model.addAttribute(“reward”, reward);
        return “rewardView”;
    }
}

So, into this controller is definied the show() method that handle HttpRequest toward the /reward/show path and take 2 input parameters:

So this controller method perform a query on the DB and obtain a Reward object that put into the Model object.

So I can have this simple view named for example rewardView.jsp(the name and the path is automatically build by the Spring view resolveréé) that show the content of the **Model object:

<html>
    <head><title>Your Reward</title></head>

    <body>
        Amount=${reward.amount} <br/>
        Date=${reward.date} <br/>
        Account Number=${reward.account} <br/>
        Merchant Number=${reward.merchant}
    </body>
</html>

So my doubts are:

  1. Is the Model object a specific implementation of a Java Map or is an object that wrap a Map? I think so because, as in a Map, I have a couple where the KEY is the field name and the VALUE is its specific value to show in the view.

  2. If my previous reasoning is correct the addAttribute() is a specific Spring method to put an element into this Map? why is not used directly the Map put() method?

Tnx

Upvotes: 1

Views: 153

Answers (3)

Harshal Patil
Harshal Patil

Reputation: 6759

The model (the M in MVC) is a Map interface, which allows for the complete abstraction of the view technology. You can integrate directly with template based rendering technologies such as JSP, Velocity and Freemarker, or directly generate XML, JSON, Atom, and many other types of content. The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model.

Upvotes: 2

SchonWieder
SchonWieder

Reputation: 213

Model is actually an interface, which declares the addAttribute method you mention. In your Show method you are effectively using this interface to access a Map.

The "put" methods are not available through the Model interface, so you instead need to use addAttribute (which performs basic sanity checks on your key/value arguments and then "puts" them into the underlying map for you.

Upvotes: 3

kiranreddykasa
kiranreddykasa

Reputation: 199

ExtendedModelMap is the implementation of Model object.

This class is extending ModelMap which itself is extending HashMap.

And addAttribute name is more meaningful as we are using it in servlet/web environment.

Upvotes: 2

Related Questions