开发者学堂课程【Java Web项目实战2:图书商城:图书模板之按分类查询图书】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/35/detail/775
图书模板之按分类查询图书
内容简介:
一、操作流程图
二、具体内容
一、操作流程图
流程:left.jsp → BookServlet#findByCategory()→list.jsp
二、具体内容
Left
1 <&@ page language="java" import"java. util.*" pageEncoding= "UTF-8"&>
2 <&@ taglib prefix="e" uri="http://java. sun. com/jsp/jstl/core"&>
3
4 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 <html>
6 <head>
7 <title>图书列表</title>
8
9 <meta http-equiv ="pragea" content= "no-cache ">
10 <meta http-equiv= "cache-control" contentm ="no-cache ">
11 <meta http-equiv "expires" content="0">
12 <meta http-equiv "keywords" content= "keyword1,keyword2 keyword3"> 13 <meta http-equiva "description" content= "This is my page ">
14 <meta http-equiva ="content-type" content= "text/html;charset-utf-8"> 15 <!--
16 <link.rel="stylesheet" type="text/cas"href="styles.oss">
17 -->
18<style type="text/css ",
19 body {
20 font-sizes: 10pt;
21 }
22 .icon
23 }
24 div {
25 background: #87CEFA;
26 margin:3px;
27 paddingt:3px;
28 }
29 a{
30 text-decoration: none ;
31}
32 </style>
33 </head>
34
35 <body>
36<div>
37 <a href=" '/BookServlet?method=findAll'/>">/全部分类>"</ a>
38 </div>
39
40
41 <c:forEach items="${categoryList }" var="oatagory">"
42 </div>
43 <a href="<c:url value=BookServelt?mathor=findByCategory&cid={category.cid}”/>”>$(category.cname }</ a>
44 </div>
45 </c:forEach>
46 </body>
47 </html>
48
Bookservelt
1 package cn.itcaat.bookatore.book.web.servlet;
2
3 import java.io.IOEception;
4
5 import javax .servlet.serwletException;
6 import javax.servlet.http.HttpservletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import cn.itcast.bookstore.book.web.servlet;
10import cn.itcast.servlet.Baseservlet;
11
12 public class Bookservlet extends Baseservlet {
13 private Bookservice bookservice = new Bookservice () ;
14
15 /**
16 *查询所有分类
17 *@param request
18 *@param response
19 *@return
20 *@throws ServletException
21 *@throws IOException
22 */
23 public String findAll(HttpServletRequest request,HttpServletResponse response)
24 throws ServletException,IOException {
25 request.setAttribute ("bookList", bookservice.findAll()}; 26 return”f/jsps/book/list.jap"; 20 }
27 }
28
Bookservice
1 package cn.itcast .bookstore.book.service;
2
3 import java.util.List;
4
5 import cn.itcast .bookstore.book .dao. BookDao;
6 import cn.itcast.bookstore.book.domain. Book;
7
8 public class Bookservice {
9 private BookDao bookDao = new BookDao();
10
11 public List<Book>findAll() {
12 return bookDao.findAll();
13 }
14
15 public List<Book>findByCategory(cid) {
16 return bookDao.findByCategory(cid);
17 }
18 }
19
Bookdao
1 packvage cn.itcast.bookstore.book.dao;
2
3 import java.sql.SQLException;
4 import java.util.List;
5
6 import org.apache . commons . dbutils.Que ryRunner;
7 import org.apache.commons . dbutils.handlers.BeanListHandler;
8
9 import
cn.itcast.bookstore.book.domain.Book;
10import cn.itcast jdbc.TxQueryRunner;
11
12 public class BookDao{
13 private QueryRunner qr =new TxQueryRunner():
14 /**
15 *按分类查询图书
16 * @return
17 */
24 public List<Book> findAll() {
25 try {
26 String =sql . "select *from book";
27 return qr·query(sql, new BeanListHandler<Book> (Book.class)):
28 } catch (SQLException e)
29 throw new RuntimeException(e) ;
30 }
31 }
32 }
33 /**
34 *按分类查询图书
35 * @param cid
36 *@return
37 */
38 public List<Book> findByCategory(string cid) {
39 try {
40 String =sql . "select *from book";
41 return qr·query(sql, new BeanListHandler<Book> (Book.class),cid):
42 } catch (SQLException e)
43 throw new RuntimeException(e) ;
44 }