I’ve got a class Module with a OneToMany binding with a class Sequence.
My aim is to show the list of Modules, and by clicking on one of them, display the associated list of Sequences
But it doesn’t work, I have a HTTP 500 error.
Here there is my controller :
@RequestMapping(value="formation", method = RequestMethod.GET)
public ModelAndView allModules() {
List<Module> allModules = moduleService.findAll();
return new ModelAndView("formation", "modules", allModules);
}
@RequestMapping(value="sequences/{module}", method = RequestMethod.GET)
public String displaySequences(@PathVariable ("module") Module module, Model model) {
List<Sequence> allSequences = sequenceService.findByModule(module);
model.addAttribute("sequences", allSequences);
return "sequences";
}
and the jsp which show the list of modules to return the list of sequences
<c:forEach items="${modules}" var="module">
<ul>
<li><a href="sequences/${module}">${module.titre}</a>
<br/>
</li>
</ul>
</c:forEach>
So, where does my error come from?
It works when I do that:
@RequestMapping(value="/sequences/{moduleId}", method = RequestMethod.GET)
public String displaySequences(@PathVariable ("moduleId") Long moduleId, Model model) {
Module module = moduleService.findById(moduleId);
model.addAttribute("module", module);
return "sequences";
}
and I change the link with :
<a href="sequences/${module}">${module.titre}
but I’d like to understand my error.
The reason why you weren’t able to display sequences is Spring doesn’t know how to parse this
/cmap-web/sequences/com.almerys.jpa.tomcatspring.Module@12b0f0aeinto Module instance.
You can read on this in Spring docs here in the section’s
16.3.2.2 URI Template Patternslast paragraph. I paste it here for convenience.