从一个JSP页面获取数据到另一个JSP页面,其中使用foreach循环显示JSP数据。

huangapple go评论72阅读模式
英文:

Get Data from one JSP to another JSP Where JSP data is show using foreach Loop

问题

我正在使用JSP中的foreach从会话变量中显示我的数据。该会话变量是产品的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>价格 = &#8377;${products.price}</p>
                    <p>库存剩余: <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")

英文:

I'm displaying my data in JSP using foreach from session variable.That session variable is Arraylist of products. I want to send the data from JSP to another JSP when user clicks on one product. How Can i achieve that>

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

How can send that specific product id and quantity which is clicked by user to another JSP. It is in for loop i cannot directly use request.getParameter(&quot;name&quot;).

答案1

得分: 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>价格 = &#8377;${products.price}</p>
                <p>剩余库存: <b>${products.quantity}</b> </p>
                <p> <input type="number" id="points" name="points" step="1" min="1" max="${products.quantity}"> </p>
                <!-- 添加隐藏字段 -->
                <input type="hidden" name="id" value="${products.pid}"/>
                <p id="${products.pid}"></p>
                <!-- 添加提交按钮 -->
                <p><button type="submit" id="cartBtn">加入购物车</button></p>
            </form>
        </div>
    </div>
</c:forEach>

然后可以使用request.getParameter("id")来获取id的值,对于数量,使用request.getParameter("points")

英文:

You can store ${products.pid} in some hidden inputs and as the details about products is already under form so the inputs which are there inside it will automatically get submitted to the other jsp page. i.e :

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

And get value of id using request.getParameter(&quot;id&quot;) and for quantity write request.getParameter(&quot;points&quot;).

huangapple
  • 本文由 发表于 2020年7月23日 20:52:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63054745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定