大家好,欢迎来到IT知识分享网。
一周前开始上手JSP,完成的第一个小项目:简易购物商城+购物车
效果图如下:
具体实现代码如下:
javabean:
MyTools.java(工具类,解决中文乱码问题和string转int)
package com.toolbean;
import java.io.UnsupportedEncodingException;
public class MyTools {
public static String toChinese(String str) {
if(str == null) {
str="";
}
try {
str = new String(str.getBytes("ISO-8859-1"),"gb2312");
}catch(UnsupportedEncodingException e) {
str="";
e.printStackTrace();
}
return str;
}
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;
}
}
GoodsSingle.java(商品基类)
package com.valuebean;
public class GoodsSingle {
private String name;
private int price;
private int num;
public GoodsSingle(String name, int f, int num){
this.name = name;
this.price = f;
this.num = num;
}
public GoodsSingle() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
GoodsList.java( GoodsSingle的集合类,用于记录商品列表)
package com.valuebean;
import java.util.ArrayList;
public class GoodsList {
private ArrayList<GoodsSingle> goodsList = new ArrayList<GoodsSingle>();
public ArrayList<GoodsSingle> getGoodList(){
return this.goodsList;
}
public GoodsSingle findGood(int i) {
return goodsList.get(i);
}
public void addGoods(GoodsSingle g) {
this.goodsList.add(g);
}
public void removeGoods(GoodsSingle g) {
int index = goodsList.indexOf(g);
this.goodsList.remove(index);
}
public void clearGoods() {
this.goodsList.clear();
}
}
ShopCar,java(GoodsSingle的集合类,用于记录购物车商品)
package com.valuebean;
import java.util.ArrayList;
import com.toolbean.MyTools;
public class ShopCar {
private ArrayList<GoodsSingle> buyList = new ArrayList<GoodsSingle>();
public ArrayList<GoodsSingle> getBuys(){
return this.buyList;
}
public GoodsSingle findGood(int i) {
return buyList.get(i);
}
public void addItem(GoodsSingle g) {
if(buyList.size() == 0) {
buyList.add(g);
}else {
int i = 0;
for(; i < buyList.size(); i++) {
if(g.getName().equals(buyList.get(i).getName())) {
buyList.get(i).setNum(buyList.get(i).getNum()+1);
break;
}
}
if(i >= buyList.size()) {
buyList.add(g);
}
}
}
public void reduceItem(GoodsSingle g) {
if(buyList.size() == 0) {
System.out.println("no item to reduce!");
}else {
int i = 0;
for(; i < buyList.size(); i++) {
if(g.getName().equals(buyList.get(i).getName())) {
if(buyList.get(i).getNum() > 1) {
buyList.get(i).setNum(buyList.get(i).getNum()-1);
break;
}else if(buyList.get(i).getNum() == 1) {
buyList.remove(buyList.get(i));
break;
}
}
}
}
}
public void removeItem(GoodsSingle g) {
if(buyList.size() == 0) {
System.out.println("没有可以删除的选项!");
}else {
buyList.remove(g);
}
}
public void clearItem(String name) {
if(buyList.size() == 0) {
}else {
int i = 0;
for(; i < buyList.size(); i++) {
if(MyTools.toChinese(name).equals(buyList.get(i).getName())) {
buyList.remove(i);
}
}
}
}
public void clearCar() {
buyList.clear();
}
}
styles.css
@charset "UTF-8";
table{
border: 1px;
width: 500px;
rules: none;
cellspacing:0px;
cellpadding:0px;
}
tr{
height: 50px;
}
td{
width:100px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
Index.jsp(初始化内容)
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.valuebean.GoodsSingle" %>
<jsp:useBean id="myGoodsList" class="com.valuebean.GoodsList" scope="session" />
<%
myGoodsList.clearGoods();
GoodsSingle g1 = new GoodsSingle("苹果",3,1);
GoodsSingle g2 = new GoodsSingle("香蕉",4,1);
GoodsSingle g3 = new GoodsSingle("梨",5,1);
GoodsSingle g4 = new GoodsSingle("橘子",6,1);
myGoodsList.addGoods(g1);
myGoodsList.addGoods(g2);
myGoodsList.addGoods(g3);
myGoodsList.addGoods(g4);
%>
<%
response.sendRedirect("ShowGoods.jsp");
%>
ShowGoods.jsp(商品展示界面)
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.valuebean.GoodsSingle" %>
<jsp:useBean id="myGoodsList" class="com.valuebean.GoodsList" scope="session"/>
<%
ArrayList goodsList = myGoodsList.getGoodList();
%>
<!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>购物商城</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<table>
<tr height="50">
<td colspan="3" align="center" ><h1> 购物商城</h1></td>
</tr>
<tr align="center" height="30" bgcolor="lightgrey">
<td></td>
<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 align="center" height="50">
<td><a href="DoCar.jsp?action=remove_goods&id=<%=i %>">
<img src="img/remove.png" width="30" height="30"/>
</a>
</td>
<td><%= single.getName() %></td>
<td><%= single.getPrice() %></td>
<td><a href="DoCar.jsp?action=buy&id=<%=i%>">加入购物车</a></td>
</tr>
<%
}
}
%>
<tr align="center" height="50">
<td align="left" colspan="3">
<a href="AddGoods.jsp">
添加商品
</a>
</td>
<td align="right" colspan="3"><a href="ShowCar.jsp">查看购物车</a></td>
</tr>
</table>
</body>
</html>
DoCar.jsp(处理相应请求)
<%@page import="com.valuebean.ShopCar"%>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.valuebean.GoodsSingle" %>
<%@ page import="com.toolbean.MyTools" %>
<jsp:useBean id="myCar" class="com.valuebean.ShopCar" scope="session"/>
<jsp:useBean id="myGoodsList" class="com.valuebean.GoodsList" scope="session"/>
<%
String action = request.getParameter("action");
if(action == null){
action = "";
session.setAttribute("flag", "heiehih");
}else if(action.equals("buy")){
int id = MyTools.strToInt(request.getParameter("id"));
GoodsSingle one = myGoodsList.findGood(id);
myCar.addItem(one);
response.sendRedirect("ShowGoods.jsp");
}else if(action.equals("remove_goods")){
int id = MyTools.strToInt(request.getParameter("id"));
GoodsSingle one = myGoodsList.findGood(id);
myGoodsList.removeGoods(one);
response.sendRedirect("ShowGoods.jsp");
}else if(action.equals("reduce_cargoods")){
int id = MyTools.strToInt(request.getParameter("id"));
GoodsSingle one = myCar.findGood(id);
myCar.reduceItem(one);
response.sendRedirect("ShowCar.jsp");
}else if(action.equals("add_cargoods")){
int id = MyTools.strToInt(request.getParameter("id"));
GoodsSingle one = myCar.findGood(id);
myCar.addItem(one);
response.sendRedirect("ShowCar.jsp");
}else if(action.equals("clear_item")){
int id = MyTools.strToInt(request.getParameter("id"));
GoodsSingle one = myCar.findGood(id);
myCar.removeItem(one);
response.sendRedirect("ShowCar.jsp");
}else if(action.equals("clear")){
myCar.clearCar();
response.sendRedirect("ShowCar.jsp");
}else{
response.sendRedirect("ShowGoods.jsp");
}
%>
AddGoods,jsp(添加商品界面)
<%@page import="com.valuebean.GoodsSingle"%>
<%@page import="com.toolbean.MyTools"%>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!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>添加商品</title>
</head>
<body>
<form action="DoAddGoods.jsp">
商品名称:<input type="text" name="name" size = "20"/>
<br>
商品价格:<input type="text" name="price" size = "20"/>
<br>
<input type="submit" value="添加">
</form>
</body>
</html>
DoAddGoods.jsp(处理添加商品请求)
<%@page import="com.valuebean.GoodsSingle"%>
<%@page import="com.toolbean.MyTools"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="myGoodsList" class="com.valuebean.GoodsList" scope="session" />
<%
String name = MyTools.toChinese(request.getParameter("name"));
int price=MyTools.strToInt(request.getParameter("price"));
GoodsSingle one = new GoodsSingle(name,price,1);
myGoodsList.addGoods(one);
response.sendRedirect("ShowGoods.jsp");
%>
ShowCar.jsp(购物车显示界面)
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.valuebean.GoodsSingle" %>
<jsp:useBean id="myCar" class="com.valuebean.ShopCar" scope="session"/>
<%
ArrayList buyList = myCar.getBuys();
float total = 0;
%>
<!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=gb2312">
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<title>购物车</title>
</head>
<body>
<table>
<tr height="50">
<td colspan="3" align="center" ><h1> 购物车</h1></td>
</tr>
<tr align="center" height="30" bgcolor="lightgrey">
<td>名称</td>
<td>价格</td>
<td>数量</td>
<td>清除</td>
</tr>
<% if(buyList == null || buyList.size() == 0){
%>
<tr height="100">
<td colspan="3" align="center">没有商品可以显示!</td>
</tr>
<%
}else{
for(int i = 0; i < buyList.size(); i++){
GoodsSingle single = (GoodsSingle)buyList.get(i);
float money = single.getPrice() * single.getNum();
total += money;
%>
<tr align="center" height="50">
<td><%= single.getName() %></td>
<td><%= single.getPrice() %></td>
<td>
<a href="DoCar.jsp?action=reduce_cargoods&id=<%=i%>" >
<img src="img/reduce.png" width="20" height="20"/>
</a>
<strong><%=single.getNum()%></strong>
<a href="DoCar.jsp?action=add_cargoods&id=<%=i%>">
<img src="img/add.png" width="20" height="20"/>
</a>
</td>
<td><a href="DoCar.jsp?action=clear_item&id=<%=i%>">移除</a></td>
</tr>
<%
}
}
%>
<tr height="50">
<td colspan="3" align="left" ><h4>应付金额:<%= total %></h4></td>
<br>
</tr>
<tr align="center" height="50">
<td align="left" colspan="3"><a href="ShowGoods.jsp">继续购物</td>
<td align="right" colspan="3"><a href="DoCar.jsp?action=clear">清空购物车</a></td>
</tr>
</table>
</body>
</html>
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21780.html