【左京淳的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>

<<:  用arduino传值至php档再使用php档传数据至heidisql时无法传中文或英文

>>:  POCO_设定所有Table继承同一个class

AI ninja project [day 13] 回归

这应该也是学习深度学习时的基础课程, 不确定跟图像分类比,哪一个会先学到, 但是在接触深度学习框架时...

Day 28--Complex lifecycle situations

在范例Dessert App中利用timer来观察更复杂的生命周期 Timer 专案中的class ...

D5 - 你不知道 Combo : 前菜 Hoisting

前言 cookie(); function cookie(){ console.log('I lov...

win破解後可以接着使用商业的大量授权吗

各位大大们 你好 小弟想询问一下,因为使用大量授权好像前面都需要先安装过win授权,像是随机版之类的...

python30天-DAY29-Matplotlib(4)

最後一天了,我来补充一些 Matplotlib 的小细节。 tick_params() 用於将格子边...