本文介绍了如何在JSP中迭代列表对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ShoppingCart课:

I have ShoppingCart class:

package shoppingcart;

import models.CD;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ShoppingCart {

    private List<CD> shoppingCartItemList ;
    private long totalPrice ;

    public ShoppingCart()
    {
        shoppingCartItemList = new ArrayList<CD>() ;
        totalPrice = 0 ;
    }

    public void addCdToCart(CD cd)
    {
        shoppingCartItemList.add(cd) ;
        totalPrice += cd.getPrice();
    }

    public List<CD> getShoppingCartItems()
    {
        return this.shoppingCartItemList ;
    }

    public long getTotalPrice()
    {
        return totalPrice;
    }

}

我在servlet中使用此类:

I use this class in a servlet:

ShoppingCart shoppingCart = new ShoppingCart();

CD cd = new CD(1, "DAVID", 10, 1);
shoppingCart.addCdToCart(cd);

request.getSession().setAttribute("shoppingCart", shoppingCart);
request.setAttribute("servletMessage", "CD added to Cart");

如您所见,我有一个列表,我必须在我的jsp中对其进行迭代.我在我的jsp中导入ShoppingCard类:

As you can see, I have a list and i have to iterate this in my jsp. I import the ShoppingCard class in my jsp:

<%@ page import="shoppingcart.ShoppingCart"%>
<c:forEach items="${shoppingCart.getShoppingCartItems}" var="cart">
<form method="POST"     action="${pageContext.request.contextPath}/removeFromCart">
<input type="hidden" name="cdId" value="${cart.title}" />
<tr>
<td><c:out value="${cart.title}" /></td>
<td align="center"><c:out value="${cart.category}" /></td>
<td align="center"><c:out value="${cart.price}" /></td>
<td align="center" width="25%"><input type="submit" value="Remove"  name="action" style="height:30px; width: 70px;font-size:10pt;"></td>
</tr>
</form>
</c:forEach>

我收到此错误:

org.apache.jasper.el.JspPropertyNotFoundException: /myCart.jsp(85,4) '${shoppingCart.getShoppingCartItems}' Property 'getShoppingCartItems' not found on type shoppingcart.ShoppingCart. What can I solve this problem?

我应该改变一些东西吗?

Should I change something?

由于添加了额外的代码而进行了编辑.

Edited because of the extra added code.

推荐答案

购物车存储在名为"shoppingCart"的属性中.所以应该像

The shopping cart is stored in an attribute named "shoppingCart". So it should be something like

<c:forEach items="${shoppingCart.xxx}" ...

其中xxx是允许访问ShoppinCart实例内的列表的属性.但是您没有这样的财产.因此,在ShoppingCart中添加以下方法:

where xxx is the property allowing to access the list inside the ShoppinCart instance. But you don't have such a property. So, add the following method the ShoppingCart:

 public List<CD> getElements() {
     return this.shoppingCartItemList;
 }

并使用

<c:forEach items="${shoppingCart.elements}" ...

请注意,将"shoppingCartItemList"命名为"ShoppinCart"类的属性是多余且冗长的.这就是为什么我选择将吸气剂命名为getElements()的原因:shoppingCart.elementsshoppingCart.shoppingCartItemList更容易阅读和自然.您还应该将私有字段重命名为elements.

Note that naming "shoppingCartItemList" an attribute of the class "ShoppinCart" is redundant and verbose. That's why I chose to name the getter getElements(): shoppingCart.elements is much easier to read and natural than shoppingCart.shoppingCartItemList. You should rename the private field to elements as well.

这篇关于如何在JSP中迭代列表对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 10:58