【问题标题】:Get Data from one JSP to another JSP Where JSP data is show using foreach Loop [duplicate]从一个 JSP 获取数据到另一个 JSP 使用 foreach 循环显示 JSP 数据的位置 [重复]
【发布时间】:2020-11-13 05:00:07
【问题描述】:

我使用来自会话变量的 foreach 在 JSP 中显示我的数据。该会话变量是产品的 Arraylist。当用户单击一个产品时,我想将数据从 JSP 发送到另一个 JSP。我怎样才能做到这一点>

<div class="row">
    <c:forEach items="${listProducts}" var="products">
    <div class="column">
        <div class="card">
        <form action="disp.jsp" method="GET">
                <h3>${products.pname}</h3>
                <p>price = &#8377;${products.price}</p>
                <p>Left out Stock: <b> ${products.quantity} </b> </p>
               <p> <input type="number" id="points" name="points" step="1" min="1" max="${products.quantity}"> </p>
                <p id="${products.pid}"></p>
                <p><button id="cartBtn">Add to Cart</button></p>
         </form>
         </div>
     </div>
    </c:forEach>
    
</div> 

如何将用户点击的特定产品 ID 和数量发送到另一个 JSP。它在for循环中我不能直接使用request.getParameter("name")

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    您可以将${products.pid} 存储在一些隐藏的输入中,并且由于有关产品的详细信息已经在表单中,因此其中的输入将自动提交到另一个 jsp 页面。即:

     <c:forEach items="${listProducts}" var="products">
        <div class="column">
            <div class="card">
               <form action="disp.jsp" method="GET">
                    <h3>${products.pname}</h3>
                    <p>price = &#8377;${products.price}</p>
                    <p>Left out Stock: <b> ${products.quantity} </b> </p>
                   <p> <input type="number" id="points" name="points" step="1" min="1" max="${products.quantity}"> </p>
                   ///added hidden fields
                     <input type="hidden" name="id" value="${products.pid}"/>
                    <p id="${products.pid}"></p>
                   //add button type submit
                    <p><button type="submit "id="cartBtn">Add to Cart</button></p>
             </form>
             </div>
         </div>
        </c:forEach>
    

    并使用request.getParameter("id") 获取id 的值,对于数量写入request.getParameter("points")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多