(1)思维导图?
(2)需要用到的包,帮助类
?
?
?(3)中央控制器 ? ??
package com.zking.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.zking.web.BookAction;
import com.zking.web.GoodsAction;
/**
* 目标: 根据自定义mvvc框架的原理图 完成 框架研发
*
* @author My
* 中央控制器
* 寻找子控制器
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
// 存放子控制器的容器
// private Map<String, ActionSuppot> actions = new HashMap<String, ActionSuppot>();
private ConfigModel configModel = null;
// 初始化子控制器容器(集合),经过初始化,action容器内部就有了子控制器
// init,service,destroy
/*
* 需求:
* 在增加一个商品类的增删改查
* 步骤:
* 改动init代码
* 思考:
* 能不能不改动代码完成需求
* 参考DBAccess的数据源配置文件config.properties
* 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());
// action.put("/order", new BookAction());
// action.put("/OrderItem", 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://locahost:8080/t266_mvc/book.action?methodName
// 目标:BookAction.add()...
/*
* 思路: 1.从浏览器URL中获取到“/book”字符串 2.在子控制器容器中拿到BookAction 3.BookAction.add()
*
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
// ActionSuppot action = actions.get(uri);
ActionModel actionModel = configModel.pop(uri);
String type = actionModel.getType();
ActionSuppot action;
try {
action = (ActionSuppot)Class.forName(type).newInstance();
// ActionSuppot action = new BookAction();
if(action instanceof ModelDriver) {
ModelDriver m = (ModelDriver) action;
Object obj = m.getModerl();
// 有对象
// 接收所有的前端jsp传递到后台的参数
Map<String, String[]> parameterMap = req.getParameterMap();
// 给对象赋值
// PropertyUtils.getProperty(obj, "");
BeanUtils.populate(obj, parameterMap);
}
// execite->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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(3.1)解决初始化有缺陷需要用的代码
package com.zking.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.huangyanting.entity.Book;
import com.zking.framework.ActionSuppot;
import com.zking.framework.ModelDriver;
public class BookAction extends ActionSuppot implements ModelDriver<Book> {
private Book book = new Book();
// execute
private String delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.delete()...");
/*
* 需求:删除数据跳转到书籍展示页面 缺陷:
* 1.出现大量重复代码
* 2.不好管理(增删改重新跳转到查询界面)
* 解决方案:在一个地方统一管理重定向还是转发,并且是定位跳转到哪一个页面
* 预测结果
* BookAction调用bookDao.delete()...
* BookAction调用bookDao.list()...
*
*
*/
try {
// req.getRequestDispatcher("/booklist.jsp").forward(req, resp);
// resp.sendRedirect("/booklist.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 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.getParammter("")代码是重复的 需求:做一个book的新增功能
* 思考:将JSP参数塞进实体类的属性中,问题是每个实体类的属性都是不一样的,没办法做到代码完全重复 思路: 不知道是哪个实体类-->泛型
* 不知道这个实体类有那些属性,但又要给属性赋值-> 解决:泛型+反射 编码 模型驱动接口:ModelDriver
* 思路:
* 不知道是哪一个实体类->泛型T
* 不知道这个实体类有哪些属性,但是又要给这些属性赋值->反射动态设置属性值
* 解决:泛型+反射
* 编码:
* 模型驱动接口:ModelDriver
*
*
*/
// Book book = new Book();
// 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)...");
}
@Override
public Book getModerl() {
return book;
}
}
(3.3)界面代码
<%@ 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 color="red">
目前多数人增删改查的代码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 color="red">
目前多数人增删改查的代码V3.0
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联</a>
<hr color="red">
演示初始化的缺陷
<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>
解决参数实力类封装的问题
<form action="${pageContext.request.contextPath }/book.action?methodName=ref" method="post">
<input type="text" name="bid" value="22">
<input type="text" name="bname" value="yj">
<input type="text" name="price" value="212">
<input type="text" name="author" value="zy">
<input type="text" name="publish" value="hhhh">
<input type="text" name="remark" value="wwwww">
<input type="submit">
</form>
</body>
</html>
|