JavaBean技术的应用——购物车

复习了一下javaBean先关的内容,写了一个购物车功能:
先看看效果图吧:

所需要的工具与技术:javaEE(Eclipse)、Tomcat9.0、、javaBean技术

废话不多说,上代码:
GoodsSingle.java用于封装商品信息的“值javaBean”
package qau.edu;

public class GoodsSingle {
	private String name;//商品名
	private float price;//商品价格
	private int num;//商品数量

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

}
MyTools.java工具类javaBean
package qau.edu;

import java.io.UnsupportedEncodingException;

public class MyTools {
	// 转换中文乱码的问题
	public static String toChinese(String str) {
		if (str == "")
			str = "";
		try {
			// 通过String类的构造方法,将指定的字符串转换成gbk编码
			str = new String(str.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			str = "";
			e.printStackTrace();
		}
		return str;
	}
	//将String数据类型转换为int数据类型
	public static int strToint(String str){
		if(str==null||str.equals(""))
			str="0";
		int i=0;
		try {
			i=Integer.parseInt(str);
		} catch (NumberFormatException e) {
			i=0;
			e.printStackTrace();
		}
		return i;
	}
	
}


shopCar.java实现购物车功能的javaBean
package qau.edu;

import java.util.ArrayList;


public class ShopCar {
	private ArrayList<GoodsSingle> buylist = new ArrayList<GoodsSingle>();

	public ArrayList<GoodsSingle> getBuylist() {
		return buylist;
	}

