开发者学堂课程【Java Web项目实战2:图书商城:图书模板之加载图书】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/35/detail/776
图书模板之加载图书
内容简介:
一、查询详细信息(加载)
二、操作流程图
三、具体内容
一、查询详细信息(加载)
二、操作流程图
流程: list.jsp (点击某一本书) → BooServlet#load() → desc.jsp
三、具体内容
List
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 margin:10px;
24 border:solid2px gray;
25 width:160px;
26 height:180px;
27 text-align:center;
28 float:left;
29
30</style>
31 </head>
32
33 <body>
34
35 <c:forEach items="$ {bookList }" var"=book">
36 <div class="icon ">
37 " <a href="<c:uir1 value=' /Bookservlet ?Method=load&bid =$ {book.bid } '/> "> < img src="<c:url value= '/$book .image}'/>"broder"="0"/></a >
38 <br/ >
39 <a href="<c:url value= ' /Bookservlet ?Method=load&bid =$ {book.bid }' /> ">${book.bname} </a >
40 </div>
41< /c: forEach>
42
43
44 </body>
45
46 html>
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;
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 public String load (HttpServletRequest request,HttpServletResponse response)
16 throws ServletException, IOException {
17 /**
18 *1.得到参数bid
19 *2.查询得到book
20 *3.保存,转发到desc.Jsp
21 */
22 request.setAttribute ("book", bookservice.load (request.getParameter("bid");
23 return "f:/jsps/book/desc.jsp";
24 }
25
26 /**
24 *查询所有分类
25 *@param request
26 *@param response
27 *@return
28 *@throws ServletException
29 *@throws IOException
30 */
31 public String findAll(HttpServletRequest request,HttpServletResponse response)
32 throws ServletException,IOException {
33 request.setAttribute ("bookList", bookservice.findAll()};
34 return”f/jsps/book/list.jap";
35 }
27 }
36
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 public Book load (String bid) {
19 return bookDao. findByBid(bid) ;
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 }
45 }
46 /**
47 *加载方法
48 * @param bid
49 *@return
50 */
51 public Book findByBid(String bid) {
52 try {
53 String =sql. "select *from book where bid=?";
54 return qr·query(sql, new BeanHandler<Book> (Book.class),bid):
55 } catch (SQLException e)
56 throw new RuntimeException(e) ;
57 }
58 }
59 }
Desc
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
23 div{
24 margin:20px;
25 border:solid2px gray;
26 width:150px;
27 height:150px;
28 text-align: center;
29 }
30 li {
31 margin: 10px;
32 }
33 a{
34 background: ur1(<o:url value'/images/all.png'/) no-repeat;
35 display: inline-block;
36
37 background-position: 0-70px;
38 margin-left: 30px;
39 height: 36px;
40 width: 146px;
41 }
42 a:HOTER{
43 background: url(<c:url valuc= '/images/a11.png'/>) no-repeat;
44 display; inline-blook;
45
46 background-position: o -106px;
47 magin-30px
48 height: 36px;
49 width: 146px;
50 }
51 </style>
52 </head>
53
54 <body>
55 <div>
56 < img arc="c:url value=' /$(book. image 1'/>"border "0"/>
57 <div>
58 <ul>
59 <li>书名: $(book.bname )</li>
60 <li>作者: ${book.author}</li>
61 <li>单价: ${book.price}元</li>
62 </ul>
63 <form id= "forn"action="c:url value='/jsps/cart/list.jsp'/>" method "post"
64 <input type="text" size"3" name= "count" value="1"/>
65 </form>
66 < a href= "javasoript: document:getElementById('form').submit();"/a>
67 </body>
68 </html>
69