1.思维导图:
2.解决子控制器初始化:
DispatchServlet:(中央控制器)
package com.zking.leijiajia.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.leijiajia.GoodsAction;
/**
* 目标:
* 根据自定义MVC框架的原理图完成 框架的研发
*
* @author zjjt
*
* 中央控制器
* 目标:寻找子控制器
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//专门存放子控制器的容器
//private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
private ConfigModel configModel=null;
//初始化子控制器容器(集合),经过初始化,actions容器内部就有了子控制器
//init,service,destroy
/**
* 需求:
* 在增加一个商品类的增删改查
* 步骤:
* 改动init中代码
* 思考:
* 能不能不改动代码完成这个需求
* 1.参考DBAcess的数据源配置文件config.prooerties
* 1.1 减少代码改动风险性
* 1.2 减少代码的编译次数(对于已经部署到服务器后)
* 解决方案:
* 改成子控制器可配置
* 解决步骤:
* 1.必须有配置文件config.xml
* 2.配置文件config.xml中要包含处理业务的子控制器
* 3.读取到配置文件config.xml中的对应的处理浏览器请求的子控制器
* 编码:
* ...
*/
@Override
public void init() throws ServletException {
//缺陷:需要改动原有代码,伴随着很大的风险
try {
configModel=ConfigModelFactory.build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// actions.put("/book", new BookAction());
// actions.put("/goods", new GoodsAction());
// actions.put("/OrderItme", new BookAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//完成寻找子控制器的过程
//浏览器:http://localhost:8080/t266_mvc/book.action?methodName=add
//目标:BookAction.add()...
/**
* 思路:
* 1.从浏览器URL中获取到“book”字符串
* 2.在子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
/**
* Debug的使用效果:
* 1.调试代码(找错)
* 2.观看\观察代码的一个流程走向,每一个参数的值
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
//action=BookAction
//ActionSupport action = actions.get(uri);
ActionModel actionModel = configModel.pop(uri);
String type = actionModel.getType();
ActionSupport action;
try {
action = (ActionSupport) Class.forName(type).newInstance();
//ActionSupport action=new BookAction();
action.execute(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
3.解决参数实体类封装的问题:
DispatchServlet:(中央控制器)
package com.leijiajia.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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 org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import com.leijiajia.web.GoodsAction;
/**
* 目标:
* 根据自定义MVC框架的原理图完成 框架的研发
*
* @author zjjt
*
* 中央控制器
* 目标:寻找子控制器
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//专门存放子控制器的容器
//private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
private ConfigModel configModel = null;
//初始化子控制器容器(集合),经过初始化,actions容器内部就有了子控制器
//init,service,destroy
/**
* 需求:
* 在增加一个商品类的增删改查
* 步骤:
* 改动init中代码
* 思考:
* 能不能不改动代码完成这个需求
* 1.参考DBAcess的数据源配置文件config.prooerties
* 1.1 减少代码改动风险性
* 1.2 减少代码的编译次数(对于已经部署到服务器后)
* 解决方案:
* 改成子控制器可配置
* 解决步骤:
* 1.必须有配置文件config.xml
* 2.配置文件config.xml中要包含处理业务的子控制器
* 3.读取到配置文件config.xml中的对应的处理浏览器请求的子控制器
* 编码:
* ...
*/
@Override
public void init() throws ServletException {
//缺陷:需要改动原有代码,伴随着很大的风险
try {
configModel=ConfigModelFactory.build();
} catch (Exception e) {
e.printStackTrace();
}
// actions.put("/book", new BookAction());
// actions.put("/goods", new GoodsAction());
// actions.put("/OrderItme", new BookAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//完成寻找子控制器的过程
//浏览器:http://localhost:8080/t266_mvc/book.action?methodName=add
//目标:BookAction.add()...
/**
* 思路:
* 1.从浏览器URL中获取到“book”字符串
* 2.在子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
/**
* Debug的使用效果:
* 1.调试代码(找错)
* 2.观看\观察代码的一个流程走向,每一个参数的值
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
//System.out.println(uri);
//action=BookAction
//ActionSupport action = actions.get(uri);
ActionModel actionModel = configModel.pop(uri);
String type = actionModel.getType();
ActionSupport action;
try {
//放射实例化
action = (ActionSupport) Class.forName(type).newInstance();
//ActionSupport action=new BookAction();
if(action instanceof ModelDriver) {
ModelDriver m=(ModelDriver) action;
Object obj = m.getModel();
//有对象
//接收所有的前端jsp传递到后台的参数
Map<String, String[]> parameterMap = req.getParameterMap();
//给对象赋值
//PropertyUtils.getProperty(obj, "");
BeanUtils.populate(obj, parameterMap);
}
action.execute(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
BookAction:(子控制器)
package com.leijiajia.web;
import java.lang.reflect.Field;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leijiajia.entity.Book;
import com.leijiajia.framework.ActionSupport;
import com.leijiajia.framework.ModelDriver;
/**
* 子控制器定义出来后,只需要关注业务代码
* @author zjjt
*
*/
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book=new Book();
private void delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.delete()...");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.add()...");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("BookAction调用bookDao.load()...");
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
//解决参数实体类封装的问题
/**
* 缺陷:对于前端传参,实体类属性值封装req.getParammeter("")代码 是重复的
* 需求:做一个book的新增功能
* 思考:将jsp参数塞进实体类的属性中,问题是每个实体类是不一样的,没办法做到代码的完全重复
* 思路:不知道是那一个实体类->泛型T
* 不知道这个实体类有哪些属性,但是又要给这个属性赋值->放射动态设置属性值
*
* 解决:泛型+放射
* 编码:
* 模型驱动接口:ModelDriver
*
*
*/
//Book book=new Book();
//Field[] declaredFields = book.getClass().getDeclaredFields();
// book.setBid(req.getParameter("bid"));
// book.setBname(req.getParameter("bname"));
// book.setPrice(req.getParameter("price"));
// book.setAuthor(req.getParameter("author"));
// book.setPublish(req.getParameter("publish"));
System.out.println(book);
System.out.println("BookAction调用bookDao.ref(book)...");
//req.getRequestDispatcher("index.jsp");
//http://47.100.191.44/login.jsp
}
@Override
public Book getModel() {
return book;
}
}
运行结果:
不需要改任何后端代码,只加字段,都是可以接受到结果的,使用更灵活;
4. 解决结果集代码重复问题:
DispatchServlet:(中央控制器)
package com.leijiajia.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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 org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import com.leijiajia.web.GoodsAction;
/**
* 目标:
* 根据自定义MVC框架的原理图完成 框架的研发
*
* @author zjjt
*
* 中央控制器
* 目标:寻找子控制器
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//专门存放子控制器的容器
//private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
private ConfigModel configModel = null;
//初始化子控制器容器(集合),经过初始化,actions容器内部就有了子控制器
//init,service,destroy
/**
* 需求:
* 在增加一个商品类的增删改查
* 步骤:
* 改动init中代码
* 思考:
* 能不能不改动代码完成这个需求
* 1.参考DBAcess的数据源配置文件config.prooerties
* 1.1 减少代码改动风险性
* 1.2 减少代码的编译次数(对于已经部署到服务器后)
* 解决方案:
* 改成子控制器可配置
* 解决步骤:
* 1.必须有配置文件config.xml
* 2.配置文件config.xml中要包含处理业务的子控制器
* 3.读取到配置文件config.xml中的对应的处理浏览器请求的子控制器
* 编码:
* ...
*/
@Override
public void init() throws ServletException {
//缺陷:需要改动原有代码,伴随着很大的风险
try {
configModel=ConfigModelFactory.build();
} catch (Exception e) {
e.printStackTrace();
}
// actions.put("/book", new BookAction());
// actions.put("/goods", new GoodsAction());
// actions.put("/OrderItme", new BookAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//完成寻找子控制器的过程
//浏览器:http://localhost:8080/t266_mvc/book.action?methodName=add
//目标:BookAction.add()...
/**
* 思路:
* 1.从浏览器URL中获取到“book”字符串
* 2.在子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
/**
* Debug的使用效果:
* 1.调试代码(找错)
* 2.观看\观察代码的一个流程走向,每一个参数的值
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
//System.out.println(uri);
//action=BookAction
//ActionSupport action = actions.get(uri);
ActionModel actionModel = configModel.pop(uri);
String type = actionModel.getType();
ActionSupport action;
try {
//放射实例化
action = (ActionSupport) Class.forName(type).newInstance();
//ActionSupport action=new BookAction();
if(action instanceof ModelDriver) {
ModelDriver m=(ModelDriver) action;
Object obj = m.getModel();
//有对象
//接收所有的前端jsp传递到后台的参数
Map<String, String[]> parameterMap = req.getParameterMap();
//给对象赋值
//PropertyUtils.getProperty(obj, "");
BeanUtils.populate(obj, parameterMap);
}
//execute->delete
String res = action.execute(req, resp);
/**
* 思路:
* 1.方法执行宪法完毕必须有一个返回值
* 2.通过返回值决定是否重定向还是转发
* 3.通过返回值决定跳转那一页
*/
ForwardModel forwardModel = actionModel.pop(res);
if(forwardModel.isRedirect()) {
//重定向
resp.sendRedirect(req.getContextPath()+forwardModel.getPath());
}
else {
//转发
req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
BookAction: (子控制器)
package com.leijiajia.web;
import java.io.IOException;
import java.lang.reflect.Field;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leijiajia.entity.Book;
import com.leijiajia.framework.ActionSupport;
import com.leijiajia.framework.ModelDriver;
/**
* 子控制器定义出来后,只需要关注业务代码
* @author zjjt
*
*/
public class BookAction extends ActionSupport implements ModelDriver<Book>{
private Book book=new Book();
private String delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.delete()...");
/**
* 需求:删除数据后跳转到书籍展示页面
* 缺陷:1.出现大量重复代码 2.不好管理(增删改重写跳到查询页面)
* 解决方案:在一个地方统一管理重定向还是转发,并且是定位跳转到那一个页面
* 预测结果:
* BookAction调用bookDao.delete()...
*
*/
// try {
// //转发
// req.getRequestDispatcher("/bookList.jsp").forward(req, resp);
// //resp.sendRedirect("/boolList.jsp");//重定向
// this.list(req, resp);
// } catch (Exception e) {
// e.printStackTrace();
// }
return "toList";
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.add()...");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("BookAction调用bookDao.load()...");
}
private String list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.list()...");
// try {
// req.getRequestDispatcher("/bookList.jsp").forward(req, resp);
// } catch (Exception e) {
// e.printStackTrace();
// }
return "list";
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
//解决参数实体类封装的问题
/**
* 缺陷:对于前端传参,实体类属性值封装req.getParammeter("")代码 是重复的
* 需求:做一个book的新增功能
* 思考:将jsp参数塞进实体类的属性中,问题是每个实体类是不一样的,没办法做到代码的完全重复
* 思路:不知道是那一个实体类->泛型T
* 不知道这个实体类有哪些属性,但是又要给这个属性赋值->放射动态设置属性值
*
* 解决:泛型+放射
* 编码:
* 模型驱动接口:ModelDriver
*
*
*/
//Book book=new Book();
//Field[] declaredFields = book.getClass().getDeclaredFields();
// book.setBid(req.getParameter("bid"));
// book.setBname(req.getParameter("bname"));
// book.setPrice(req.getParameter("price"));
// book.setAuthor(req.getParameter("author"));
// book.setPublish(req.getParameter("publish"));
System.out.println(book);
System.out.println("BookAction调用bookDao.ref(book)...");
//req.getRequestDispatcher("index.jsp");
//http://47.100.191.44/login.jsp
}
@Override
public Book getModel() {
return book;
}
}
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/book" type="com.leijiajia.web.BookAction">
<forward name="list" path="/bookList.jsp" redirect="false" />
<!-- 增删改操作之后要再一次查询数据库的新的数据做展示 -->
<forward name="toList" path="/book.action?methodName=list" redirect="true" />
</action>
</config>
5.视图层:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
目前多数人的增删改查的代码
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/delete">删除</a>
<a href="${pageContext.request.contextPath }/book/edit">修改</a>
<a href="${pageContext.request.contextPath }/book/list">出现</a>
<hr>
增删改查的代码V2.0
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=delete">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">出现</a>
<hr>
增删改查的代码V3.0
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联</a>
<hr>
演示原有的初始化子控制器的缺陷
<a href="${pageContext.request.contextPath }/goods.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=delete">删除</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=list">出现</a>
<hr>
解决参数实体类封装的问题
<form action="${pageContext.request.contextPath }/book.action?methodName=ref" method="post">
<input type="text" name="bid" value="22">
<input type="text" name="bname" value="lyf">
<input type="text" name="price" value="34">
<input type="text" name="author" value="ljj">
<input type="text" name="publish" value="scs">
<input type="text" name="remark" value="zking">
<input type="submit">
</form>
</body>
</html>