	/**
	 * 向购物车中添加商品 参数single为GooleSigle类的对象
	 */
	public void addItem(GoodsSingle single) {
		if (buylist.size() == 0) {
			GoodsSingle temp = new GoodsSingle();
			temp.setName(single.getName());
			temp.setNum(single.getNum());
			temp.setPrice(single.getPrice());
			buylist.add(temp);
		} else {
			int i = 0;
			// 遍历集合查看是否已经存在药品添加的商品
			for (; i < buylist.size(); i++) {
				GoodsSingle temp = (GoodsSingle) buylist.get(i);
				if (temp.getName().equals(single.getName())) {
					temp.setNum(temp.getNum() + 1);
					break;
				}
			}
			if (i > buylist.size()) {
				GoodsSingle temp = new GoodsSingle();
				temp.setName(single.getName());
				temp.setNum(single.getNum());
				temp.setPrice(single.getPrice());
				buylist.add(temp);
			}
		}
	}
	/**
	 * 从购物车中移除指定的商品
	 */
	public void removeItem(String name){
		for (int i = 0; i < buylist.size(); i++) {
			GoodsSingle temp=(GoodsSingle) buylist.get(i);
			if(temp.getName().equals(MyTools.toChinese(name))){
				if(temp.getNum()>1){
					temp.setNum(temp.getNum()-1);
					break;
				}
				else if(temp.getNum()==1){
					buylist.remove(i);
				}
			}
		}
		
	}
	
	
	/**
	 * 清空购物车
	 */
	public void clearCar(){
		buylist.clear();
	}
}
index.jsp 用于初始化商品信息的列表(正式开发中应使用数据库进行数据的存储)
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	static ArrayList<GoodsSingle> goodslist = new ArrayList<GoodsSingle>();
	static {
		String[] names = { "苹果", "香蕉", "菠萝", "西瓜", "葡萄" };
		float[] prince = { 2.8f, 3.1f, 2.5f, 2.3f, 2.4f };
		for (int i = 0; i < 5; i++) {
			GoodsSingle single = new GoodsSingle();
			single.setName(names[i]);
			single.setPrice(prince[i]);
			single.setNum(1);
			goodslist.add(i, single);
		}
	}
	%>
	<%
		session.setAttribute("goodslist", goodslist);
		response.sendRedirect("show.jsp");
	%>
</body>
</html>

show.jsp 用于显示商品列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		ArrayList<GoodsSingle> goodslist =(ArrayList<GoodsSingle>)session.getAttribute("goodslist");
	%>
	<table border="1" rules="none" width="450" cellpadding="0"
		cellspacing="0">
		<tr height="50">
			<td colspan="3" align="center">提供商品如下:</td>
		</tr>
		<tr align="center" height="30" bgcolor="lightgryey">
			<td>名称</td>
			<td>价格</td>
			<td>购买</td>
		</tr>
		<%
			if (goodslist == null || goodslist.size() == 0) {
		%>
		<tr height="100">
			<td colspan="3" align="center">没有商品显示!</td>
		</tr>
		<%
			} else {
				for (int i = 0; i < goodslist.size(); i++) {
					GoodsSingle single = (GoodsSingle) goodslist.get(i);
		%>
		<tr height="50" align="center">
			<td><%=single.getName()%></td>
			<td><%=single.getPrice()%></td>
			<td><a href="docar.jsp?action=buy&id=<%=i%>">购买</a></td>
		</tr>
		<%
			}
			}
		%>
		<tr height="50">
			<td align="center" colspan="3"><a href="shopcar.jsp">查看购物车</a></td>
		</tr>
	</table>
</body>
</html>
docar.jsp 处理相应的action
<%@page import="com.sun.xml.internal.ws.encoding.MtomCodec"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
<%@page import="qau.edu.MyTools"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<jsp:useBean id="myCar" class="qau.edu.ShopCar" scope="session"></jsp:useBean>
	<%
		String action = request.getParameter("action");
		if (action == "")
			action = "";
		if (action.equals("buy")) {
			//购买商品
			ArrayList<GoodsSingle> goodslist = (ArrayList<GoodsSingle>) session.getAttribute("goodslist");
			int id = MyTools.strToint(request.getParameter("id"));
			GoodsSingle single = (GoodsSingle) goodslist.get(id);
			myCar.addItem(single);
			response.sendRedirect("show.jsp");
		} else if (action.equals("remove")) {
			//移除商品
			String name = request.getParameter("name");
			myCar.removeItem(name);
			response.sendRedirect("shopcar.jsp");

		} else if (action.equals("clear")) {
			myCar.clearCar();
			response.sendRedirect("shopcar.jsp");
		} else {
			response.sendRedirect("show.jsp");
		}
	%>

</body>
</html>

shopcar.jsp 显示购物车中的内容,清空购物车或删除商品

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<jsp:useBean id="myCar" class="qau.edu.ShopCar" scope="session"></jsp:useBean>
	<%
		ArrayList<GoodsSingle> buylist = myCar.getBuylist();
		float total = 0;
	%>
	<table border="1" rules="none" width="450" cellpadding="0"
		cellspacing="0">
		<tr height="50">
			<td colspan="5" align="center">购买商品如下:</td>
		</tr>
		<tr align="center" height="30" bgcolor="lightgryey">
			<td width="25%">名称</td>
			<td>价格(元/斤)</td>
			<td>数量</td>
			<td>总价(元)</td>
			<td>移除(-1/次)</td>
		</tr>
		<%
			if (buylist == null || buylist.size() == 0) {
		%>
		<tr height="100">
			<td colspan="5" align="center">您的购物车为空!</td>
		</tr>
		<%
			} else {
				for (int i = 0; i < buylist.size(); i++) {
					GoodsSingle single = (GoodsSingle) buylist.get(i);
					String name = single.getName();
					float price = single.getPrice();
					int num = single.getNum();
					float money = ((int) ((price * num + 0.05f) * 10)) / 10f;
					total += money;
		%>
		<tr height="50" align="center">
			<td><%=name%></td>
			<td><%=price%></td>
			<td><%=num%></td>
			<td><%=money%></td>
			<td><a href="docar.jsp?action=remove&name=<%=name%>">移除</a></td>
		</tr>
		<%
			}
			}
		%>
		<tr height="50" align="center">
			<td colspan="5">应付金额:<%=total%></td>
		</tr>
		<tr>
		    <td colspan="2"><a href="show.jsp">继续购买</a></td>
			<td colspan="3"><a href="docar.jsp?action=clear">清空购物车</a></td>
		</tr>
	</table>
</body>
</html>
关于购物车功能的相关代码已经全部贴在这了,下面是我在运行中出现的问题以及解决方法:

在catalina.properties中添加或者修改一个注释内容(改成下面这个模样):tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
至于原理是什么可以去百度搜索400-bad request的错误

猜你喜欢

转载自blog.csdn.net/DoWhatYouSay/article/details/79849296