2016-04-30 12 views
0

ich weiß nicht mehr, was wirklich ist.

ive für etwa 1 Monat dieses Problem mit seiner basierend auf einem Tutorial Udemy Tutorial hier building an eccomerce store java spring mvc

das Tutorial selbst mit 0 Dateien kamen Sie haben ziemlich viel um ihn zu kopieren und beten es

arbeitet

für Ein Grund dafür ist, dass die Cartitems in der Datenbank verbleiben, jedoch nicht nach der Post auf der Cart-Ansicht angezeigt werden. im mit Agular js für die Zwei-Wege-Datenbindung

ich sehen kann, dass die Daten so im Blei halten an zu glauben, dass der Fehler bei dem Winkel ist aber kann nicht ganz sicher sein,

mein Grund, dies zu der Annahme besteht, dass das ist Der Fehler wäre beim Parsen durch den Winkel und mehr in das Programm mehr an die Modelle angepasst worden. Ich rede mehr wie ein Scifi Buff als ein Programmierer, aber das ist der beste Weg, um so etwas zu beschreiben.

Danke Jungs würde jede mögliche Hilfe

heres die Steuerung für Winkel

/** 
* 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(); 

    } 

} 

und schließlich der Ansicht geschätzt werden

<%@ 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" %> 

einige böse beleuchteter tle Bilder cartItems error

dank mehr krank brauchte thats, bieten gerne dank

Antwort

0

Ihrer Ansicht Sie verweisen cart.cartItems:

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

aber in der Controller, ist es nicht so aussehen wie Sie welche haben Code, der alles, was Sie vom Server abrufen, $ scope.cart.cartItems zuweist.

Wenn das $ http.get genau das zurückgibt, was Sie benötigen, dann fehlt wahrscheinlich die Zuordnung von Daten zu $ ​​scope.cart.cartItems.

+0

Basierend auf was ich gegeben habe, wie würde ich darüber gehen. Ich habe keine Ahnung ... Ich habe den Code neulich überprüft und bin sogar rausgegangen, um jemanden zu finden, der die Jungs Code b im Internet gepostet hat. Ich verlange nicht, dass Sie mein Programm vollständig verstehen, sondern dass ich calGrandTotal für die Schleife zu $ ​​scope.cart.cartItems.length in irgendeiner Weise hinzufügen soll und wenn ja, wie –