2016-11-12 48 views
0

我正在嘗試使用JavaScript創建一個在線購物應用程序,它既可以創建項目,也可以創建並查看所有已完成的訂單。使用JavaScript的在線商店

我寫了下面的代碼迄今:

IndexPage.jsp 

<html> 
<head> 
    <title>Order Item Editing</title> 
</head> 
<body> 
<h1>Welcome to the Online Supermarket</h1> 
<h2>Please select one of the following options</h2> 
<div> 
<a href="/items/" class="btn btn-default">Items</a> 
<a href="/orders/" class="btn btn-default">Orders</a> 
</div> 
</body> 
</html> 



itemHomepage.jsp 

<html> 
<head> 
    <title>Summary of Items</title> 
</head> 
<body> 
<h2>Item Information</h2> 
<section> 
<a href="/item/itemDetails" class="btn btn-default">Add a new item</a> 
<a href="/" class="btn btn-default">Return to the home page</a> 
<p/> 
</section> 
<section> 
<table class="TFtable"> 
<tr> 
    <td><h3>Item Id</h3></td> 
    <td><h3>Name</h3></td> 
    <td><h3>Description of Item </h3></td> 
    <td><h3>Cost</h3></td> 
    <td><h3></h3></td> 
    <td><h3></h3></td> 
</tr> 
<c:forEach items="${itemList}" var="item"> 
<tr> 
    <td><c:out value="${item.getItemId()}"/></td> 
<td><c:out value="${item.getItemName()}"/></td> 
<td><c:out value="${item.getItemDescription()}"/></td> 
<td><c:out value="${item.getItemCost()}"/></td> 
<td><a href="/item/itemDetail?itemId=${item.getItemId()}">Edit Item</a></td> 
<td><a href="/item/delete?itemId=${item.getItemId()}">Delete Item</a></td> 
</tr> 
</c:forEach> 
</table> 
</section> 
</body> 
</html> 

itemDetail.jsp

OrderHomepage.jsp

<html> 
<head> 
    <title>Summary of Orders</title> 
</head> 
<body> 
<h2>Summary of Orders</h2> 
<section> 
<a href="/order/detailsOfOrders" class="btn btn-default">Add a new order</a> 
<a href="/" class="btn btn-default">Return to the Homepage</a> 
<p/> 
</section> 
<section> 
<table class="TFtable"> 
<tr> 
    <td><h3>Order Id</h3></td> 
    <td><h3>Total Cost</h3></td> 
    <td><h3></h3></td> 
    <td><h3></h3></td> 
</tr> 
<c:forEach items="${listOfOrders}" var="order"> 
<tr> 
    <td><c:out value="${order.getOrderId()}"/></td> 
    <td><c:out value="${order.getOrderCost()}"/></td> 
    <td><a href="/order/detailsOfOrders?orderId=${order.getOrderId()}">Edit</a></td> 
    <td><a href="/order/delete?orderId=${order.getOrderId()}">Delete</a></td> 
</tr> 
</c:forEach> 
</table> 
</section> 
</body> 
</html> 

detailsOfOrders.jsp

<html> 
<head> 
    <title>Order Information</title> 
</head> 
<body> 
<h2>Order Information</h2> 
<table> 
<form:form method="POST" commandName="order" action="/order/addOrder"> 
    <tr> 
    <td> <form:label path="id">ID: </form:label> </td> 
    <td> <form:input path="id" readonly="true"/> </td> 
    </tr> 
</table> 

</form:form> 
<section> 
<a href="/itemDetail" class="btn btn-default">Add new item</a> 
<a href="/order/" class="btn btn-default">Show all orders</a> 
<p/> 
</section> 
<table class="TFtable"> 
<tr> 
    <td><h3>Item Id</h3></td> 
    <td><h3>Name of Item</h3></td> 
    <td><h3>Price</h3></td> 
    <td><h3>Amount</h3></td> 
    <td><h3>Total Cost</h3></td> 
    <td><h3></h3></td> 
    <td><h3></h3></td> 
</tr> 
</table> 
</body> 
</html> 

我創建了一個控制器,如下所示:

package eMarket.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

import eMarket.EMarketApp; 
import eMarket.domain.Product; 
import eMarket.domain.Order; 

@Controller 
@RequestMapping("/order") 
public class OrderController { 

    @RequestMapping("/") 
    public String index(Model model) { 
     model.addAttribute("listOfOrders", EMarketApp.getShop().getlistOfOrders()); 
     return "form/orderHomepage"; 
    } 

    @RequestMapping(value = "/orderDetail", method = RequestMethod.GET) 
    public String orderDetail(@ModelAttribute("order") Order order, @RequestParam(value="orderId", required=false, defaultValue="-1") int orderId) { 
     if (orderId >= 0) { 
     // modify 
     Order o2 = EMarketApp.getShop().getlistOfOrders().stream().filter(o -> (((Order) o).getId() == orderId)).findAny().get(); 
     order.setId(o2.getId()); 
     } else { 
     // add 
     order.setId(); 
     } 
     return "form/orderDetail"; 
    } 

ItemController.java

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

import eMarket.EMarketApp; 
import eMarket.domain.Product; 
import eMarket.domain.Order; 
import eMarket.domain.Item; 

@Controller 
@RequestMapping("/item") 
public class ItemController { 

    @RequestMapping("/") 
    public String index(Model model) { 
     model.addAttribute("listOfItems", EMarketApp.getShop().getListOfProducts()); 
     return "form/productHomepage"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String productMaster(@ModelAttribute("product") Product product, Model model) { 
     EMarketApp.getShop().getListOfProducts().removeIf(p -> (p.getProductId() == product.getProductId())); 
     EMarketApp.getShop().getListOfProducts().add(product); 

     model.addAttribute("itemList", EMarketApp.getShop().getListOfProducts()); 
     return "item/itemDetail"; 

出於某種原因,當我嘗試的一個項目以一個新的秩序,我不是重新定向到itemDetail頁面。請你能告訴我哪裏出錯了?

+2

這是不相關的Java腳本 –

+0

'jsp'代表 「的Java服務器頁面」,而不是JavaScript的。 – Pointy

回答

0

我假定類級別@RequestMapping您ItemController是具有/項目等@RequestMapping如itemsHomePage.jsp所示

<a href="/item/itemDetails" class="btn btn-default">Add a new item</a> 

的值(「/項目」)。

我建議你做到以下幾點:

  1. 更改在detailsOfOrders.jsp從

    <a href="/itemDetail" class="btn btn-default">Add new item</a> 
    

    '添加新項的網址'(先從/件)到

    <a href="/item/itemDetail" class="btn btn-default">Add new item</a> 
    
  2. 如果問題仍然存在,請嘗試使用c:url值HREF值,如:

    <a href="<c:url value = '/item/itemDetail' />" class="btn btn-default">Add new item</a> 
    
+0

問題仍然存在:或許問題出在ItemController的某個地方? (請參閱上面的代碼編輯)。 – NoahSM1993

+0

你介意分享物品控制器的片段嗎? –

+0

我已經將它包含在上面問題的底部。 – NoahSM1993