Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBoban Talevski
24,793 PointsIs there a particular reason that we should use a Map when passing in the model?
This is what Craig does in the video in Main.java
get("/ideas/:slug", (req, res) -> {
Map<String, Object> model = new HashMap<>();
model.put("idea", dao.findBySlug(req.params("slug")));
return new ModelAndView(model, "idea-details.hbs");
}, new HandlebarsTemplateEngine());
This is what I did when working on the same task
get("/ideas/:slug", (req, res) -> {
CourseIdea idea = dao.findBySlug(req.params("slug"));
return new ModelAndView(idea, "idea-details.hbs");
}, new HandlebarsTemplateEngine());
So, he creates a Map and adds a single entry with the key "idea" and the value as the CourseIdea object returned by the findBySlugMethod and passes that Map object as the model.
When working on this, I just thought I should pass the CourseIdea object itself as the model and it seems to work. Now, I'm really curious to know if there's something wrong with my way of doing this. I mean, why pass a Map when we can simply pass the CourseIdea object itself as the model, since we are really interested in the details of this particular object only?
Also, I didn't need to use this.title, this.voters in the hbs template file, I could directly access the values with title, voters etc. since I passed the object itself. It looks a bit cleaner for me this way, but want to make sure I'm not missing something.
And I know we did indeed use Maps in all prior videos in the course, but haven't really thought about it until I was faced with implementing this part myself :). So it was a great learning task actually, just this little bit got me confused if I'm getting everything right.
1 Answer
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsYou can pass any object to ModelAndView. But you can pass only one object, so it's better to use a map, because it means you can add more objects to the map later, and have a convenient key to get to those objects.
If you wanted to add a second object now, you would not only have to refactor your controller method, you would also have to change all the field references in the template. Using a map from the start, even if it's not strictly necessary, makes it easier to extend your site later. Since software changes all the time, making sure your software is easy to extend and maintain is a big part of software development. That's why sometimes you take the slightly longer route to do things.
Boban Talevski
24,793 PointsBoban Talevski
24,793 PointsThanks for clearing it up and confirming what I started to realize as I progressed through the course. And that is that it's indeed best to use a map to which we can add/remove things at different stages of request/response handling. Like we start building our model in the before filter, and then just add whatever needs to be added in the particular route.