2021-08-30

 自定义MVC的V1.0和V2.0版本

@Override
			protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				// TODO Auto-generated method stub
				doPost(req, resp);
			}
			@Override
			protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
				/**
				 * 增删改查缺陷
				 * 当需求发生改变或者新增需求,需要改动下面代码
				 */
		String  methodName	= req.getParameter("methodName");
		if ("add".equals(methodName)) {
			add(req,resp);
		}else if ("edit".equals(methodName)) {
			edit(req,resp);
		}else if ("delete".equals(methodName)) {
			delete(req,resp);
		}else if ("list".equals(methodName)) {
			list(req,resp);
		}else if ("load".equals(methodName)) {
			load(req,resp);
		}
			}
			private void add(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.add()...");
			}
			private void edit(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.edit()...");
			}
			private void delete(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.delete()...");
			}
			private void list(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.list()...");
			}
			//修改页面数据回显
			private void load(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.load()...");
			}

  * 增删改查缺陷
                 * 当需求发生改变或者新增需求,需要改动下面代码

如图上的load在最下面回显后需要在上面添加

else if ("load".equals(methodName)) {
            load(req,resp);
        }

  * 解决方案
                 * 做判断的目的是为了调用方法
                 * 比如浏览器发了个请求最终目的是掉方法,我们想要做的是能不能跳过他们直接调用当前类对象的方法
                 * this.methodName

		String  methodName	= req.getParameter("methodName");
		try {
			Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.invoke(this, req,resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		/**
		if ("add".equals(methodName)) {
			add(req,resp);
		}else if ("edit".equals(methodName)) {
			edit(req,resp);
		}else if ("delete".equals(methodName)) {
			delete(req,resp);
		}else if ("list".equals(methodName)) {
			list(req,resp);
		}else if ("load".equals(methodName)) {
			load(req,resp);
		}*/
			}
			private void add(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.add()...");
			}
			private void edit(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.edit()...");
			}
			private void delete(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.delete()...");
			}
			private void list(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.list()...");
			}
			//修改页面数据回显
			private void load(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.load()...");
			}

然后这样就我们的V3.0版本基本上也出来了

增删改查代码V3.0
		<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
		<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联查询</a>

运行一下

 看看方法

//关联查询
			private void ref(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.list()...");
			}
			//修改页面数据回显
			private void load(HttpServletRequest req, HttpServletResponse resp) {
				// TODO Auto-generated method stub
				System.out.println("bookDao.load()...");

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值