?自定义mvc思维导图?:
部分代码截图?及代码块
package com.yq.web;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 目标:自定义mvc的工作原理
* 1.什么是自定义mvc框架
* 关键: 自定义mvc 框架
*
* 2.它的运行原理
* 2.1代码演绎过程
* 2.2总结代码运行原理
*
* 思考:
* 什么是mvc
* Model模型、view 视图、controller控制层
* mvc的出现原因:各司其职
*
*
* 不足:
* model模型:
* Dao层:(增删改 查)
* 1.建立数据库连接
* 2.预定义对象PreparedStatement
* 3.执行查询
* 4.处理结果集
* 解决方案:通用分页解决了以上问题
*
* view视图:
* 1.重复的HTML分页条代码
* 2。重复的JS代码
* 解决方案:自定义page标签
*
* controller控制层:
* 1.重写了doGet,doPost并且doGet没有用
* 2.参数的封装代码冗余了
* req.getParammeter("xxx");
* 3.跳转页面代码重复
* 解决方案:自定义mvc
* 框架:反射+设计模式,案例:通用分页+自定义page标签+自定义mvc的组合就是框架
* @author 狗女士
*
*/
/*@WebServlet("/book.action")*/
public class Bookservlet extends HttpServlet{
@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 {
/**
* 增删改查缺陷
* 当需求发生改变,或者新增需求,需要改动下面代码 load回显
*
* 解决方案 :前台传递methodName到后台,实际就是想要调用当前类对象的methodName方法
* this.methodName
*/
String methodName = req.getParameter("methodName");
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.add()");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.delete()");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.edit()");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
System.out.println("bookDao.list()");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("bookDao.load()");
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("bookDao.ref()");
}
}
package com.yq.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.yq.web.BookAction;
/**
* 目标:
* 根据自定义mvc框架的原理图 完成 框架的研发
*
* 中央控制器
* 寻找子控制器
* @author 狗女士
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet{
//专门存放子控制器的容器
private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
//初始化子控制器容器(集合)经过初始化,actions容器内部就有了子控制器
@Override
public void init() throws ServletException {
actions.put("/book", 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 {
//完成寻找子控制器的过程
/*
* 思路:
* 1.从浏览器url中获取到"/book"字符串
* 2.从子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
ActionSupport action = actions.get(uri);
action.execute(req, resp);
}
}
?自定义mvc演绎过程:?
?代码运行结果截图:
?
以上为思维导图、部分代码截图以及代码运行结果?
|