[英]Class cast exception when i try to cast getAttribute from the session object?
我知道有可能不抛出任何异常,但是我该如何投射此对象/ bean:
public class ShoppingCart {
private HashMap<String, Item> items;
private String owner;
public ShoppingCart(String owner) {
items = new HashMap<String, Item>();
this.owner = owner;
}
public class Servlet extends HttpServletRequest request
if (action.equals("/addItem")) {
//if()
String Id = request.getParameter("itemId");
ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
int quantity = Integer.parseInt(request.getParameter("quantity").toString());
Item item = this.stock.getItem(Id);
cart.addItem(item, quantity);
dispatcher = this.getServletContext().getRequestDispatcher(action = "/CartView.jspx");
}
购物车强制抛出类强制转换异常无法将字符串强制转换为模型类型lessonSelection。 任何帮助将非常感激。
您正在将一个String
对象放入带有session.setAttribute("cart", username);
会话中session.setAttribute("cart", username);
但您稍后尝试将此String
为ShoppingCart
。 通常无法将String
强制转换为ShoppingCart
因此它给出ClassCastException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.