自定义MVC

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);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值