2016-04-30 1 views
0

もう何もわかりません。

IVEは、約1ヶ月間、この問題を抱えてそのここudemyチュートリアルでは、チュートリアルに基づいてbuilding an eccomerce store java spring mvc

チュートリアル自体が、あなたはかなり彼をコピーして、それがために

を作品祈るする必要が0のファイルに付属していますcartitemsがデータベースに残っている何らかの理由がありますが、ポストの後のcartビューではbieng placeではありません。イム私はデータに誤りが角が、カントであると信じてそうイムリードを永続化され、非常にこれを信じるため

私の理由があることであることを確認することがわかります

をデータバインディングの双方向のためagular JSを使用して誤差は、角度を解析する際にモデルに合わせて調整され、プログラムに多く反映されていました。私はプログラマーよりもscifi buffのように話していますが、これはこのようなことを記述する最善の方法です。

おかげでみんな任意のヘルプはHERESに

角度

/** 
* Created by dwight on 4/8/2016. 
*/ 

var cartApp = angular.module ('cartApp', []); 

cartApp.controller('cartCtrl', function ($scope, $http) { 

    $scope.refreshCart = function() { 
     $http.get('/emusicstore/rest/cart/' + $scope.cartId).success(function (data) { 
      $scope.cart = data; 

     }); 

    }; 

    $scope.clearCart = function() { 
     $http.delete('/emusicstore/rest/cart/' + $scope.cartId).success($scope.refreshCart()); 
    }; 

    $scope.initCartId = function(cartId){ 
     $scope.cartId = cartId; 
     $scope.refreshCart(cartId); 
    }; 

    $scope.addToCart = function(productId){ 
     $http.put('/emusicstore/rest/cart/add/'+productId).success(function(){ 
      alert('Product successfully added to the cart!') 
     }); 
    }; 

    $scope.removeFromCart = function(productId){ 
     $http.put('/emusicstore/rest/cart/remove/'+productId).success(function(data){ 
      $scope.refreshCart(); 


     }); 
    }; 

    $scope.calGrandTotal = function(){ 
     var grandTotal=0; 

     for(var i=0; i<$scope.cart.cartItems.length; i++){ 
      grandTotal+=$scope.cart.cartItems[i].totalPrice; 
     } 

     return grandTotal; 
    }; 


}); 

heres the model 

    package com.emusicstore.model; 

import com.fasterxml.jackson.annotation.JsonIgnore; 

import javax.persistence.*; 

import java.io.Serializable; 

/** 
* Created by dwight on 4/4/2016. 
*/ 
@Entity 
public class CartItem implements Serializable{ 


private static final long serialVersionUID = -904360230041854157L; 

@Id 
@GeneratedValue 
private int cartItemId; 

@ManyToOne 
@JoinColumn(name = "cartId") 
@JsonIgnore 
private Cart cart; 

@ManyToOne 
@JoinColumn(name = "productId") 
private Product product; 


private int quantity; 
private double totalPrice; 

public int getCartItemId() { 
    return cartItemId; 
} 

public void setCartItemId(int cartItemId) { 
    this.cartItemId = cartItemId; 
} 

public Cart getCart() { 
    return cart; 
} 

public void setCart(Cart cart) { 
    this.cart = cart; 
} 

public Product getProduct() { 
    return product; 
} 

public void setProduct(Product product) { 
    this.product = product; 
} 

public int getQuantity() { 
    return quantity; 
} 

public void setQuantity(int quantity) { 
    this.quantity = quantity; 
} 

public double getTotalPrice() { 
    return totalPrice; 
} 

public void setTotalPrice(double totalPrice) { 
    this.totalPrice = totalPrice; 
} 

}

heres the dao or "data accessing object" 

    package com.emusicstore.dao.impl; 


import com.emusicstore.dao.CartItemDao; 
import com.emusicstore.model.Cart; 
import com.emusicstore.model.CartItem; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional; 

import java.util.List; 

/** 
* Created by dwight on 4/18/2016. 
*/ 

@Repository 
@Transactional 
public class CartItemDaoImpl implements CartItemDao{ 

    @Autowired 
    private SessionFactory sessionFactory; 

    public void addCartItem(CartItem cartItem){ 
     Session session = sessionFactory.getCurrentSession(); 
     session.saveOrUpdate(cartItem); 
     session.flush(); 
    } 

    public void removeCartItem(CartItem cartItem){ 
     Session session = sessionFactory.getCurrentSession(); 
     session.delete(cartItem); 
     session.flush(); 


    } 

    public void removeAllCartItems(Cart cart){ 
     List<CartItem> cartItems = cart.getCartItems(); 

     for(CartItem item : cartItems){ 
      removeCartItem(item); 

     } 
    } 

    public CartItem getCartItemByProductId(int productId){ 
     Session session = sessionFactory.getCurrentSession(); 
     Query query = session.createQuery("from CartItem where productId = ?"); 
     query.setInteger(0, productId); 
     session.flush(); 

     return (CartItem)query.uniqueResult(); 

    } 

} 

最後にビュー

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@include file="/WEB-INF/views/templates/header.jsp" %> 

<div class="container-wrapper"> 
    <div class="container"> 
     <section> 
      <div class="jumbotron"> 
       <div class="container"> 
        <h1>Cart</h1> 

        <p>Selected products in your shopping cart</p> 
       </div> 
      </div> 
     </section> 

    <section> 
     <div 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 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>{{calGrandTotal()}}</th> 
         <th></th> 

        </tr> 

       </table> 
       <a href="<spring:url value="/product/productlist"/> " class="btn btn-default">Continue Shopping</a> 
      </div> 
     </div> 

    </section> 
    </div> 
</div> 



<script src="<c:url value="/resources/js/controller.js"/> "></script> 
<%@include file="/WEB-INF/views/templates/footer.jsp" %> 

いくつかの厄介な点灯するためのコントローラをいただければ幸いです

<tr ng-repeat="item in cart.cartItems"> 

しかし、あなたはいずれかを持っているように、あなたのコントローラでは、それは見ていません:TLE絵はもう病気に喜んで必要なthatsのcartItems error

おかげで、あなたがcart.cartItemsを参照しているビューに感謝

答えて

0

を提供しますサーバーから取得したものを$ scope.cart.cartItemsに割り当てるコードです。

$ http.getが必要なものを正確に返している場合、$ scope.cart.cartItemsにデータを割り当てることは、おそらくあなたが欠けているものです。

+0

私が与えたことに基づいて、どのように私はそれについて行くだろう。私は手がかりを持っていません....私はコードをチェックして、他の夜には、私は誰かがインターネット上に人のコードを投稿した人を見つけるためにすべてに行った。私はあなたが私のプログラムを完全に理解していることを求めているわけではありませんが、私は明示的に$ scope.cart.cartItems.lengthにループのcalGrandTotalを何らかの方法で追加します。 –

関連する問題