El expression issues encountered in shopping cart

Source: Internet
Author: User

When making a shopping cart, the console reported the problem:

Java.lang.RuntimeException: Method Internal Error Org.apache.jasper.JasperException:An exception occurred processing JSP page/item/ Jsps/cart/list.jsp at line 7471: <th> subtotal </th>72: <th> operation </th>73: </tr>74: <c:foreach items= "${sessionscope.cart.cartitems}" var= "Cartitem" >75: <tr>76: <td><div> '/></div></td>77: <td>${cartitem.book.bname} </td>

The code for the JSP part of the shopping cart is as follows:
    • <c:foreach items= "${sessionscope.cart.cartitems}" var= "Cartitem" >
    • <tr>
    • <td><div></div></td >
    • <td>${cartitem.book.bname}</td>
    • <td>${cartitem.book.author}</td>
    • <td>${cartitem.book.price} Yuan </td>
    • <td>${cartitem.count}</td>
    • <td>79.8 Yuan </td>
    • <td><a href= "Javascript:alert (' delete shopping item succeeded! ‘);" > Delete </a></td>
    • </tr>
    • </c:forEach>
The bean code for CART class carts is:

public class Cart {

map<string,cartitem> cart = new linkedhashmap<string,cartitem> ();
Add Shopping Cart
public void Add (Cartitem item) {
Get book
Book book = Item.getbook ();

int num = Item.getcount ();


Update entry
if (Cart.containskey (Book.getbid ())) {
Get Old entries
Cartitem Olditem = Cart.get (Book.getbid ());
Olditem.setcount (Olditem.getcount () +num);
Cart.put (Book.getbid (), Olditem);
}else{
Cart.put (Book.getbid (), item);
}

}
  Public collection<cartitem> Getcartitems () {
return Cart.values ();
}
}

After careful search, it turns out that the Getcartitems () method was not written, resulting in the El expression having no way to obtain data when reading JavaBean. It is also important to note that the attribute name should be the method name minus the first letter of the get and lowercase ${sessionscope.cart. Cartitems} is the same as the Getcartitems() method with the red part of the first letter lowercase, and after the method is added, it is normal.

--------------------------------------------------------------------------------------------------------------- ----------------

Here's a little note that needs to be understood

When you need to delete items from your shopping cart in Cartservlet, you need to first get the shopping cart object from the session, not the new cart () object, otherwise you are not working on the same object

For example: Cartbean for

  • public class Cart {
  • map<string,cartitem> cart = new linkedhashmap<string,cartitem> ();
  • Add Shopping Cart
  • public void Add (Cartitem item) {
  • Get book
  • Book book = Item.getbook ();
  • int num = Item.getcount ();
  • Update entry
  • if (Cart.containskey (Book.getbid ())) {
  • Get Old entries
  • Cartitem Olditem = Cart.get (Book.getbid ());
  • Olditem.setcount (Olditem.getcount () +num);
  • Cart.put (Book.getbid (), Olditem);
  • }else{
  • Cart.put (Book.getbid (), item);
  • }
  • }
  • Public collection<cartitem> Getcartitems () {
  • return Cart.values ();
  • }
  • Total
  • Public double gettotal () {
  • Double d = 0.0;
  • set<string> set = Cart.keyset ();
  • for (String S:set) {
  • D+=cart.get (s). Gettotal ();
  • }
  • BigDecimal bd = new BigDecimal (d);
  • return Bd.doublevalue ();
  • }
  • Delete a form item
  • public void Delete (String bid) {
  • Cart.remove (BID);
  • }
  • Empty shopping Cart
  • public void Clear () {
  • Cart.clear ();
  • }
  • Get the number of form items
  • public int Getitemnum () {
  • return Cart.size ();
  • }
  • }
  • The Delete method for Cartservlet should be:
  • Delete Shopping Cart
    Public String Delete (HttpServletRequest request, httpservletresponse response) {
    Get the form item ID
    String bid = Request.getparameter ("bid");
    Gets the cart stored in the session and operates its properties
    Note that this is not the new Cart (). Delete (BID);
    A Cart object is created instead of manipulating the Cart object in the session
    This is where the cart object in the session is being manipulated throughout.
    Cart cart = (cart) request.getsession (). getattribute ("cart");
    Cart.delete (BID);
    return "f:/item/jsps/cart/list.jsp";
    }

El expression issues encountered in shopping cart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.