eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購(gòu)物車的方法
本文將帶領(lǐng)大家實(shí)現(xiàn)第一個(gè)用eclipse寫的第一個(gè)Javaweb項(xiàng)目–簡(jiǎn)單購(gòu)物車。文章會(huì)在問題分析、具體實(shí)現(xiàn)和常見問題這三塊為大家詳細(xì)解說。
問題分析:
首先我們要了解我們要完成的是什么----購(gòu)物車。然后那實(shí)現(xiàn)購(gòu)物車的什么呢?是不是往購(gòu)物車添加心儀商品呢。那是不是還要實(shí)現(xiàn)價(jià)格的計(jì)算呢?既然我們了解問題本質(zhì)了,那我們接下來就要進(jìn)行具體實(shí)現(xiàn)了。
具體實(shí)現(xiàn):
首先我們要看一下項(xiàng)目整體的結(jié)構(gòu)

下面我們要先創(chuàng)建實(shí)體類,就是我們的商品、預(yù)購(gòu)商品和購(gòu)物車這三個(gè)實(shí)體類。
Beans
Cart類(這個(gè)類是購(gòu)物車實(shí)體類,包含了購(gòu)物車中添加的商品和總計(jì)兩個(gè)屬性。)
package Beans;
import java.util.HashMap;
public class Cart {
private HashMap<String,CartItem> cartItems=new HashMap<String,CartItem>();//購(gòu)物車中添加的商品
private double total;//總計(jì)
public HashMap<String, CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(HashMap<String, CartItem> cartItems) {
this.cartItems = cartItems;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
}
CartItem類(這個(gè)是購(gòu)物車中添加的商品類,包含有商品、商品個(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 HashMap<String,CartItem> cartItems=new HashMap<String,CartItem>();
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);
HashMap<String, CartItem> cartItems=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.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% Cart cart=(Cart)request.getSession().getAttribute("cart");
if(cart==null) {
%>
<p>It is nothing!</p>
<%
}
else{
HashMap<String, CartItem> cartItems=cart.getCartItems();
double total=cart.getTotal();
%>
Your product list:<br>
<%
Set<String> keys=cartItems.keySet();
Iterator<String> 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());
};
%>
<br>
<%
out.print(" total:"+total+"$");
}
%>
</body>
</html>
index.jsp
在action=“”屬性的配置是不能只寫后臺(tái)配置的“/productServlet”路徑,一定要加上<%=request.getContextPath() %>,否則有可能找不著路徑。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> Please select the item you want to buy.<br> <form action="<%=request.getContextPath() %>/productServlet" method="post"> Mobile phone(1000$) <input name="Product" type="radio" value="001" checked="checked"><br> Watch(100$) <input name="Product" type="radio" value="002"><br> please input the number! number:<input name="number" type="number"><br> <input type="submit" value="ok!"> </form> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea中創(chuàng)建jsp項(xiàng)目的詳細(xì)實(shí)戰(zhàn)步驟
才學(xué)javaWeb,以防自己忘記創(chuàng)建項(xiàng)目的過程,所以淺淺的記錄一下吧,下面這篇文章主要給大家介紹了關(guān)于idea中創(chuàng)建jsp項(xiàng)目的詳細(xì)步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Spring Data JPA進(jìn)行數(shù)據(jù)分頁與排序的方法
這篇文章主要介紹了Spring Data JPA進(jìn)行數(shù)據(jù)分頁與排序的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
基于spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯
這篇文章主要介紹了spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

