小編給大家分享一下eclipse中web項(xiàng)目如何實(shí)現(xiàn)Javaweb購物車,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比阜新網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式阜新網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋阜新地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
具體實(shí)現(xiàn):
首先我們要看一下項(xiàng)目整體的結(jié)構(gòu)
下面我們要先創(chuàng)建實(shí)體類,就是我們的商品、預(yù)購商品和購物車這三個(gè)實(shí)體類。
Beans
Cart類(這個(gè)類是購物車實(shí)體類,包含了購物車中添加的商品和總計(jì)兩個(gè)屬性。)
package Beans; import java.util.HashMap; public class Cart { private HashMapcartItems=new HashMap ();//購物車中添加的商品 private double total;//總計(jì) public HashMap getCartItems() { return cartItems; } public void setCartItems(HashMap cartItems) { this.cartItems = cartItems; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
CartItem類(這個(gè)是購物車中添加的商品類,包含有商品、商品個(gè)數(shù)和小計(jì))
package Beans; public class CartItem { private Product product;//商品 private int buyNum;//個(gè)數(shù) private double subTotal;//小計(jì) public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getBuyNum() { return buyNum; } public void setBuyNum(int buyNum) { this.buyNum = buyNum; } public double getSubTotal() { return subTotal; } public void setSubTotal(double subTotal) { this.subTotal = subTotal; } }
Product類 (這里是具體的商品類,包含有商品編號(hào)、商品名和商品價(jià)格三個(gè)屬性)
package Beans; public class Product { private String pid;//商品編號(hào) private String name;//商品名 private double price;//商品價(jià)格 public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Product(String pid,String name,double price) { // TODO Auto-generated constructor stub this.pid = pid; this.name = name; this.price = price; } }
Service
這個(gè)包下面只有一個(gè)類,主要的作用是存入商品,并能根據(jù)商品編號(hào)找到商品。
ProductService類
package Service; import java.util.HashMap; import Beans.CartItem; import Beans.Product; public class ProductService { private HashMapcartItems=new HashMap (); public ProductService() //構(gòu)造函數(shù) { CartItem cartltem1=new CartItem(); CartItem cartltem2=new CartItem(); Product product1=new Product("001","Mobilephone",1000); Product product2=new Product("002","Watch",100); cartltem1.setProduct(product1); cartltem2.setProduct(product2); cartItems.put("001",cartltem1); cartItems.put("002", cartltem2); } public Product findProductbypid(String pid) { CartItem cartItem=cartItems.get(pid); Product product=cartItem.getProduct(); return product; } }
Servelet
ProductServlet類 (在這經(jīng)常會(huì)報(bào)錯(cuò) 1、httpservelet類無法繼承;因?yàn)閔ttpservelet類是在tomcat下的所以這里可能是tomcat沒有配置入項(xiàng)目或者h(yuǎn)ttpservelet類沒有導(dǎo)入,所以要重新導(dǎo)入tomcat。2、dopost和doget兩種基礎(chǔ)方法使用錯(cuò)誤,導(dǎo)致頁面?zhèn)鱽淼臄?shù)據(jù)無法進(jìn)行處理;解決:servelet類中的方法要與頁面選擇方法一致。3、亂碼,中文亂碼;解決:中文的編碼最好用utf-8【servlet改編碼是對(duì)req、resp設(shè)置】,并且頁面和后臺(tái)采用的編碼要一致。)
這里的路徑配置采用的是標(biāo)簽(方便)、也可采用.xml配置.
package Servlet; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Beans.Cart; import Beans.CartItem; import Beans.Product; import Service.ProductService; @WebServlet("/productServlet") public class ProductServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ProductService productService = new ProductService(); String pid=(String)req.getParameter("Product"); int buyNum=Integer.parseInt(req.getParameter("number")); HttpSession session=req.getSession(); Cart cart=(Cart)session.getAttribute("cart"); if(cart==null) { cart=new Cart(); } CartItem cartItem=new CartItem(); cartItem.setBuyNum(buyNum); Product product=productService.findProductbypid(pid); cartItem.setProduct(product); double subTotal=product.getPrice()*buyNum; cartItem.setSubTotal(subTotal); HashMapcartItems=cart.getCartItems(); double newSubTotal=0; if(cartItems.containsKey(pid)) { CartItem item=cartItems.get(pid); int oldBuyNum= item.getBuyNum(); oldBuyNum=oldBuyNum+buyNum; item.setBuyNum(oldBuyNum); double oldSubTotal= item.getSubTotal(); newSubTotal=buyNum*product.getPrice(); oldSubTotal=oldSubTotal+newSubTotal; item.setSubTotal(oldSubTotal); } else { cartItems.put(pid, cartItem); newSubTotal=buyNum*product.getPrice(); } double total=cart.getTotal()+newSubTotal; cart.setTotal(total); cart.setCartItems(cartItems); session.setAttribute("cart", cart); req.getRequestDispatcher("cart.jsp").forward(req, resp); } }
cart.jsp
這里一定要導(dǎo)入其他類 ,用<%@ page import=""%>標(biāo)簽。
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="Beans.Cart" %> <%@ page import="Beans.CartItem" %> <%@ page import="Beans.Product" %> <%@page import="java.util.*"%>Insert title here <% Cart cart=(Cart)request.getSession().getAttribute("cart"); if(cart==null) { %>It is nothing!
<% } else{ HashMapcartItems=cart.getCartItems(); double total=cart.getTotal(); %> Your product list:
<% Setkeys=cartItems.keySet(); Iterator iter = keys.iterator(); while(iter.hasNext()){ String key= iter.next(); CartItem cartItem=cartItems.get(key); Product product=cartItem.getProduct(); out.print(product.getName()+" ") ; out.println("price:"+product.getPrice()+"$") ; out.println("number:"+cartItem.getBuyNum()); }; %>
<% out.print(" total:"+total+"$"); } %>
index.jsp
在action=“”屬性的配置是不能只寫后臺(tái)配置的“/productServlet”路徑,一定要加上<%=request.getContextPath() %>,否則有可能找不著路徑。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>Insert title here Please select the item you want to buy.
以上是“eclipse中web項(xiàng)目如何實(shí)現(xiàn)Javaweb購物車”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!