getTotalPrice()

本文探讨了在JSP中使用EL表达式调用对象字段时的注意事项,强调了get…()方法命名的重要性,避免出现PropertyNotFoundException错误。通过具体代码示例展示了如何正确调用totalPrice字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
这段代码修改一个商品数量时其他商品数量也会变化,请进行修改const app = getApp() let tableNum = null Page({ data: { cartList: [], totalPrice: 0, // 总价,初始为0 totalNum: 0, //总数,初始为0 }, onShow() { tableNum = wx.getStorageSync("tableNum") let cartList = wx.getStorageSync('cart') || []; this.setData({ cartList }) this.getTotalPrice(); }, // 获取购物车总价、总数 getTotalPrice() { var cartList = this.data.cartList; // 获取购物车列表 var totalP = 0; var totalN = 0 for (var i in cartList) { // 循环列表得到每个数据 totalP += cartList[i].quantity * cartList[i].price; // 所有价格加起来 totalN += cartList[i].quantity } this.setData({ // 最后赋值到data中渲染到页面 cartList: cartList, totalNum: totalN, totalPrice: totalP.toFixed(2) }); }, // 购物车增加数量 addCount(e) { let item = e.currentTarget.dataset.item; let arr = wx.getStorageSync('cart') || []; let f = false; if (arr.length > 0) { for (let j in arr) { // 遍历购物车找到被点击的商品,数量加1 if (arr[j]._id == item._id) { arr[j].quantity += 1; f = true; try { wx.setStorageSync('cart', arr) } catch (e) { console.log(e) } break; } } if (!f) { arr.push(item); } } else { arr.push(item); } try { wx.setStorageSync('cart', arr) } catch (e) { console.log(e) } this.setData({ cartList: arr, }, success => { this.getTotalPrice(); }) }, //购物车减少数量 minusCount(e) { let item = e.currentTarget.dataset.item; let cartList = wx.getStorageSync('cart') || [];
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值