MVC案例之通过配置切换底层存储源(面向接口)
1.深入理解面向接口编程:
在类中调用接口的方法,而不必关心具体的实现。这将有利于代码的解耦。使程序有更好的可移植性
和可扩展性

动态修改 Customer 的存储方式:通过修改类路径下的 switch.properties 文件的方式来实现
switch.properties

①. CustomerServlet 中不能在通过 private CustomerDAO customerDAO =
new CustomerDAOXMLImpl(); 的方式来写死实现类
②. 需要通过一个类的一个方法来获取具体的实现类的对象
2.实现步骤
①
当前 WEB 应用才启动的时候,InitServlet 被创建,并由 Servlet 容器调用其 init() 方法:
读取类路径下的 switch.properties 文件
获取 switch.properties 的 type 属性值
赋给了 CustomerDAOFactory 的 type 属性值
InitServlet
package com.aff.mvcapp.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import com.aff.mvcapp.dao.factory.CustomerDAOFactory; @WebServlet("/initServlet")
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
public void init() throws ServletException {
CustomerDAOFactory.getInstance().setType("jdbc");
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/classes/switch.properties");
Properties properties = new Properties();
try {
properties.load(is);
String type = properties.getProperty("type");
CustomerDAOFactory.getInstance().setType(type);
} catch (IOException e) {
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>com.aff.mvcapp.servlet.CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.aff.mvcapp.servlet.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
②
创建 CustomerServlet 时,为 customerDAO 属性赋值是通过 CustomerDAOFactory 的
getCustomerDAO() 方法完成的 。
此时的 type 已经在 InitServlet 中被赋值了。
CustomerServlet
package com.aff.mvcapp.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aff.mvcapp.dao.CriteriaCustomer;
import com.aff.mvcapp.dao.CustomerDAO;
import com.aff.mvcapp.dao.factory.CustomerDAOFactory;
import com.aff.mvcapp.domian.Customer; @WebServlet("/customerServlet")
public class CustomerServlet extends HttpServlet {
private CustomerDAO customerDAO = CustomerDAOFactory.getInstance().getCustomerDAO();
//private CustomerDAO customerDAO = new CustomerDAOImpl(); private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 获取ServletPath: /edit.do 或 addCustomer.do
String servletPath = request.getServletPath();
// 2.去除 / 和 .do 得到类似于 edit 或 addCustomer 这样的字符串
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length() - 3); try {
// 3.利用反射获取 methodName 对应的方法
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);
// 4.利用反射调用对应的方法
method.invoke(this, request, response);
} catch (Exception e) {
// e.printStackTrace();
// 可以有一些响应
response.sendRedirect("error.jsp");
}
} private void addCustomer(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取表单参数:name address phone
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone"); // 2.检验 name 是否被占用
// 2.1调用CustomerDAO的getCountWithName(String name) 获取 name 在数据库中是否存在
long count = customerDAO.getCountWithName(name);
// 2.2若返回值大于0,则响应 newcustomer.jsp 页面, 通过转发的方式响应newcustomer.jsp
if (count > 0) {
// 2.2.1要求页面显示一个错误消息: 用户名 name已经被占用,请重新选择,
// 在request中放入一个属性message :用户名 name 已经被占用,请重新选择!
// 在页面通过request.getAttribute("message")的方式显示
request.setAttribute("message", "用户名 " + name + "已经被占用,请重新选择"); // 2.2.2 newcustomer.jsp 的表单可以回显
// 通过value="<%=request.getParameter("name") == null ? "" :
// request.getParameter("name") %>"进行回显 // 2.2.3结束方法:return
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
return;
}
// 3.若验证通过,把表单参数封装为一个Customer 对象 customer
Customer customer = new Customer(name, address, phone); // 4.调用CustomerDAO的 save(Customer customer) 执行保存操作
customerDAO.save(customer); // 5.重定向的 success.jsp 页面:使用重定向可以避免表单的重复提交问题
response.sendRedirect("success.jsp"); } private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forwardPath = "/error.jsp";// 初始值
// 1.获取请求参数 id
String idStr = request.getParameter("id");
// 2.调用CustomerDAO 的 customerDAO.get(id) 获取 id 对应的Customer 对象customer
try {
Customer customer = customerDAO.get(Integer.parseInt(idStr));
if (customer != null) {// 当customer为null是转到 error.jsp
forwardPath = "/updatecustomer.jsp";
// 3.将customer放入request 中
request.setAttribute("customer", customer);
}
} catch (Exception e) {
}
// 4.响应updatecustomer.jsp 页面: 转发
request.getRequestDispatcher(forwardPath).forward(request, response); } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取表单参数:id,name,addres, phone, oldName
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
String oldName = request.getParameter("oldName"); // 2.检查name是否被占用
// 2.比较name和oldName 是否相同,若相同说明 name可用
// 2.1若不相同, 则调用CustomerDAO 的 getCountWithName(String name) 获取 name在数据库中是否存在
if (!oldName.equalsIgnoreCase(name)) {
long count = customerDAO.getCountWithName(name);
if (count > 0) {
// 2.2若返回值大于0,则响应 updatecustomer.jsp 页面,
// 通过转发的方式响应updatecustomer.jsp
// 2.2.1要求页面显示一个错误消息: 用户名 name已经被占用,请重新选择,
// 在request中放入一个属性message :用户名 name 已经被占用,请重新选择!
// 在页面通过request.getAttribute("message")的方式显示
request.setAttribute("message", "用户名" + name + "已经被占用,请重新选择!"); // 2.2.2 updatecustomer.jsp 的表单可以回显
// address, phone 显示提交表单的新的值,而 name 显示oldName, 而不是新提交的 name // 2.2.3结束方法:return
request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
return;
}
} //3.若验证通过,则把表单参数封装为一个Customer 对象 customer
Customer customer = new Customer(name, address, phone);
customer.setId(Integer.parseInt(id));//表单过来的 安全的 //4.调用CustomerDAO 的 update(Customer customer) 执行更新操作
customerDAO.update(customer);
//5.重定向到 query.do
response.sendRedirect("query.do"); } private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone"); CriteriaCustomer cc = new CriteriaCustomer(name, address, phone); // 1.调用 CustomerDAO 的 getForListWithCriteriaCustomer() 得到 Customer 的集合
List<Customer> customers = customerDAO.getForListWithCriteriaCustomer(cc); // 2.把 Customer 的集合放入 request 中
request.setAttribute("customers", customers); // 3.转发页面到 index.jsp 中( 不能使用重定向)
request.getRequestDispatcher("/index.jsp").forward(request, response); } private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idstr = request.getParameter("id");
int id = 0;
// try-catch的作用 , 防止恶意的输入, idStr 不能转为int类型,若出异常 id直接为0
try {
id = Integer.parseInt(idstr);
customerDAO.delete(id);
} catch (Exception e) {
}
response.sendRedirect("query.do");
}
}
③
CustomerDAOFactory 的 getCustomerDAO() 方法为 customerDAO 属性赋值
CustomerDAOFactory
package com.aff.mvcapp.dao.factory;
import java.util.HashMap;
import java.util.Map;
import com.aff.mvcapp.dao.CustomerDAO;
import com.aff.mvcapp.dao.impl.CustomerDAOImpl;
import com.aff.mvcapp.dao.impl.CustomerDAOXMLImpl; public class CustomerDAOFactory { private Map<String, CustomerDAO> daos = new HashMap<>();
private static CustomerDAOFactory instance = new CustomerDAOFactory();
public static CustomerDAOFactory getInstance() {
return instance;
}
public String type = null;
public void setType(String type) {
this.type = type;
}
private CustomerDAOFactory() {
daos.put("jdbc", new CustomerDAOImpl());
daos.put("xml", new CustomerDAOXMLImpl());
} public CustomerDAO getCustomerDAO() {
return daos.get(type);
} }
MVC案例之通过配置切换底层存储源(面向接口)的更多相关文章
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1)框架搭建 (2):数据 ...
- JavaWeb(五):MVC案例
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它把应用程序分成三个核心模块:模型.视图.控制器,它们各自处理自己的任务.模型是应用程序的主体部分 ...
- 【JavaWeb】MVC案例之新闻列表
MVC案例之新闻列表 作者:白宁超 2016年6月6日15:26:30 摘要:本文主要针对javaweb基本开发之MVC案例的简单操作,里面涉及mysql数据库及表的创建,以及jsp页面和servle ...
- SpringBoot系列之三_一个完整的MVC案例
这一节让我们来做一个完整的案例. 我们将使用MyBatis作为ORM框架,并以非常简单的方式来使用MyBatis,完成一个完整的MVC案例. 此案例承接上一节,请先搭建好上一节案例. 一.数据库准备 ...
- Apache Druid 底层存储设计(列存储与全文检索)
导读:首先你将通过这篇文章了解到 Apache Druid 底层的数据存储方式.其次将知道为什么 Apache Druid 兼具数据仓库,全文检索和时间序列的特点.最后将学习到一种优雅的底层数据文件结 ...
- Kooboo CMS技术文档之三:切换数据存储方式
切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...
- Percona 开始尝试基于Ceph做上层感知的分布式 MySQL 集群,使用 Ceph 提供的快照,备份和 HA 功能来解决分布式数据库的底层存储问题
本文由 Ceph 中国社区 -QiYu 翻译 英文出处:Using Ceph with MySQL 欢迎加入CCTG Over the last year, the Ceph world drew m ...
- ASP.NET MVC案例教程(二)
ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...
- ASP.NET MVC案例教程(三)
ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...
随机推荐
- 解决虚拟机中linux系统无法使用本机无线wifi联网的问题
VMware Workstation 在嵌入式开发中经常会遇到,但是显示大多数人使用开发环境是Win10 + 无线网卡,针对这种情况,需要配置虚拟机的上网环境使用的是NAT模式,将配置过程进行描述: ...
- python-CSV格式清洗与转换、CSV格式列变换、CSV格式数据清洗【数据读入的三种方法】【strip、replace、split、join函数的使用】
1)CSV格式清洗与转换 描述 附件是一个CSV格式文件,提取数据进行如下格式转换: (1)按行进行倒序排列: (2)每行数据倒序排 ...
- ipc$链接批量爆破
写了一个bat bat code: @echo off cls echo Useage: % ip.txt pass.txt ) do ( FOR /F ) do ( echo net use \\% ...
- 《C程序设计语言》 练习2-1
问题描述 编写一个程序以确定分别由signed及unsigned限定的char.short.int及long类型变量的取值范围.采用打印标准头文件中的相应值以及直接计算两种方式实现 Write a p ...
- Python3中正则的贪婪匹配模式
什么是贪婪模式 正则在进行匹配时,从开始位置查找最远的结束位置,这种模式称之为贪婪模式. 在进行HTML标签类似内容获取时,贪婪模式会导致整个内容的返回,需要使用非贪婪模式. 固定的书写规则 : .* ...
- 我去,你竟然还不会用 synchronized
二哥,离你上一篇我去已经过去两周时间了,这个系列还不打算更新吗?着急着看呢. 以上是读者 Jason 发来的一条信息,不看不知道,一看真的是吓一跳,上次我去是 4 月 3 号更新的,离现在一个多月了, ...
- 第九章(二)DAG上的动态规划
DAG上的动态规划: 有向无环图上的动态规划是学习DP的基础,很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 1.没有明确固定起点重点的DAG模型: 嵌套矩形问题:有n个矩形,每个矩形可 ...
- [hdu5164]ac自动机
中文题目:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=563&pid=1003 首先贴一下bc的题解 ...
- Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 下
一,引言 上一节讲到如何在我们的项目中集成Azure AD 保护我们的API资源,以及在项目中集成Swagger,并且如何把Swagger作为一个客户端进行认证和授权去访问我们的WebApi资源的?本 ...
- Amaze UI学习笔记——JS学习历程一
1.自定义事件 (1)一些组件提供了自定义事件,命名方式为{事件名称}.{组件名称}.amui,用户可以查看组件文档了解.使用这些事件,如: $('#myAlert').on('close.alert ...