我正在关注使用Intellij Idea中的spring框架构建电子商务Web应用程序的教程,我对购物车功能有疑问。
功能正常:我可以添加产品但是当我想在购物车中显示产品时没有显示
这是我的代码
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);
}
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);
}
}
模型(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();
}
}
}
controller.js文件,其中包含购物车功能的所有功能
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'));
});
};
});
JSP文件viewProduct我在哪里可以看到&#34;添加到购物车按钮&#34;和#34;查看购物车&#34;按钮
<%@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"%>
这是cart.jsp。这是添加到购物车的产品应该是什么,我什么也看不见
<%@ 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"%>
我在标题部分中包含了Angular
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js" > </script>
当我尝试调试cart.jsp页面时,我在Chrome " Failed to load resource: the server responded with a status of 406 (Not Acceptable) "
这个Apache错误406
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
任何人都可以帮我这个吗