标签:
WEB应用总览
1.建立好相应的包,从底层开始开发
domain包添加Book类
package cn.itcast.domain; public class Book { private String id; private String name; private String author; private Double price; private String description; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String id, String name, String author, Double price, String description) { super(); this.id = id; this.name = name; this.author = author; this.price = price; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
2 用一个类模拟数据库,map保存书本
package cn.itcast.DB; import java.util.LinkedHashMap; import java.util.Map; import cn.itcast.domain.Book; //代表数据库 public class DB { private static Map map=new LinkedHashMap(); static{ map.put("1", new Book("1","javaweb", "张三", 20.0, "一本好书")); map.put("2", new Book("2","jsp", "李四", 50.0, "一本好书")); map.put("3", new Book("3","html", "王五", 40.0, "一本好书")); map.put("4", new Book("4","Spring", "张韧", 60.0, "一本好书")); map.put("5", new Book("5","Ajax", "张文博", 20.0, "一本好书")); map.put("6", new Book("6","struts", "周利强", 40.0, "一本好书")); } public static Map getAll(){ return map; } }
3 操作数据库的Dao类
package cn.itcast.dao; import java.util.Map; import cn.itcast.DB.DB; import cn.itcast.domain.Book; public class BookDao { public Map getAll(){ return DB.getAll(); } public Book find(String id){ return (Book) DB.getAll().get(id); } }
4 初步写出业务层
package cn.itcast.service; import java.util.Map; import cn.itcast.dao.BookDao; import cn.itcast.domain.Book; import cn.itcast.domain.Cart; public class BusinessService { private BookDao dao=new BookDao(); public Map getAllBook(){ return dao.getAll(); } public Book findBook(String id){ return dao.find(id); }
5 图书商城首页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>图书商城</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body style="text-align: center"> <a style="font-size:50px" href="${pageContext.request.contextPath }/servlet/ListBookServlet">浏览书籍</a> <br> </body> </html>
6 由上可知,点击首页的浏览,跳转到一个WBE层的ListBookServlet
public class ListBookServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BusinessService service=new BusinessService(); //WEB层统一调用业务层方法 Map map=service.getAllBook(); //获取数据库,并转发到一个jsp页面 request.setAttribute("map", map); request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
7 在WEB-INF下新建目录jsp,再在jsp中 新建 listbook.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%-- 导入jstl包,将c.tld标签库引入 --%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>书籍列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body style="text-align: center"> <h1>书籍列表</h1> <center> <table style="align: center" border="1" width="80%" > <tr > <td >书名</td> <td>作者</td> <td>售价</td> <td>描述</td> <td>操作</td> </tr> <c:forEach var="entry" items="${map }"> <%-- JSTL中forEach标签,遍历map --%> <tr> <td>${entry.value.name} </td> <td>${entry.value.author}</td> <td>${entry.value.price}</td> <td>${entry.value.description}</td> <td> <a href="${pageContext.request.contextPath}/servlet/BuyServlet?id=${entry.value.id}" target="_blank">购买</a> </td> </tr> </c:forEach> </center> </body> </html>
发布到服务器测试成功后接着完善功能
8 <a href="${pageContext.request.contextPath}/servlet/BuyServlet?id=${entry.value.id}" target="_blank">购买</a>
将要买的书的id发送到BuyServlet
所以我们准备WEB层新建BuyServlet
但此时有一个疑问,买的商品要放到哪里呢?正确的做法是买到购物车中,再结算,而购物车的每个购物项目都有自己相应的数目
so,先在domain中建立购物车Cart和购物项CartItem类。
package cn.itcast.domain; import java.util.Map; //存放书的购物项 public class CartItem { private Book book; //书 private int quantity; //书的数量 private double price; //购物项的总价 public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public int getQuantity() { return quantity; } //一旦数量改变,购物项的总价就改变 public void setQuantity(int quantity) { this.quantity = quantity; this.price=this.book.getPrice()*this.quantity; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
package cn.itcast.domain; import java.util.LinkedHashMap; import java.util.Map; //代表购物车 public class Cart { private Map<String,CartItem> map=new LinkedHashMap(); private double price; //购物车总价 public Map<String, CartItem> getMap() { return map; } public void setMap(Map<String, CartItem> map) { this.map = map; } //购物车总价为map中存放的所有购物项总价之和 public double getPrice() { double totalprice=0; for(Map.Entry<String, CartItem> entry:map.entrySet()){ CartItem item=entry.getValue(); totalprice+=item.getPrice(); } this.price=totalprice; return price; } public void setPrice(double price) { this.price = price; } //每增加一本书,购物项中对应的书的数量加一 public void add(Book book){ CartItem item=map.get(book.getId()); if(item==null){ item =new CartItem (); item.setBook(book); item.setQuantity(1); map.put(book.getId(), item); } else{ item.setQuantity(item.getQuantity()+1); } } }
9 这时候我们再来新建BuyServlet
package cn.itcast.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.domain.Book; import cn.itcast.domain.Cart; import cn.itcast.service.BusinessService; public class BuyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据id得到相应的书 String id=request.getParameter("id"); BusinessService service=new BusinessService(); Book book=service.findBook(id); //得到购物车,将车加到购物车 //购物车存在Session中 Cart cart=(Cart) request.getSession().getAttribute("cart"); if(cart==null){ cart=new Cart(); request.getSession().setAttribute("cart", cart); } cart.add(book); //转发到listcart.jsp,此处有瑕疵,刷新一次购买一次 request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
此处应该用重定向(或其他方法?)。。避免刷新一次购买一次,日后改
10 jsp文件夹中新建
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%-- 导入jstl包,将c.tld标签库引入 --%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>购物车页面</title> <script type="text/javascript"> //友好提示删除 function deleteitem(id){ var b=window.confirm("确认删除吗?"); if(b){ window.location.href="${pageContext.request.contextPath }/servlet/DeleteItemServlet?id="+id; } } //友好提示清空 function clearcart(){ var b=window.confirm("确认清空吗?"); if(b){ window.location.href="${pageContext.request.contextPath }/servlet/ClearCartServlet"; } } //修改数量功能 function changeQuantity(input,id,oldvalue){ var quantity=input.value; //检查用户输入 if(isNaN(quantity)||quantity<0){ alert("请输入正整数"); input.value=oldvalue; return; } var b=window.confirm("确认修改数量吗?"); if(b){ window.location.href="${pageContext.request.contextPath }/servlet/ChangeQuantityServlet?id="+id + "&quantity="+quantity; } } </script> </head> <body style="text-align: center"> <h1>购物车列表</h1> <%-- jstl标签库if --%> <c:if test="${empty(cart.map)}">您没有购买任何物品</c:if> <c:if test="${!empty(cart.map)}"> <table width="80%" border="1" > <tr > <td>书名</td> <td>作者</td> <td>单价</td> <td>数量</td> <td>小计</td> <td>操作</td> </tr> <c:forEach var="entry" items="${ cart.map}"> <tr> <td>${entry.value.book.name} </td> <td>${entry.value.book.author}</td> <td>${entry.value.book.price}</td> <td><input type="text" name="quantity" value="${entry.value.quantity}" style="width:30px" onchange="changeQuantity(this,${entry.value.book.id},${entry.value.quantity})"></td> <td>${entry.value.price}</td> <td> <a href="javascript:void(0)" onclick="deleteitem(${entry.key})" >删除</a> </td> </tr> </c:forEach> <tr> <td colspan="3">总价</td> <td colspan="2">${cart.price}元</td> <td colspan="1"><a href="javascript:clearcart()">清空购物车</a></td> </tr> </table> </c:if> </body> </html>
11 再写相应的Servelet之前,要把业务层增加功能
package cn.itcast.service; import java.util.Map; import cn.itcast.dao.BookDao; import cn.itcast.domain.Book; import cn.itcast.domain.Cart; public class BusinessService { private BookDao dao=new BookDao(); public Map getAllBook(){ return dao.getAll(); } public Book findBook(String id){ return dao.find(id); } //删除购物车中购物项目 public void deleteCartItem(String id, Cart cart) { // TODO Auto-generated method stub cart.getMap().remove(id); } //清空购物车 public void cleatCart(Cart cart) { // TODO Auto-generated method stub cart.getMap().clear(); } //更改购物项数目 public void changeItemQuantity(String id, int quantity, Cart cart) { // TODO Auto-generated method stub cart.getMap().get(id).setQuantity(quantity); } }
12写相应的Servelet
ackage cn.itcast.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.domain.Book; import cn.itcast.domain.Cart; import cn.itcast.service.BusinessService; public class DeleteItemServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); BusinessService service=new BusinessService(); Cart cart=(Cart) request.getSession().getAttribute("cart"); service.deleteCartItem(id,cart); //删除后跳回购物车 request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package cn.itcast.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.domain.Cart; import cn.itcast.service.BusinessService; public class ClearCartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BusinessService service=new BusinessService(); Cart cart=(Cart) request.getSession().getAttribute("cart"); service.cleatCart(cart); request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package cn.itcast.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.domain.Cart; import cn.itcast.service.BusinessService; public class ChangeQuantityServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); int quantity=Integer.parseInt(request.getParameter("quantity")); BusinessService service=new BusinessService(); Cart cart=(Cart) request.getSession().getAttribute("cart"); service.changeItemQuantity(id,quantity,cart); request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
发布效果
点击几次购买
清空
修改数量
总结:没有解决刷新又购买一次和又删除一次的问题
界面功能太少
没有对用户输入进行用java代码校正。
标签:
原文地址:http://www.cnblogs.com/xurui1995/p/5372599.html