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:
long id: that is extract from the URL of the request, something like /reward/show?id=1
The Model model object: I think that it contains the values to sharw with the view.
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:
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.
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
Aucun commentaire:
Enregistrer un commentaire