2020.12.2收获

用集合模拟数据库

    //既然是购物车案例,应该会有增删的操作,通过关键字查询书籍,所以使用LinkedHashMap集合
    private static Map<String, Book> map = new LinkedHashMap<>();
    
    static {
        map.put("1",new Book("1", "java", "zhongfucheng", "好书", 99));
        map.put("2",new Book("2", "javaweb", "ouzicheng", "不好的书", 44));
        map.put("3",new Book("3", "ajax", "xiaoming", "一般般", 66));
        map.put("4",new Book("4", "spring", "xiaohong", "还行", 77));
    }

    public static Map<String, Book> getAll() {


        return map;
    }

dao层:至少提供获取所有的书籍和根据关键字获取得到书籍

    //既然是购物车案例,应该会有增删的操作,通过关键字查询书籍,所以使用LinkedHashMap集合
    private static Map<String, Book> map = new LinkedHashMap<>();
    
    static {
        map.put("1",new Book("1", "java", "zhongfucheng", "好书", 99));
        map.put("2",new Book("2", "javaweb", "ouzicheng", "不好的书", 44));
        map.put("3",new Book("3", "ajax", "xiaoming", "一般般", 66));
        map.put("4",new Book("4", "spring", "xiaohong", "还行", 77));
    }

    public static Map<String, Book> getAll() {


        return map;
    }

service层是对dao层的一个封装

    public class BusinessService {
    
        BookDao bookDao = new BookDao();
    
        /*列出所有的书*/
        public Map getAll() {
    
            return bookDao.getAll();
        }

        /*根据书的id获取书*/
        public Book findBook(String id) {
            return bookDao.find(id);
        }
    
        //...待会还有其他的功能再从这里补充!
    }

开发提供JSP页面的Servlet

        //调用service层的方法,获取得到存放书籍的Map集合
        BusinessService businessService = new BusinessService();
        Map books = businessService.getAll();
        
        //存放在request域对象中,交给jsp页面显示
        request.setAttribute("books", books);
        
        //跳转到jsp页面中
        request.getRequestDispatcher("/WEB-INF/listBook.jsp").forward(request, response);

开发显示所有书籍的jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>显示所有的书籍</title>
</head>
<body>

<%--Servlet传递过来的是一个Map对象,要显示所有的书籍,就需要遍历Map集合(EL表达式和JSTL标签合用)--%>
<table border="1px">
    <tr>
        <td>书籍编号</td>
        <td>名称</td>
        <td>作者</td>
        <td>详细信息</td>
        <td>价格</td>
    </tr>

    <c:forEach items="${books}" var="me">
        <tr>
            <td>${me.key}</td>
            <td>${me.value.name}</td>
            <td>${me.value.author}</td>
            <td>${me.value.description}</td>
            <td>${me.value.price}</td>
        </tr>
    </c:forEach>


</table>

</body>
</html>

购物项的界面 

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>购物车显示页面</title>
    </head>
    <body>
    <h1>购物车显示页面</h1>
    
    <%--empty函数是判断集合中有没有元素--%>
    <%--如果购物车是没有任何购物项的--%>
    <c:if test="${empty(cart.bookMap)}">
        <h1>您还没有购买过任何的书籍呀!</h1>
    </c:if>
    
    <%--如果购物车有购物项,就应该把购物项的信息显示给用户--%>
    <c:if test="${!empty(cart.bookMap)}">
    
        <table border="1px">
            <tr>
                <td>书籍编号</td>
                <td>名称</td>
                <td>数量</td>
                <td>小计</td>
                <td>操作</td>
            </tr>
            <c:forEach items="${cart.bookMap}" var="me">
                <tr>
                    <td>${me.key}</td>
                    <td>${me.value.book.name}</td>
                    <td>${me.value.quantity}</td>
                    <td>${me.value.price}</td>
                    <td><a href="#">删除</a></td>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="2"><a href="#">清空购物车</a></td>
    
                <td colspan="2">合计:</td>
                <td>${cart.price}</td>
            </tr>
    
        </table>
    
    </c:if>
    
    
    </table>
    
    </body>
    </html>
原文地址:https://www.cnblogs.com/ltw222/p/14171084.html