iT邦幫忙

1

【左京淳的JAVA WEB學習筆記】第十章 重複代碼的提取

Dao物件的提取

Dao類的物件用來訪問DB,我們可以把其中常用的、重複性的代碼抽取出來。

新建抽象類BaseDao
注意點:connection是珍貴資源,一次request訪問應該重複利用同一個connection。
所以connection的開啟雖在Dao層,但不是每次訪問完DB就關閉。關閉的時機應交給service層決定。
開啟DB錯誤時需拋出異常,關閉時出錯則不必。

package com.aaa.dao;

public abstract class BaseDao {
  protected Connection connection;
  public Connection getConnection(){
    return connection;
  }
  public void setConnection(Connection connection){
    this.connection = connection;
  }
  public void openConnection() throws Exception{
    try {
      if (connection == null || connection.isClosed()) {
        DbInfo dbinfo = DbInfo.instance();
        Class.forName(dbInfo.getDriver());
        connection = DriverManager.getConnection(dbInfo.getUrl(), dbInfo.getUname(), dbInfo.getPwd());
      }
    } catch (ClassNotFoundException e) {
      Log.logger.error(e.getMessage(), e);
      throw e;
    } catch (SQLException e) {
      Log.logger.error(e.getMessage(), e);
      throw e;
    }   
  }
  public void CloseConnection(){
    if(this.connection != null) {
      try {
        this.connection.close();
      }catch(Exception e){
        Log.logger.error(e.getMessage(),e);
      }
    }
  }
  public void beginTransaction() throws Exception{
    this.openConnection();
    this.connection.setAutoCommit(false);
  }
  public void commit() throws Exception{
    if(this.connection != null) {
      this.connection.commit();
    }
  }
  public void rollback() throws Exception{
    if(this.connection != null) {
      this.connection.rollback();
    }
  }
}

當要確保一連串的動作都順利完成才儲存(commit())的時候,使用Transaction功能。
若途中出錯了,也能用rollback()回復到原本狀態。

JSP頁面的提取

網站的頁首頁尾經常會使用共同的樣式,
把使用者和管理員用的頁首分別提取成mhead.jsp和bhead.jsp
mhead.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName()  
  + ":" + request.getServerPort() + path + "/";
%>
<c:if test="${user != null}">
    welcome ${user.name } &nbsp;
    <a href="<%=basePath%>user/ShopCarSvl>">購物車</a>&nbsp;
    <a href="<%=basePath%>user/LogoutSvl>">退出</a>
    <c:if test="${user.role == 1 }">
        <a href="<%=basePath%>back/BookAddSvl">後台</a>
    </c:if>
</c:if>
<c:if test="${user == null}">
    <a href="<%=basePath%>LoginSvl>">登入</a>&nbsp;
    <a href="<%=basePath%>RegistSvl>">註冊</a>
</c:if>

bhead.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName()  
  + ":" + request.getServerPort() + path + "/";
%>
<tr>
    <td align=right>管理員:admin&nbsp;<a href="<%=basePath%>user/LogoutSvl>">退出</a></td>
</tr>
<tr>
    <td align=center>
        <a href="<%=basePath%>back/BookAddSvl>">新書上架</a>&nbsp;
        <a href="#">增加庫存</a>&nbsp;
        <a href="#">商品下架</a>&nbsp;
        <a href="#">用戶管理</a>&nbsp;
        <a href="#">修改售價</a>&nbsp;
        <a href="<%=basePath%>back/BuyRecordSvl>">用戶購買紀錄</a>
    </td>
</tr>

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言