手把手做一個JSP入門程式(六):擷取單一商品詳情(JSP)

來源:互聯網
上載者:User
擷取單一商品詳情 拓展BookDAO類,實現根據isbn值擷取書籍資訊方法

  其實現和擷取所有的商品是非常相似的。這裡還用到了一個PreparedStatment的一個可以傳參的屬性。

public Book getBookByISBN(String isbn){    Connection conn = null;    PreparedStatement preStmt = null;    ResultSet bookSet = null;    try{//--------------- 1. 串連資料庫------------------        conn = DBHelper.getConnection();//--------------- 2. 執行SQL語句,接收執行結果集ResultSet ------------------        String sql = "select * from book where isbn=?;";        preStmt = conn.prepareStatement(sql);        preStmt.setString(1, isbn);        bookSet = preStmt.executeQuery();//--------------- 3. 處理執行結果集ResultSet ------------------        if(bookSet.next()){            Book book = new Book();            book.setAuthor(bookSet.getString("author"));            book.setImg(bookSet.getString("img"));            book.setIntro(bookSet.getString("intro"));            book.setIsbn(bookSet.getString("isbn"));            book.setName(bookSet.getString("name"));            book.setPrice(Float.parseFloat(bookSet.getString("price")));            book.setPrice_original(Float.parseFloat(bookSet.getString("price_original")));            book.setPublish_company(bookSet.getString("publish_company"));//              時間格式化            SimpleDateFormat dateformat  = new SimpleDateFormat( "yyyy年MM月dd日 ");            ParsePosition pos = new ParsePosition(0);            book.setPublish_time(dateformat.parse(bookSet.getString("name"),pos));            return book;        }else{            return null;        }    }catch(Exception ex){        ex.printStackTrace();        return null;    }finally{//--------------- 4. 必要的關閉ResultSet、Statement ------------------//          為了確保資源不被佔用,需要在fianlly釋放資源,注意這裡不可以關閉Connection,否則會報錯//          釋放 ResultSet        if(bookSet != null){            try{                bookSet.close();                bookSet = null;            }catch(Exception ex){                ex.printStackTrace();            }        }//                      釋放語句對象:PreparedStatement        if(preStmt != null){            try{                preStmt.close();                preStmt = null;            }catch(Exception ex){                ex.printStackTrace();            }        }    }}
顯示單一商品詳細頁

  我們不難發現,index.jsp頁和single頁的頭部和尾部是一樣的,因此我們可以用JSP的3個編譯指令(page、include和taglib)中的include指令來最佳化代碼。
使用include指令,可以將一個外部檔案嵌入到當前JSP檔案中,同時解析這個頁面中的JSP語句。文法如下:

<%@ include file="檔案相對路徑" %>

所以,下面我們建立兩個檔案:header.jsp和footer.jsp分別存放網頁的頭部和尾部代碼。然後single.jsp代碼如下。同時用同樣的方法修改index.jsp檔案。
header.jsp

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html><html lang="en"><head>    <base href="<%=basePath%>">    <meta charset="UTF-8">    <title>DIAGRAM</title>    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />  <link href="./bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>  <link href="./css/style.css" rel="stylesheet"/>  <link href="./css/single.css" rel="stylesheet"/></head><body>    <nav class="navbar navbar-default" role="navigation">        <div class="logo-big"><span>D&nbsp;I&nbsp;A&nbsp;G&nbsp;R&nbsp;A&nbsp;M</span></div>        <div class="container-fluid">         <div class="row nav-menuare">            <div class="navbar-header">                <a class="navbar-brand logo-small" href="#">DIAGRAM</a>                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">                    <span class="sr-only">Toggle navigation</span>                    <span class="icon-bar"></span>                    <span class="icon-bar"></span>                    <span class="icon-bar"></span>                </button>            </div>            <div class="row navbar-row">                <div class="nav-divider col-md-3 col-sm-3"><span class="divider"></span></div>                <div id="navbar"  class="col-md-6 col-sm-6 collapse">                    <ul>                        <li class="li-col"><a href="#">SHOPINDEX</a></li>                        <li class="li-col"><a href="#">NEWBOOK</a></li>                        <li class="li-col li-logo"><a href="./index.jsp"><img class="logo-img" src="./img/robot.png"/></a></li>                        <li class="li-col"><a href="#">CART(2)</a></li>                        <li class="li-col"><a href="#">CUSTOMER</a></li>                    </ul>                </div>                <div class="nav-divider col-md-3 col-sm-3"><span class="divider"></span></div>            </div>            </div>        </div>    </nav>

footer.jsp

    <div class="footer">        <div class="container-fluid">            <div class="row">                <div class="col-md-3">                </div>                <div class="blog-coyp col-md-6">                    <div class="blog text-left"><a href="https://github.com/donespeak" target="_blank">GITHUB</a> | <a href="http://blog.csdn.net/donespeak" target="_blank">CSDN</a> | EMAIL:yangguanr@gmail.com </div><div class="copy text-right">&copy;DoneSpeak.inc</div>                </div>            </div>          </div>    </div>    <script src="./js/jquery-2.1.4.min.js"></script>  <script src="./bootstrap-3.3.0/js/bootstrap.min.js"></script>  <script src="./js/script.js"></script></body></html>

single.jsp

<%@ page language="java" import="java.util.*"  contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ page import="entity.Book" %><%@ page import="dao.BookDAO" %><%@ include file="header.jsp" %>    <div class="main newbook-all">        <div class="container-fluid">            <div class="row">                <%                    BookDAO bookdao = new BookDAO();                    Book book = bookdao.getBookByISBN(request.getParameter("isbn"));                    if(book != null){                %>                <div class="col-md-3 col-md-offset-1 col-sm-4 col-sm-offset-1">                    <div class="book-img">                        <img src="img/<%= book.getImg() %>" />                    </div>                </div>                <div class="sell col-md-5 col-sm-6">                    <div class="booktext">                        <h3>《<%= book.getName() %>》</h3>                        <div class="divider divider-bookinfo"></div>                        <p><span class="t1">作者:<%= book.getAuthor() %></span><span class="t1">著出版社:<%= book.getPublish_company() %></span><span class="t1">出版時間:<%= book.getPublish_time() %></span><p>                        <p class="newbook-intro">《<%= book.getIntro() %></p>                    </div>                    <div class="snapped-up-btn-area">                        <span class="cost">¥<%= book.getPrice() %></span> / <span class="cost-original">¥<%= book.getPrice_original() %></span><button class="snapup">Add To Cart</button>                    </div>                </div>                <%                    }                %>                <div class="col-md-2 record">                    <div class="rec-head">瀏覽記錄</div>                    <div class="rec-card">                        <a href="single.html"><img class="img-rec pull-left" src="img/圖解網站分析.jpg"/></a>                        <div class="pull-left txt-rec">圖解網站分析<br />¥34.8</div>                    </div>                    <div class="rec-card">                        <a href="single.html"><img class="img-rec pull-left" src="img/圖解網站分析.jpg"/></a>                        <div class="pull-left txt-rec">圖解網站分析<br />¥34.8</div>                    </div>                    <div class="rec-card">                        <a href="single.html"><img class="img-rec pull-left" src="img/圖解網站分析.jpg"/></a>                        <div class="pull-left txt-rec">圖解網站分析<br />¥34.8</div>                    </div>                    <div class="rec-card">                        <a href="single.html"><img class="img-rec pull-left" src="img/圖解網站分析.jpg"/></a>                        <div class="pull-left txt-rec">圖解網站分析<br />¥34.8</div>                    </div>                </div>            </div>        </div>    </div><%@ include file="footer.jsp" %>

include指令
  以上使用include的方式屬於靜態include指令,該指令會之間將指定檔案中的內容嵌入到指令所在位置中,在進行編譯。如果被內嵌檔案需要經常改變,建議使用操作指令。
是動作指令,其與上面的<%@include %>編譯指令有很大不同,它不是將檔案的內容直接嵌入,而是獨立運行完之後,將產生的輸出結果(html)嵌入,所以裡面的java代碼是不會被匯入的。事實上,用指定的檔案會獨立的產生一個Servlet,而<%@ include%>不會。基本文法格式如下:

<jsp:include page="{relativeURL | <%=expression%>}" flush="true"/><%-- 或者 --%><jsp:include page="{relativeURL | <%=expressi
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.