一:思维导图
二: 代码
Action.java
package com.lhw.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* 1.处理前台浏览器请求
* 2.运行jsp传递到后台的方法字符串所带代表的方法
*
*
* @author Administrator
*
*/
public interface Action {
public void execute(HttpServletRequest req, HttpServletResponse resp);
/*private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.add()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.delete()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.edit()...");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
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()...");
}*/
}
ActionSupport.java
package com.lhw.framework;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) {
String name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DispatchServlet.java
package com.lhw.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.lhw.web.BookAction;
/**
*
* 目标:
* 根据自定义mvc框架的原理图 完成框架的研发
*
* @author Administrator 中央控制器
* 寻找子控制器
*
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//存放子控制器的容器
private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
//初始化子控制器容器(集合),经过初始化,action容器内部就有了子控制器
//init(初始化方法),service(服务),destroy(销毁)
@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 {
//完成子控制器对的过程
//浏览器:http://localhost:8080/J2ee12/book.action?name=add
//目标:BookAction.add()...
/**
* 思路:
* 1.从浏览器URL中获取到"/book"字符串
* 2.在子控制器中拿到BookAction
* 3.BookAction.add()
*/
String url = req.getRequestURI();
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(url);
action.execute(req, resp);
}
}
AddBookServlet.java
package com.lhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/add")
public class AddBookServlet 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 {
System.out.println("bookDao.add()...");
}
}
BookAction.java
package com.lhw.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lhw.framework.ActionSupport;
public class BookAction extends ActionSupport {
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.add()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.delete()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.edit()...");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.list()...");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
//修改页面数据回显
System.out.println("BookAction调用bookDao.load()...");
}
private void ref(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("BookAction调用bookDao.ref()...");
}
}
BookServlet.java
package com.lhw.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.它的运行原理
*
* 思考:
* 什么是mvc?
* Model模型、view视图、controller控制层
* mvc的出现原因:各司其职
* 不足:
* Model模型:
* Dao层:(增删改 查)
* 1.建立数据库连接
* 2.预定义对象Preparestatement
* 3.执行查询
* 4.处理结果集
* 通用分页解决上面问题
* view视图:
* 1.重复的HTML分页条代码
* 2.重复jsp代码
* 自定义page标签
* controller控制层:
* 1.重写doGet、doPost,并且doGet没有用
* 2.参数的封装代码冗余了
* req.getParameter("xxx");
* 3.对于跳转页面的代码是重复的
* req.getRequestDispatcher("index.jsp").forward(req, resp);
* resp.sendRedirect("index.jsp");
* 解决方案:
* 自定义mvc就出现了
* 框架:反射+设计模式,案例:通用分页+自定义page标签+自定义mvc的组合就是框架
*
*
* @author Administrator
*
*/
//@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 {
/**
* 增删改查缺陷:
* 当需求发送改变,或者新增需求的时候,需要改动下面代码
*
* 解决方案:
* 前台传递name到后台,实际就是想要调用当前(this)类对象的name方法
*/
String name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*if ("add".equals(name)) {
add(req,resp);
}else if ("delete".equals(name)) {
delete(req,resp);
}else if ("edit".equals(name)) {
edit(req,resp);
}else if ("list".equals(name)) {
list(req,resp);
}else if ("load".equals(name)) {
load(req,resp);
}*/
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.add()...");
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.delete()...");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("bookDao.edit()...");
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
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()...");
}
}
DeleteBookServlet.java
package com.lhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/delete")
public class DeleteBookServlet 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 {
System.out.println("bookDao.delete()...");
}
}
EditBookServlet.java
package com.lhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/edit")
public class EditBookServlet 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 {
System.out.println("bookDao.edit()...");
}
}
ListBookServlet.java
package com.lhw.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/list")
public class ListBookServlet 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 {
System.out.println("bookDao.list()...");
}
}
rebel.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
Refer to https://manuals.zeroturnaround.com/jrebel/standalone/config.html for more information.
-->
<application generated-by="eclipse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_1.xsd">
<classpath>
<dir name="D:/Java/java/J2EE/J2ee12/build/classes">
</dir>
</classpath>
<web>
<link target="/">
<dir name="D:/Java/java/J2EE/J2ee12/WebContent">
</dir>
</link>
</web>
</application>
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>
增删改查的代码2.0
<a href="${pageContext.request.contextPath}/book.action?name=add">增加</a>
<a href="${pageContext.request.contextPath}/book.action?name=delete">删除</a>
<a href="${pageContext.request.contextPath}/book.action?name=edit">修改</a>
<a href="${pageContext.request.contextPath}/book.action?name=list">查询</a>
<hr>
增删改查的代码3.0
<a href="${pageContext.request.contextPath}/book.action?name=load">回显</a>
<a href="${pageContext.request.contextPath}/book.action?name=ref">关联</a>
</body>
</html>
三:页面效果以及运行结果