EL表达式相除问题

EL表达式相除问题

老师我现在做了一个购物车把之前的商品信息放进去并显示,但到要根据商品数量计算商品总价的时候总是不成功,想问一下老师,EL表达式可以和页面新输入的数字相乘并输出结果么

这是前台页面其中的一段代码

<div class="Shopping-cart-area pb-80">

<div class="container">

<div class="row">

<div class="col-12">

<form class="form-horizontal"

action="${pageContext.request.contextPath}/AddCartServlet"

enctype="multipart/form-data" onsubmit="return form()"

method="post">

<div class="table-content table-responsive">

<table class="table">

<thead>

<tr>

<th class="indecor-product-remove">remove</th>

<th class="indecor-product-thumbnail">images</th>

<th class="cart-product-name">Product</th>

<th class="indecor-product-price">Unit Price</th>

<th class="indecor-product-quantity">Quantity</th>

<th class="indecor-product-subtotal">Total</th>

</tr>

</thead>

<tbody>

<c:forEach items="${sessionScope.cartList }" var="cl"

>

<tr>

<td class="indecor-product-remove"><a

href="${pageContext.request.contextPath}/DeleteCartServlet?productId=${cl.productId}"><i

class="fa fa-times"></i></a></td>

<td class="indecor-product-thumbnail"><a href="#"><img

src="${cl.productPicture }" width="100px" height="100px"></a></td>

<td class="indecor-product-name"><a href="#">${cl.productName}</a></td>

<td class="indecor-product-price"><span class="amount">$${cl.productPrice}</span></td>

<td class="indecor-product-quantity"><input value="1" name="Cartquantity" id="Cartquantity" class="Cartquantity"

type="number">${cl.productAmount }</td>

<td class="product-subtotal" onsubmit="return money()"><span id="TotalPrice" class="TotalPrice">${cl.productPrice}*${cl.productAmount }</span></td>

</tr>


</c:forEach>

</tbody>

</table>

</div>

<div class="row">

<div class="col-12">

<div class="coupon-all">

<div class="coupon">

<input id="coupon_code" class="input-text" name="coupon_code"

value="" placeholder="Coupon code" type="text"> <input

class="button" name="apply_coupon" value="Apply coupon"

type="submit">

</div>

<div class="coupon2">

<input class="button" name="update_cart" value="Update cart"

type="submit">

</div>

</div>

</div>

</div>

<div class="row">

<div class="col-md-5 ml-auto">

<div class="cart-page-total">

<h2>Cart totals</h2>

<ul>

<li>Total <span>$130.00</span></li>

</ul>

<a href="#">Proceed to checkout</a>

</div>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

下面是购物车类的代码

package com.imooc.domain;


public class Cart {

private String productId;

private String productName;

private String productPrice;

private String productPicture;

private String productDetail;

private int productAmount;


public Cart() {

super();

}

public Cart(String productId, String productName, String productPrice, String productPicture, String productDetail,

int productAmount) {

super();

this.productId = productId;

this.productName = productName;

this.productPrice = productPrice;

this.productPicture = productPicture;

this.productDetail = productDetail;

this.productAmount = productAmount;

}

public String getProductId() {

return productId;

}

public void setProductId(String productId) {

this.productId = productId;

}

public String getProductName() {

return productName;

}

public void setProductName(String productName) {

this.productName = productName;

}

public String getProductPrice() {

return productPrice;

}

public void setProductPrice(String productPrice) {

this.productPrice = productPrice;

}

public String getProductPicture() {

return productPicture;

}

public void setProductPicture(String productPicture) {

this.productPicture = productPicture;

}

public String getProductDetail() {

return productDetail;

}

public void setProductDetail(String productDetail) {

this.productDetail = productDetail;

}

public int getProductAmount() {

return productAmount;

}

public void setProductAmount(int productAmount) {

this.productAmount = productAmount;

}

@Override

public String toString() {

return "Cart [productId=" + productId + ", productName=" + productName + ", productPrice=" + productPrice

+ ", productPicture=" + productPicture + ", productDetail=" + productDetail + ", productAmount="

+ productAmount + "]";

}




}



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
提问者 niuhuiyang 2020-03-14 12:42:10

老师我知道了,那现在我编写了一个代码想动态计算商品价格成衣其数量后的值,但param运行就没有生效,麻烦老师看看什么问题

<table class="myTable">

<thead>

<tr>

<th class="indecor-product-remove">remove</th>

<th class="indecor-product-thumbnail">images</th>

<th class="cart-product-name">Product</th>

<th class="indecor-product-price">Unit Price</th>

<th class="indecor-product-quantity">Quantity</th>

<th class="indecor-product-subtotal">Total</th>

</tr>

</thead>

<tbody>

<c:forEach items="${sessionScope.cartList }" var="cl"

>

<tr>

<td class="indecor-product-remove"><a

href="${pageContext.request.contextPath}/DeleteCartServlet?productId=${cl.productId}"><i

class="fa fa-times"></i></a></td>

<td class="indecor-product-thumbnail"><a href="#"><img

src="${cl.productPicture }" width="100px" height="100px"></a></td>

<td class="indecor-product-name"><a href="#">${cl.productName}</a></td>

<td class="indecor-product-price"><span class="amount">$${cl.productPrice}</span></td>

<td class="indecor-product-quantity"><input value="1" name="Cartquantity" id="Cartquantity" class="Cartquantity"

type="number"></td>

<td class="product-subtotal" onsubmit="return money()"><span id="TotalPrice" class="TotalPrice">${cl.productPrice*param.Cartquantity}</span></td>

</tr>

</c:forEach>

</tbody>

</table>

http://img1.sycdn.imooc.com//climg/5e6c605d0851aa0311840245.jpg

http://img1.sycdn.imooc.com//climg/5e6c601408ad2a1411810152.jpg可以获得商品数量,但最后的计算值仍然为0,后台也没有报错

  • 同学你好,同学是在js里使其显示的数量吧?这个param.Cartquantity 是不是并不是存在在request域中的数字呢?同学如果单单用el表达式,可以取出param.Cartquantity 么?老师猜测,param.Cartquantity并不是el表达式可以取出来的。如果是这样,建议同学这个总数也使用js来计算,而不用el表达式了
    2020-03-14 15:11:05
好帮手慕阿莹 2020-03-14 11:43:27

同学你好

el表达式的相乘要在${}表达式里

例如${cl.productPrice *  cl.productAmount }

老师写了个例子:

http://img1.sycdn.imooc.com//climg/5e6c52ba09eb02b408440119.jpg

http://img1.sycdn.imooc.com//climg/5e6c52cd091589d906330079.jpg

http://img1.sycdn.imooc.com//climg/5e6c52d809fa3c0602270059.jpg

如果我的回答解决了你的问题,请采纳,祝学习愉快.

提问者 niuhuiyang 2020-03-13 21:54:25

EL表达式相乘,不好意思老师打错了

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师