初学者在这里。所以这里有一个我的代码示例供您跟踪:
/// if addToCart action is called
if (userPath.equals("/addToCart")) {
// if user is adding item to cart for first time
// create cart object and attach it to user session
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("cart", cart);
}
// get user input from request
String productId = request.getParameter("productId");
if (!productId.isEmpty()) {
Product product = productFacade.find(Integer.parseInt(productId));
cart.addItem(product);
}
userPath = "/category";
// if updateCart action is called
} else if (userPath.equals("/updateCart")) {
// get input from request
String productId = request.getParameter("productId");
String quantity = request.getParameter("quantity");
Product product = productFacade.find(Integer.parseInt(productId));
cart.update(product, quantity);
userPath = "/cart";
}
IDE 忽略第一个值并改为"/category"
分配。"/cart"
此问题会导致应用程序行为异常。例如,当用户在浏览器中点击添加到购物车时,它不会停留在类别页面上,而是直接进入购物车......
我需要使用未使用的值
... 我该如何解决?我已经尝试了几件事,但到目前为止没有成功......这确实很奇怪......我第一次遇到这个问题......
编辑
一些进展......昨天我花了一整天的时间试图解决仍然没有解决的问题......
这可能是一个需要报告的错误吗?
到目前为止我尝试过的修复:
- 删除了未使用的值......并重新输入它们显然没有做任何事情......
- 玩过括号
{}
(值得一试)...... 稍微更改了代码,并在类的开头调用了这些值,如下所示:
@WebServlet(name = "Controller", loadOnStartup = 1, urlPatterns = {"/category", "/addToCart",
ETC..
我认为这与未在某处关闭的语句有关?因为被分配的是最后一条语句,忽略了之前的语句......
应要求进行第二次编辑
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
HttpSession session = request.getSession();
ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
Wishlist wishlist = (Wishlist) session.getAttribute("wishlist");
/// if addToCart action is called
if (userPath.equals("/addToCart")) {
// if user is adding item to cart for first time
// create cart object and attach it to user session
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("cart", cart);
}
// get user input from request
String productId = request.getParameter("productId");
if (!productId.isEmpty()) {
Product product = productFacade.find(Integer.parseInt(productId));
cart.addItem(product);
}
userPath = "/category"; => UNUSED VALUE
// if updateCart action is called
} else if (userPath.equals("/updateCart")) {
// get input from request
String productId = request.getParameter("productId");
String quantity = request.getParameter("quantity");
Product product = productFacade.find(Integer.parseInt(productId));
cart.update(product, quantity);
userPath = "/cart"; => UNUSED VALUE
// if addToWishlist action is called
} else if (userPath.equals("/addToWishlist")) {
// if user is adding item to wishlist for first time
// create wishlist object and attach it to user session
if (wishlist == null) {
wishlist = new Wishlist();
session.setAttribute("wishlist", wishlist);
}
// get user input from request
String productId = request.getParameter("productId");
if (!productId.isEmpty()) {
Product product = productFacade.find(Integer.parseInt(productId));
wishlist.addItem(product);
}
userPath = "/category"; => UNUSED VALUE
// if updateWishlist action is called
} else if (userPath.equals("/updateWishlist")) {
// get input from request
String productId = request.getParameter("productId");
String quantity = request.getParameter("quantity");
Product product = productFacade.find(Integer.parseInt(productId));
wishlist.update(product, quantity);
userPath = "/wishlist"; => UNUSED VALUE
// if purchase action is called
} else if (userPath.equals("/purchase")) {
// extract user data from request
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String address_1 = request.getParameter("address_1");
String address_2 = request.getParameter("address_2");
String city = request.getParameter("city");
String State_Province_Region = request.getParameter("State_Province_Region");
String Postal_Zip_Code = request.getParameter("Postal_Zip_Code");
String country = request.getParameter("Country");
int orderId = 0;
// save order to database
// get order details
Map orderMap = orderManager.getOrderDetails(orderId);
// place order details in request scope
request.setAttribute("customer", orderMap.get("customer"));
request.setAttribute("products", orderMap.get("products"));
request.setAttribute("orderRecord", orderMap.get("orderRecord"));
request.setAttribute("orderedProducts", orderMap.get("orderedProducts"));
}
userPath = "/confirmation"; => USED VALUE FOR ALL OTHER ACTIONS BEFORE.
换句话说,IDE对他之前
userPath
的所有其他人使用最后一个 /confirmationuserPath
。所以无论我点击/addToCart
,/updateCart
,/addToWishlist
,/updateWishlist
, 分配的 userPath 都是最后一个 userPath/confirmation
,这意味着一切都被重定向到确认页面,它不应该是这样的。所有其他 userPath 都被忽略并设置为unused value
...我不知道如何解决这个问题。