Spring MVC 将对象从 foreach 传递到另一个控制器

Spring MVC Passing object from a foreach to another Controller

我有一个由 forEach spring 标签显示的对象列表。这是 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>

上面的代码有以下结果:

当我点击 "Afficher la fiche" 时,我想在另一个控制器上选择实际的 fiche 对象。

我尝试通过以下控制器进行操作:

@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;
}

我不知道这是否是个好方法,因为它不起作用。我总是得到一个'0'到fiche.getId()。如果不可能,我怎么能只传递 fiche.id 元素?

<button type="submit" action="display_fiche" class="waves-effect waves-light btn">
Afficher la fiche
</button>

创建隐藏输入以保留点击的 ID。

在上面的按钮中添加一个 JavaScipt 调用,以便在实际提交表单之前将点击的 ID 存储到隐藏的输入中。

但您似乎不需要 formpost 这么复杂的方法。使用通常的 <a> 标签并在控制器端获取映射就足够了。此外,只需传递 fiche id 就足够了。

<a href="/display_fiche/${fiche.id}" class="waves-effect waves-light btn">
Afficher la fiche
</a>

和控制器

@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;
}