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 trial
Rodrigo Castro
15,652 PointsDo the Key/Value pairs in the ModelMap object must always be the same?
Do the Key/Value pairs in the ModelMap object must always be the same? By the same I mean the String in the key must be similar to the value, e.g. "gif" => gif.
Thanks.
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsSure not. You are talking about Spring Java Framework, right?
If you really want you can do in @Controller :
@RequestMapping("/gif/{name}")
public String gifDetails(@PathVariable String name, Model model) {
Gif gif = gifRepository.findByName(name);
model.addAttribute("thing", gif);
return "gif-details";
}
And then in Thymeleaf template you can access:
<p th:text="${thing.name}"></p>
Although this example might be artificial and unneeded, because you don't know what thing is, I can provide more realistic scenario:
I would say where it can be helpful, for example @Controller :
@RequestMapping("/")
public String listGifs(Model model) {
List<Gif> allGifs = gifRepository.getAllGifs();
model.addAttribute("gifs", allGifs);
List<Gif> favoriteGifs = gifRepository.getFavoriteGifs();
model.addAttribute("favorites", favoriteGifs);
return "home";
}
And the use them in Thymeleaf templates favorites and gifs in different places accordingly..
Looks good ?