Java I';我试图用Spring建立一个电子商务网站。我添加了购物车功能。但当我点击订单按钮时,购物车没有更新
CartItemControllerJava I';我试图用Spring建立一个电子商务网站。我添加了购物车功能。但当我点击订单按钮时,购物车没有更新,java,spring,spring-boot,spring-mvc,jsp,Java,Spring,Spring Boot,Spring Mvc,Jsp,CartItemController package com.emusicstore.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annot
package com.emusicstore.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/cart")
public class CartItemController {
@RequestMapping
public String get(HttpServletRequest request)
{
return "redirect:/cart/"+request.getSession(true).getId(); //Session id is used as cart Id
}
@RequestMapping(value="/{cartId}",method= RequestMethod.GET)
public String getCart(@PathVariable(value="cartId") String cartId, Model model)
{
model.addAttribute("cartId",cartId);
return "cart";
}
}
CartController.java
package com.emusicstore.controller;
import com.emusicstore.dao.CartDao;
import com.emusicstore.dao.ProductDao;
import com.emusicstore.model.Cart;
import com.emusicstore.model.CartItem;
import com.emusicstore.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/rest/cart")
//Rest services we need to start,starts with rest in path
public class CartController {
@Autowired
private CartDao cartDao;
@Autowired
private ProductDao productDao;
@RequestMapping(value="/{cartId}",method= RequestMethod.GET)
public @ResponseBody Cart read(@PathVariable(value ="cartId") String cartId)
{
return cartDao.read(cartId);
}
//@Response Body returns a model object in JSON Format
//Cart object which is returned is put into @ResponseBody and because of jackson deendendency The sping converts the object
//to JSON format and puts in @ResponseBody
//Reverse Process
@RequestMapping(value="/{cartId}",method=RequestMethod.PUT)
@ResponseStatus(value= HttpStatus.NO_CONTENT)
public void update(@PathVariable(value="cartId") String cartId, @RequestBody Cart cart)
{
cartDao.update(cartId,cart);
}
//Read something-GET
//Post something in UrL=POST
//Update info-PUT
@RequestMapping(value="/{cartId}",method=RequestMethod.DELETE)
@ResponseStatus(value=HttpStatus.NO_CONTENT)
public void delete(@PathVariable(value="cartId")String cartId)
{
cartDao.delete(cartId);
}
@RequestMapping(value="/add/{productId}",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}",method = RequestMethod.PUT)
@ResponseStatus(value=HttpStatus.NO_CONTENT)
public void removeItem(@PathVariable 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="Illegal request, Please your verify your payload")
public void handleClientErrors(Exception e)
{
}
@ExceptionHandler(Exception.class)
@ResponseStatus(value= HttpStatus.INTERNAL_SERVER_ERROR,reason="Internal Server Error")
public void handleServerErrors(Exception e)
{
}
}
CartDaoImpl.java
package com.emusicstore.dao.impl;
import com.emusicstore.dao.CartDao;
import com.emusicstore.model.Cart;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;
@Repository
public class CartDaoImpl implements CartDao {
private Map<String, Cart> listOfCarts;
public CartDaoImpl()
{
listOfCarts=new HashMap<String, Cart>();
}
@Override
public Cart create(Cart cart) {
if(listOfCarts.keySet().contains(cart.getCartId())){
throw new IllegalArgumentException(String.format("Cannot create a cart. A cart " +
"with given id(%)"+"already"+"exists",cart.getCartId()));
}
listOfCarts.put(cart.getCartId(),cart);
return cart;
}
@Override
public Cart read(String cartId) {
return listOfCarts.get(cartId);
}
@Override
public void update(String cartId, Cart cart) {
if(!listOfCarts.keySet().contains(cartId))
{
throw new IllegalArgumentException(String.format("Cannot update cart. The cart with the given id(%)" +
"does not"+"exists.",cart.getCartId()));
}
listOfCarts.put(cartId,cart);
}
@Override
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);
}
}
当我尝试执行它时,我会看到产品页面,但在按下“立即订购”按钮后,url会变为
其中1是productId,但之后没有任何弹出窗口说明订单已经下好,当我查看购物车时,我只得到我在jsp脚本中使用的变量名称,而不是值。当我尝试调试时,我发现来自viewProduct.jsp的ng click没有执行任何操作。请帮我解决这个问题…jsp中是否包含angular js库?看看这个帅哥。jsp页面现在工作正常。但是当我点击orderNow按钮时,购物车没有更新。谢谢兄弟,现在一切都很好。我为src制作的类型为scr。现在它起作用了……不客气,我很高兴它起了作用
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="/WEB-INF/views/template/header.jsp" %>
<div class="container-wrapper">
<div class="container">
<div class="page-header">
<h1>Product Detail</h1>
<p class="lead">Here is the detailed information of the products</p>
</div>
<div class="container-fluid" ng-app="cartApp">
<div class="row">
<div class="col-md-5">
<img src="<c:url value="/resources/images/${product.productId}.png"/>" alt="image" style="width:100%"/>
<!-- ;height:300px-->
</div>
<div class="col-md-5">
<h3>${product.productName}</h3>
<p>${product.productDescription}</p>
<p>
<strong>Manufacturer</strong>:${product.productManufacturer}
</p>
<p>
<strong>Category</strong>:${product.productCategory}
</p>
<p>
<strong>Condition</strong>:${product.productCondition}
</p>
<p>
<h4>${product.productPrice} USD</h4>
</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>Order Now</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>
</div>
</div>
<script scr="<c:url value="/resources/js/controller.js"/>"></script>
<%@include file="/WEB-INF/views/template/footer.jsp" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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>All the selected products in your shopping cart</p>
</div>
</div>
</section>
<section class="container" ng-app="cartApp">
<div ng-controller="cartCtrl" ng-init="initCartId('${cartId}')">
<div>
<a class="bnt btn-danger pull-left" ng-click="clearCart()"><span class="glyphicon glyphicon-remove-sign"></span>Clear Cart</a>
</div>
<table class="table table-hover">
<tr>
<th>Product</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr ng-repeat="item in cart.cartItems">
<td>{{item.product.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>remove</a></td>
</tr>
<tr>
<th></th>
<th></th>
<th>Grand Total</th>
<th>grandTotal</th>
</tr>
</table>
<a href="<spring:url value="productList"/>"class="btn btn-default">Continue Shopping</a>
</div>
</section>
</div>
</div>
<script src="<c:url value="/resources/js/controller.js"/>"></script>
<%@include file="/WEB-INF/views/template/footer.jsp" %>
var cartApp=angular.module("cartApp",[]);
cartApp.controller("cartCtrl",function($scope,$http){
$scope.refreshCart=function(cartId){
$http.get('/eMusicStore_war_exploded/rest/cart/'+$scope.cartId).success(function (data){
$scope.cart=data;
});
};
$scope.clearCart=function(){
$http.delete('/eMusicStore_war_exploded/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('/eMusicStore_war_exploded/rest/cart/add/'+productId).success(function (data){
$scope.refreshCart($http.get('/eMusicStore_war_exploded/rest/cart/cartId'));
alert('Product successfully added to cart!')
});
};
$scope.removeFromCart=function(productId){
$http.put('/eMusicStore_war_exploded/rest/cart/remove/'+productId).success(function(data){
$scope.refreshCart($http.get('eMusicStore_war_exploded/rest/cart/cartId'));
});
};
});