1.思维导图
?2.代码
?
<%@ 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>
</body>
</html>
package com.zking.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 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();
}
}
}
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 com.zking.web.BookAction;
/**
* 目标:
* 根据自定义mvc框架的原理图完成 框架的研发
* @author zjjt
*中央控制器
*寻找子控制器
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet{
//放子控制器的容器
private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
//初始化子控制器容器(集合),经过初始化,actions容器内存就有了子控制器
//init,service,destroy
@Override
public void init() throws ServletException {
actions.put("/book", new BookAction());
/*actions.put("/order", new BookAction());
actions.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://localhost:8080/t266_mvc/book.action?methodName=add
//目标:BookAction.add()..
/**
* 思路:
* 1.从游览器URL中获取到“/book”字符串
* 2.在子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(uri);
//ActionSupport action=new BookAction()
action.execute(req, resp);
}
}
?
|