简体   繁体   English

如何在JSP中迭代列表对象?

[英]How can I iterate list object in JSP?

I have ShoppingCart class: 我有ShoppingCart课程:

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

}

I use this class in a servlet: 我在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");     

As you can see, I have a list and i have to iterate this in my jsp. 如您所见,我有一个列表,我必须在jsp中对其进行迭代。 I import the ShoppingCard class in my jsp: 我在jsp中导入ShoppingCard类:

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

I get this error: 我收到此错误:

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. 由于添加了额外的代码而进行了编辑。

The shopping cart is stored in an attribute named "shoppingCart". 购物车存储在名为“ shoppingCart”的属性中。 So it should be something like 所以应该像

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

where xxx is the property allowing to access the list inside the ShoppinCart instance. 其中xxx是允许访问ShoppinCart实例内的列表的属性。 But you don't have such a property. 但是您没有这样的财产。 So, add the following method the ShoppingCart: 因此,在ShoppingCart中添加以下方法:

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

and use 和使用

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

Note that naming "shoppingCartItemList" an attribute of the class "ShoppinCart" is redundant and verbose. 注意,将“ shoppingCartItemList”命名为类“ ShoppinCart”的属性是多余且冗长的。 That's why I chose to name the getter getElements() : shoppingCart.elements is much easier to read and natural than shoppingCart.shoppingCartItemList . 这就是为什么我选择来命名吸气getElements() shoppingCart.elements比更容易阅读和自然shoppingCart.shoppingCartItemList You should rename the private field to elements as well. 您还应该将私有字段重命名为elements

I recommend you to use JSTL for iterating objects in JSP 我建议您使用JSTL来迭代JSP中的对象

here is a case similar to this: JSTL iterate over list of objects 这是类似的情况: JSTL遍历对象列表

here is an example: http://www.journaldev.com/2090/jstl-tutorial-with-examples-jstl-core-tags 这是一个示例: http : //www.journaldev.com/2090/jstl-tutorial-with-examples-jstl-core-tags

here is a jstl tutorial: http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm 这是一个jstl教程: http : //www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

it has more things to have a better source code. 它拥有更多东西来拥有更好的源代码。 I hope this can help you... 我希望这可以帮助您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM