jsp调用字段时注意事项
前端用${SHOPPINGCART_IN_SESSION.totalPrice}调用totalPrice时,getTotalPrice()方法的命名需要遵从get…()的原则。
否则报错:
严重: Servlet.service() for servlet [jsp] in context with path [] threw exception [javax.el.PropertyNotFoundException: Property 'totalPrice' not found on type com._520it._02shoppingcart.ShoppingCart] with root cause
javax.el.PropertyNotFoundException: Property 'totalPrice' not found on type com._520it._02shoppingcart.ShoppingCart
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:291)
at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
at javax.el.BeanELResolver.property(BeanELResolver.java:378)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
at org.apache.el.parser.AstValue.getValue(AstValue.java:184)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
public BigDecimal getTotalPrice() {
BigDecimal totalPrice = BigDecimal.ZERO;
for (CartItem item : items) {
totalPrice=totalPrice.add(item.getPrice().multiply(new BigDecimal(item.getNum())));
System.out.println(totalPrice);
}
return totalPrice;
}