I'm following a tutorial on building an e-commerce web application with spring framework in Intellij Idea, and i have a problem with the the shopping cart functionality.
The functionality works fine : I can add product to it but when i want to show the product in the cart nothing is shown
This is my code
The CartController
@Controller
@RequestMapping("/rest/cart")
public class CartController {
@Autowired
private CartDao cartDao;
@Autowired
private ProductDao productDao;
@RequestMapping(value = "/{cartId}",headers="Accept=*/*", method = RequestMethod.GET)
public @ResponseBody Cart read(@PathVariable(value = "cartId") String cartId) {
return cartDao.read(cartId);
}
@RequestMapping(value = "/{cartId}",headers="Accept=*/*", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void update(@PathVariable(value = "cartId") String cartId, @RequestBody Cart cart) {
cartDao.update(cartId, cart);
}
@RequestMapping(value = "/{cartId}",headers="Accept=*/*", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void delete(@PathVariable(value = "cartId") String cartId) {
cartDao.delete(cartId);
}
@RequestMapping(value = "/add/{productId}",headers="Accept=*/*", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addItem(@PathVariable (value = "productId") String productId, HttpServletRequest request) {
String sessionId = request.getSession(true).getId();
Cart cart = cartDao.read(sessionId);
if(cart == null) {
cart = cartDao.create(new Cart(sessionId));
}
Product product = productDao.getProductById(productId);
if(product == null) {
throw new IllegalArgumentException(new Exception());
}
cart.addCartItem(new CartItem(product));
cartDao.update(sessionId, cart);
}
@RequestMapping(value = "/remove/{productId}",headers="Accept=*/*", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void removeItem(@PathVariable(value = "productId") String productId, HttpServletRequest request) {
String sessionId = request.getSession(true).getId();
Cart cart = cartDao.read(sessionId);
if(cart == null) {
cart = cartDao.create(new Cart(sessionId));
}
Product product = productDao.getProductById(productId);
if(product == null) {
throw new IllegalArgumentException(new Exception());
}
cart.removeCartItem(new CartItem(product));
cartDao.update(sessionId, cart);
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Illigal request, please verify your payload")
public void handleClientError(Exception e) {}
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Internal Server Error")
public void handleServerError(Exception e) {}
}
The CartDao
public interface CartDao {
Cart create(Cart cart);
Cart read(String cartId);
void update (String cartId, Cart cart);
void delete (String cartId);
}
The Implementation of the CartDao
@Repository
public class CartDaoImpl implements CartDao{
private Map<String, Cart> listOfCarts;
public CartDaoImpl() {
listOfCarts = new HashMap<String, Cart>();
}
public Cart create(Cart cart) {
if(listOfCarts.keySet().contains(cart.getCartId())) {
throw new IllegalArgumentException(String.format("Cannot create a cart. A cart with the given id(%) " + "already" + "exists", cart.getCartId()));
}
listOfCarts.put(cart.getCartId(), cart);
return cart;
}
public Cart read(String cartId) {
return listOfCarts.get(cartId);
}
public void update(String cartId, Cart cart) {
if(!listOfCarts.keySet().contains(cartId)) {
throw new IllegalArgumentException(String.format("Cannot create cart. The cart with the given id(%) " + "does not" + "exist", cart.getCartId()));
}
listOfCarts.put(cartId, cart);
}
public void delete(String cartId) {
if(!listOfCarts.keySet().contains(cartId)) {
throw new IllegalArgumentException(String.format("Cannot delete cart. A cart with the given id(%) " + "does not" + "exists", cartId));
}
listOfCarts.remove(cartId);
}
}
The model ( Cart.java)
public class Cart {
private String cartId;
private Map<String, CartItem> cartItems;
private double grandTotal;
private Cart() {
cartItems = new HashMap<String, CartItem>();
grandTotal = 0 ;
}
public Cart(String cartId) {
this();
this.cartId = cartId;
}
public String getCartId() {
return cartId;
}
public void setCartId(String cartId) {
this.cartId = cartId;
}
public Map<String, CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(Map<String, CartItem> cartItems) {
this.cartItems = cartItems;
}
public double getGrandTotal() {
return grandTotal;
}
public void setGrandTotal(double grandTotal) {
this.grandTotal = grandTotal;
}
public void addCartItem (CartItem item) {
String productId = item.getProduct().getProductId();
if(cartItems.containsKey(productId)) {
CartItem existingCartItem = cartItems.get(productId);
existingCartItem.setQuantity(existingCartItem.getQuantity()+item.getQuantity());
cartItems.put(productId, existingCartItem);
} else {
cartItems.put(productId, item);
}
updateGrandTotal();
}
public void removeCartItem (CartItem item) {
String productId = item.getProduct().getProductId();
cartItems.remove(productId);
updateGrandTotal();
}
public void updateGrandTotal() {
grandTotal = 0;
for (CartItem item : cartItems.values()) {
grandTotal = grandTotal + item.getTotalPrice();
}
}
}
The controller.js file that contains all the functions for the Cart functionality
var cartApp = angular.module("cartApp", []);
cartApp.controller("cartCtrl", function ($scope,$http) {
$scope.refreshCart = function (cartId) {
$http.get('/eCafeteriaStore/rest/cart/'+$scope.cartId).success(function (data) {
$scope.cart=data;
});
};
$scope.clearCart = function () {
$http.delete('/eCafeteriaStore/rest/cart/'+$scope.cartId).success($scope.refreshCart($scope.cartId));
};
$scope.initCartId = function (cartId) {
$scope.cartId = cartId;
$scope.refreshCart(cartId);
};
$scope.addToCart = function (productId) {
$http.put('/eCafeteriaStore/rest/cart/add/'+productId).success(function (data) {
$scope.refreshCart($http.get('/eCafeteriaStore/rest/cart/cartId'));
alert("Le produit a été ajouté au panier")
});
};
$scope.removeFromCart = function (productId) {
$http.put('/eCafeteriaStore/rest/cart/remove/'+productId).success(function (data) {
$scope.refreshCart($http.get('/eCafeteriaStore/rest/cart/cartId'));
});
};
});
the JSP file viewProduct where i can see the "Add to cart button" and the "view cart" button
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@include file="/WEB-INF/views/template/header.jsp"%>
<div class="container-wrapper">
<div class="container">
<div class="page-header">
<h1>Products Information</h1>
<p class="lead">View product details</p>
</div>
<div class="container" ng-app="cartApp">
<div class="row">
<div class="col-lg-5">
<img src="<c:url value="/resources/images/${product.productId}.png"/> " alt="image" style="width: 100%" />
</div>
<div class="col-lg-5">
<h3>${product.productName}</h3>
<p>${product.productDescription}</p>
<p><strong>Manufacturer</strong> : ${product.productManufacturer}</p>
<p><strong>Categorie</strong> : ${product.productCategory}</p>
<p><strong>Condition</strong> : ${product.productCondition}</p>
<p>${product.productPrice} DH</p>
<br>
<c:set var="role" scope="page" value="${param.role}" />
<c:set var="url" scope="page" value="/productList" />
<c:if test="${role='admin'}" >
<c:set var="url" scope="page" value="/admin/productInventory" />
</c:if>
<p ng-controller="cartCtrl">
<a href="<c:url value="${url}" /> " class="btn btn-default">Back</a>
<a href="#" class="btn btn-warning btn-large"
ng-click="addToCart('${product.productId}')"><span
class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</a>
<a href="<spring:url value="/cart" />"
class="btn btn-default"><span class="glyphicon glyphicon-hand-right"></span>View Cart</a>
</p>
</div>
</div>
</div>
<script src="<c:url value="/resources/js/controller.js" /> "></script>
<%@include file="/WEB-INF/views/template/footer.jsp"%>
And this is the cart.jsp. this is where the products added to the cart supposed to be and i see nothing
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@include file="/WEB-INF/views/template/header.jsp"%>
<div class="container-wrapper">
<div class="container">
<section>
<div class="jumbotron">
<div class="container">
<h1>Cart</h1>
<p>The products in your Cart</p>
</div>
</div>
</section>
<section class="container" ng-app="cartApp">
<div ng-controller = "cartCtrl" ng-init="initCartId('${cartId}')">
<div>
<a class="btn btn-danger pull-left" ng-click="clearCart()"><span
class="glyphicon glyphicon-remove-sign"></span>Clear</a>
</div>
<table class="table table-hover">
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Action</th>
</tr>
<tr ng-repeat = "item in cart.cartItems">
<td>{{item.productName}}</td>
<td>{{item.product.productPrice}}</td>
<td>{{item.quantity}}</td>
<td>{{item.totalPrice}}</td>
<td><a href="#" class="label label-danger" ng-click="removeFromCart(item.product.productId)">
<span class="glyphicon glyphicon-remove"></span>Supprimer</a></td>
</tr>
<tr>
<th></th>
<th></th>
<th>Total</th>
<th>{{cart.grandTotal}}</th>
<th></th>
</tr>
</table>
<a href="<spring:url value="/productList" />" class="btn btn-default">Continuer Shooping</a>
</div>
</section>
</div>
</div>
<script src="<c:url value="/resources/js/controller.js" /> "></script>
<%@include file="/WEB-INF/views/template/footer.jsp"%>
I have included Angular in the header section
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js" > </script>
When i tried to debug the cart.jsp page i have this message in the developers tool in Chrome " Failed to load resource: the server responded with a status of 406 (Not Acceptable) "
And this Apache error 406
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
Can anyone help me with this please