I've a list of object which is dispayed by a forEach
spring tag. Here's the code of the jsp :
<c:forEach items="${liste_fiche}" var="fiche">
<div class="card blue-grey darken-1">
<form:form action="display_fiche" method="post" commandName="fiche" varStatus="status">
<div class="card-content white-text">
<span class="card-title">Fiche numéro ${fiche.id}</span>
<p>reference de la fiche : ${fiche.ref_fiche}</p>
<p>type de fiche : ${fiche.typeFiche}</p>
</div>
<div class="card-action">
<button type="submit" action="display_fiche"
class="waves-effect waves-light btn">Afficher la fiche</button>
</div>
</form:form>
</div>
</c:forEach>
The code above has the following reuslt :
When I click on "Afficher la fiche" I would like to go on another controller with the actual fiche object selected.
I tried to do it by the following controller :
@RequestMapping(value="display_fiche", method = RequestMethod.POST)
private ModelAndView displayFiche(@ModelAttribute("fiche") Fiche fiche, ModelMap modelMap) {
System.out.println("Fiche séléctionnée : " + fiche.getId());
return model;
}
I don't know if it's the good way to do, because it does not work. I always get a '0' to fiche.getId()
. If it's not possible, how can I just pass only the fiche.id
element?
<button type="submit" action="display_fiche" class="waves-effect waves-light btn">
Afficher la fiche
</button>
Create a hidden input to keep clicked id.
In the button above add a JavaScipt call to store the clicked id into the hidden input before actual submitting the form.
But it seems you dont' need such a complicated way with form
and post
. It is enough to use usual <a>
tag and have get mapping on the controller side. Moreover it's enough to pass just fiche id.
<a href="/display_fiche/${fiche.id}" class="waves-effect waves-light btn">
Afficher la fiche
</a>
and controller
@RequestMapping(value="/display_fiche/{id}", method = RequestMethod.GET)
private ModelAndView displayFiche(@PathVariable("id") Long id, ModelMap modelMap) {
System.out.println("Fiche séléctionnée : " + id);
return model;
}