自定义mvc框架

本文介绍了如何通过反射优化传统的增删改查操作,减少了大量重复类的创建,实现了一个通用的Servlet来处理不同的CRUD请求。同时,解释了MVC模式的工作原理,展示了DispatcherServlet如何作为中央控制器分发请求,Action接口和ActionSupport类如何协同处理业务逻辑,实现了代码的复用和模块化。

目录

1.初版的增删改查改进

 2.反射优化

3.mvc工作原理及对应打码

1.初版的增删改查改进

上述问题
        1.关于单个实体类/表操做场越多,需要新建的类也就越多,造成了项目中类的数量过于庞大

<h3>目前增删该查的方法</h3>
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/del">删除</a> 
<a href="${pageContext.request.contextPath }/book/edit">修改</a>
<a href="${pageContext.request.contextPath }/book/list">查看</a>

实际操作

 2.反射优化

<h3>类数量过多问题的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查看</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>

servlet展示

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		为了区分当前请求的目的,增删改查的目的,就从前台要调用的方法名传递到后台
		String methodName=request.getParameter("methodName");
//		methodNamek可能是add/del/edit
//		前台传递什么方法就调用当前类的对应方法
		try {
			Method m = this.getClass()//BookSerlvet
				.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.setAccessible(true);
//			调用当前类实例的methodName方法
			m.invoke(this,request,response);
		} catch (Exception  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		if("add".equals(methodName)) {
////			如果前台传递到后台是一个增加的请求,那么后台就调用增加的方法
//			add(request,response);
//		}
//		else if("del".equals(methodName)) {
//			del(request,response);
//		}
//		else if("edit".equals(methodName)) {
//			edit(request,response);
//		}
//		else if("list".equals(methodName)) {
//			list(request,response);
//		}
	}

	
//	方法
	
	private void list(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("在同一个orderservlet调用查看方法");
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("在同一个orderservlet调用修改方法");
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个orderservlet调用删除方法");
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个orderservlet调用增加方法");

	}
	
	private void load(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个orderservlet调用回显方法");

	}

代码执行

3.mvc工作原理及对应打码

package com.hz.framework;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 子控制器
 * 	对应请求的处理人
 * @author Administrator
 *
 */
public interface Action {
	void execute(HttpServletRequest request, HttpServletResponse response);
	
}


--------------------------------------------------------------------------------
package com.hz.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 request, HttpServletResponse response) {
		String methodName=request.getParameter("methodName");
//		methodNamek可能是add/del/edit
//		前台传递什么方法就调用当前类的对应方法
		try {
			Method m = this.getClass()//BookSerlvet
				  .getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.setAccessible(true);
//			调用当前类实例的methodName方法
			m.invoke(this,request,response);
		} catch (Exception  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

}


-----------------------------------------------------------------------------------
package com.hz.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.hz.web.BookAction;

/**
 * 中央控制器
 * 	主要职能:接收浏览器请求,找到对应的处理人
 * 
 * @author Administrator
 *
 */
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
	private Map<String, Action> action=new HashMap<String, Action>();
	
//	程序启动时只会加载一次
	@Override
	public void init() throws ServletException {
		action.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/mvc/book.action?methodName=list
		String uri = req.getRequestURI();
//		要拿到/book. 接收最后一个/到最后一个点的位置
		uri=uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
		
		Action action2 = action.get(uri);
		action2.execute(req, resp);
	}
}


--------------------------------------------------------------------------------------
package com.hz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hz.framework.Action;
import com.hz.framework.ActionSupport;

public class BookAction extends ActionSupport{
	private void list(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("在同一个servlet调用查看方法");
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("在同一个servlet调用修改方法");
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个servlet调用删除方法");
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个servlet调用增加方法");

	}
	
	private void load(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("在同一个servlet调用回显方法");

	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值