我正在尝试使用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
<html>
<head>
<title> Edit Item </title>
</head>
<body>
<h2>Add Item Information</h2>
<form:form method="POST" commandName="item" action="/item/add">
<table>
<tr>
<td><form:label path="itemId">Id</form:label></td>
<td><form:input path="itemID" readonly="true"/></td>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="description">Description</form:label></td>
<td><form:input path="description" /></td>
</tr>
<tr>
<td><form:label path="cost">Cost</form:label></td>
<td><form:input path="cost" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" class="btn btn-default"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
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页面。请问你能告诉我哪里出错了吗?
答案 0 :(得分:0)
我假设您的ItemController的类级别@RequestMapping的值为/ item,如@RequestMapping(&#34; / item&#34;),如
所示 <a href="/item/itemDetails" class="btn btn-default">Add a new item</a>
itemHomePage.jsp中的。
我建议你做以下事情:
更改“添加新项目网址&#39;在detailsOfOrders.jsp(从 / item 开始)来自
<a href="/itemDetail" class="btn btn-default">Add new item</a>
到
<a href="/item/itemDetail" class="btn btn-default">Add new item</a>
如果问题仍然存在,请尝试将c:url值作为href值,如:
<a href="<c:url value = '/item/itemDetail' />" class="btn btn-default">Add new item</a>