代码之家  ›  专栏  ›  技术社区  ›  Couim

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

  •  0
  • Couim  · 技术社区  · 7 年前

    forEach

    <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>
    

    上面的代码具有以下特点: displaying of fiches

    当我点击“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 要素

    1 回复  |  直到 7 年前
        1
  •  1
  •   StanislavL    7 年前
    <button type="submit" action="display_fiche" class="waves-effect waves-light btn">
    Afficher la fiche
    </button>
    

    创建隐藏输入以保留单击的id。

    但似乎你不需要这么复杂的方式 form post . 用普通的就够了 <a>

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