w306752855 2014-05-06 08:37 采纳率: 0%
浏览 4080

c foreach和c if嵌套程序问题



cellpadding="10">
























/c:if
                                            </c:forEach>

                                        </table>
                                        <table width="100%" border="1" cellspacing="5"
                                            cellpadding="10">
                                            <tr>
                                                <td align="center">餐品名:</td>
                                                <td align="center">购买数量:</td>
                                                <td align="center">单价:</td>
                                                <td align="center">总计:</td>
                                            </tr>
                                            <c:forEach var="listOrderDetails" items="${listOrderDetails}">
                                            <c:forEach var="orderDetails" items="${listOrderDetails}">
                                            <c:if test="${orderDetails.orderID==orderList.orderID}">
                                            <c:forEach var="product" items="${product}">

                                            <c:if test="${orderDetails.foodID==product.foodID}">
                                            <tr>
                                                <td align="center">${product.foodName}</td>
                                                <td align="center">${orderDetails.foodCount}</td>
                                                <td align="center">${product.foodPrice}</td>
                                                <td align="center">${product.foodPrice*orderDetails.foodCount}</td>
                                            </tr>

                                            </c:if>
                                            </c:forEach>
                                            </c:if>
                                            </c:forEach>
                                            </c:forEach>
                                        </table>



                                    </div>
                                    </c:forEach>

后面的餐品列表出不来什么错误?求大神帮忙!

订单编号: ${orderList.orderID} 订单总价: ${orderList.totalPrice}
订餐人: ${orderMan.orderName} 就餐地址: ${orderMan.address} 电话: ${orderMan.telephone } 邮箱: ${orderMan.email}
订单状态: ${orderMan.orderType} 送餐方式: ${orderMan.sendType} 支付方式: ${orderMan.payType} 订餐时间: ${orderMan.writeTime}
备注: ${orderMan.message}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 01:22
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这段代码的目的是在一个HTML页面上显示一个表格,其中包含用户提供的餐品信息。为了实现这个功能,你需要在每个<c:forEach>循环内部使用Java的条件语句(如ifelse ifswitch等)来处理不同情况。

    然而,你的代码存在一些问题:

    1. listOrderDetailsproduct 是两个不同的变量。
    2. 你没有正确地设置<c:forEach>标签内的var属性。应该为listOrderDetailsproduct分别设置var="listOrderDetails"var="product"
    3. 你应该避免直接访问JSP页面中的数据,因为这可能导致安全风险和性能问题。通常情况下,你应该从数据库或其他持久化存储中获取数据,并将它们传递给JSP页面进行渲染。

    以下是一个修改后的版本,它应该可以解决上述问题并正确地显示餐品列表:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>餐桌展示</title>
    </head>
    <body>
        <h1>餐品列表</h1>
        <table>
            <thead>
                <tr>
                    <th>餐品名</th>
                    <th>购买数量</th>
                    <th>单价</th>
                    <th 总计</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach var="listOrderDetails" items="${listOrderDetails}">
                    <tr>
                        <c:forEach var="orderDetails" items="${listOrderDetails}">
                            <c:if test="${orderDetails.orderID==orderList.orderID}">
                                <c:forEach var="product" items="${product}">
                                    <c:if test="${orderDetails.foodID==product.foodID}">
                                        <td>${product.foodName}</td>
                                        <td>${orderDetails.foodCount}</td>
                                        <td>${product.foodPrice}</td>
                                        <td>${product.foodPrice*orderDetails.foodCount}</td>
                                    </c:if>
                                </c:forEach>
                            </c:if>
                        </c:forEach>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </body>
    </html>
    

    在这个版本的代码中,我已更改了<c:forEach>标签内var属性以确保每次迭代都只遍历一次listOrderDetailsproduct数组。同时,我也移除了对JSP页面的直接引用,转而使用JSTL标签库中的<c:forEach><c:if>标签。此外,我还添加了一个简单的标题行和表头行,以及一个空的<tbody>元素来容纳所有表格行。

    评论

报告相同问题?