作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个基于 session 的购物车。我使用两个bean类和servlet Controller ,并尝试使用jSTL在jsp上显示bean的内容。在这里,我收到错误:在 Cart.CartItemBean 类型上找不到 'Title'。我已经在 CartItemBean 中声明了 Tiltle 属性。有人可以告诉我为什么显示错误。
Cart.CartItemBean 代码
package Cart;
public class CartItemBean {
public int IdNumber;
private String Title;
private String Image;
private double dblUnitCost;
private int iQuantity;
private double dblTotalCost;
public int getIdNumber() {
return IdNumber;
}
public void setIdNumber(int IdNumber) {
this.IdNumber = IdNumber;
}
public String getImage() {
return Image;
}
public void setImage(String Image) {
this.Image = Image;
}
public String getTitle() {
return Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
public double getUnitCost() {
return dblUnitCost;
}
public void setUnitCost(double dblUnitCost) {
this.dblUnitCost = dblUnitCost;
}
public int getQuantity() {
return iQuantity;
}
public void setQuantity(int quantity) {
iQuantity = quantity;
}
public double getTotalCost() {
return dblTotalCost;
}
public void setTotalCost(double dblTotalCost) {
this.dblTotalCost = dblTotalCost;
}
}Cart.CartBean 代码
package Cart;
import Cart.CartItemBean;
import java.util.ArrayList;
public class CartBean {
private ArrayList alCartItems = new ArrayList();
private double dblOrderTotal;
int lineItemCount = alCartItems.size();
public int getLineItemCount() {
return lineItemCount;
}
public void deleteCartItem(String strItemIndex) {
int iItemIndex = 0;
try {
iItemIndex = Integer.parseInt(strItemIndex);
alCartItems.remove(iItemIndex - 1);
calculateOrderTotal();
} catch (NumberFormatException nfe) {
System.out.println("Error while deleting cart item: " + nfe.getMessage());
nfe.printStackTrace();
}
}
public void updateCartItem(String strItemIndex, String strQuantity) {
double dblTotalCost = 0.0;
double dblUnitCost = 0.0;
int iQuantity = 0;
int iItemIndex = 0;
CartItemBean cartItem = null;
try {
iItemIndex = Integer.parseInt(strItemIndex);
iQuantity = Integer.parseInt(strQuantity);
if (iQuantity > 0) {
cartItem = (CartItemBean) alCartItems.get(iItemIndex - 1);
dblUnitCost = cartItem.getUnitCost();
dblTotalCost = dblUnitCost * iQuantity;
cartItem.setQuantity(iQuantity);
cartItem.setTotalCost(dblTotalCost);
calculateOrderTotal();
}
} catch (NumberFormatException nfe) {
System.out.println("Error while updating cart: " + nfe.getMessage());
nfe.printStackTrace();
}
}
public void addCartItem(int strModelNo, String strDescription,
String strUnitCost, String strQuantity) {
double dblTotalCost = 0.0;
double dblUnitCost = 0.0;
int iQuantity = 0;
CartItemBean cartItem = new CartItemBean();
try {
dblUnitCost = Double.parseDouble(strUnitCost);
iQuantity = Integer.parseInt(strQuantity);
if (iQuantity > 0) {
dblTotalCost = dblUnitCost * iQuantity;
cartItem.setIdNumber(strModelNo);
cartItem.setTitle(strDescription);
cartItem.setUnitCost(dblUnitCost);
cartItem.setQuantity(iQuantity);
cartItem.setTotalCost(dblTotalCost);
alCartItems.add(cartItem);
calculateOrderTotal();
}
} catch (NumberFormatException nfe) {
System.out.println("Error while parsing from String to primitive types: " + nfe.getMessage());
nfe.printStackTrace();
}
}
public void addCartItem(CartItemBean cartItem) {
alCartItems.add(cartItem);
}
public CartItemBean getCartItem(int iItemIndex) {
CartItemBean cartItem = null;
if (alCartItems.size() > iItemIndex) {
cartItem = (CartItemBean) alCartItems.get(iItemIndex);
}
return cartItem;
}
public ArrayList getCartItems() {
return alCartItems;
}
public void setCartItems(ArrayList alCartItems) {
this.alCartItems = alCartItems;
}
public double getOrderTotal() {
return dblOrderTotal;
}
public void setOrderTotal(double dblOrderTotal) {
this.dblOrderTotal = dblOrderTotal;
}
protected void calculateOrderTotal() {
double dblTotal = 0;
for (int counter = 0; counter < alCartItems.size(); counter++) {
CartItemBean cartItem = (CartItemBean) alCartItems.get(counter);
dblTotal += cartItem.getTotalCost();
}
setOrderTotal(dblTotal);
}
}
ShoppingCart.jsp 代码
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Shopping Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p><font face="Verdana, Arial, Helvetica, sans-serif"><strong>Shopping Cart</strong></font></p>
<p><a href="AllProducts.jsp" mce_href="AllProducts.jsp">Model List</a> </p>
<table width="75%" border="1">
<tr bgcolor="#CCCCCC">
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Model
Description</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Quantity</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Unit
Price</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Total</font></strong></td>
</tr>
<jsp:useBean id="cart" scope="session" class="Cart.CartBean"/>
<c:if test="${cart.lineItemCount==0}">
<tr>
<td colspan="4"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">- Cart is currently empty -<br/>
</tr>
</c:if>
<c:forEach var="cartItem" items="${cart.cartItems}" varStatus="counter">
<form name="item" method="POST" action="CartController">
<tr>
<td>
<c:out value="${cartItem.Title}"/></font></td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b></b><br/>
</font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><input type='hidden' name='itemIndex' value='<c:out value="${counter.count}"/>'><input type='text' name="quantity" value='<c:out value="${cartItem.quantity}"/>' size='2'> <input type="submit" name="action" value="Update">
<input type="submit" name="action" value="Delete"></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">$<c:out value="${cartItem.unitCost}"/></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">$<c:out value="${cartItem.totalCost}"/></font></td>
</tr>
</form>
</c:forEach>
<tr>
<td colspan="2"> </td>
<td> </td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Subtotal: $<c:out value="${cart.orderTotal}"/></font></td>
</tr>
</table>
</body>
enter code here
最佳答案
更改为
${cartItem.title}
还将您的后台 Bean 字段名称更改为标准 Bean 命名约定
它遵循jsp bean method naming convention解析 getter 时
关于javax.el.PropertyNotFoundException : Property 'Title' not found on type Cart. CartItemBean jsp 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008146/
这个问题已经有答案了: javax.el.PropertyNotFoundException: Property 'foo' not found on type com.example.Bean (6
我是一名优秀的程序员,十分优秀!