[英]Cast from an Object ArrayList to Object Array in JSP
I am trying to do a simple conversion from ArrayList to Object Array but I am getting an error, I have tried numerous methods as mention on the internet but none of them is working, NEED HELP 我正在尝试从ArrayList到Object Array的简单转换,但是出现错误,我尝试了互联网上提到的许多方法,但是没有一个起作用,需要帮助
Here is what I am doing 这是我在做什么
private List<CartItems> cartobj = new ArrayList<CartItems>();
--- Bean (Cart) ---
private String name;
private float price;
private int quantity;
--- In Servlet ---
Order h = new Order(String a, String b, String c);
cartobj.add(h);
session.setAttribute("Cart", cartobj);
--- In JSP ---
<tbody>
<% ArrayList<CartItems> cartobj = new ArrayList<CartItems>();
cartobj.add((CartItems)session.getAttribute("Cart"));
for(int i=0;i<cartobj.size();i++)
{%>
<tr>
<td><% out.println(i+1); %></td>
<td><% cartobj.get(i).getProductName(); %></td>
<td><% cartobj.get(i).getPrice(); %></td>
<td><% cartobj.get(i).getSales_Address(); %></td>
<td><% cartobj.get(i).getOrder_Date(); %></td>
<td><% cartobj.get(i).getQuantity(); %></td>
</tr>
<% } %>
I also tried this 我也尝试过
<%
CartItems[] obj = (CartItems[])session.getAttribute("Cart");
for(int i=0;i<obj.length;i++)
{%>
<tr>
<td><% out.println(i+1); %></td>
<td><% obj[i].getProductName(); %></td>
<td><% obj[i].getPrice(); %></td>
<td><% obj[i].getSales_Address(); %></td>
<td><% obj[i].getOrder_Date(); %></td>
<td><% obj[i].getQuantity(); %></td>
</tr>
<% } %>
I am getting this following error 我收到以下错误
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Final_Project] threw exception [java.lang.ClassCastException: java.util.ArrayList cannot be cast to [LObjects.CartItems;] with root cause
java.lang.ClassCastException: java.util.ArrayList cannot be cast to [LObjects.CartItems; java.lang.ClassCastException:无法将java.util.ArrayList强制转换为[LObjects.CartItems; at org.apache.jsp.cart_jsp._jspService(cart_jsp.java:165) 在org.apache.jsp.cart_jsp._jspService(cart_jsp.java:165)
You can't cast a List
to an array. 您不能将List
转换为数组。 A List
is not a type of array. List
不是数组的类型。 But, you could use List.toArray(T[])
and change 但是,您可以使用List.toArray(T[])
并更改
session.setAttribute("Cart", cartobj);
to something like 像
session.setAttribute("Cart", cartobj.toArray(new CartItems[cartobj.size()]));
then 然后
CartItems[] obj = (CartItems[])session.getAttribute("Cart");
would be valid. 将是有效的。
You need to cast it to List like this: 您需要像这样将其强制转换为List:
List<CartItems> obj = (List<CartItems>) session.getAttribute("Cart");
Still better to use JSTL for directly iterating on your list of objects: 更好的方法是使用JSTL直接迭代对象列表:
<c:forEach items="${Cart}" var="element">
<tr>
<td>${element.productName}</td>
<td>${element.salesAddress}</td>
<td>${element.quantity}</td>
</tr>
</c:forEach>
And follow Java bean naming conventions for your setters and getters, for example, getSales_Address is not a correctly defined. 并遵循您的setter和getter的Java Bean命名约定,例如,getSales_Address没有正确定义。
You are setting List<CartItems>
to session, ArrayList
can not be cast with Array
. 您正在将List<CartItems>
设置为session, ArrayList
无法使用Array
进行List<CartItems>
转换。
Try the following: 请尝试以下操作:
<%
List<CartItems> obj = (List<CartItems>)session.getAttribute("Cart");
for(int i=0;i<obj.length;i++)
{
CartItems cartItem = obj.get(i);
%>
<tr>
<td><% out.println(i+1); %></td>
<td><% cartItem.getProductName(); %></td>
<td><% cartItem.getPrice(); %></td>
<td><% cartItem.getSales_Address(); %></td>
<td><% cartItem.getOrder_Date(); %></td>
<td><% cartItem.getQuantity(); %></td>
</tr>
<% } %
Avoid java code in your jsp as much as you can. 尽可能避免在jsp中使用Java代码。 You can achieve this using JSTL
. 您可以使用JSTL
实现此目的。
<c:forEach items="${Cart}" var="c" varStatus="loop">
<tr>
<td>${loop.index+1}</td>
<td>${c.productName}</td>
<td>${c.price}</td>
<td>${c.sales_address}</td>
<td>${c.quantity}</td>
</tr>
</c:forEach>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.