IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 自定义mvc01 -> 正文阅读

[开发测试]自定义mvc01

一、思维导图

二,代码

Action

package com.zking.framework;

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


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()...");
	}*/

}

ActionSuppor

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 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

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;



@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

package com.zking.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

package com.zking.web;

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


import com.zking.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

package com.zking.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.它的运行原理
 *  2.1代码演绎过程
 *  2.2总结代码运行原理
 *  
 *   思考:
 *  	什么是mvc
 *   Model模型、View视图、controller控制层
 *   mvc的出现原因:各司其职
 *   一个餐馆: 点菜、收银、炒菜、上菜、收盘子、洗盘子
 *   发展壮大: 点菜员、收银员、厨师、传菜员、洗碗阿姨...
 *   
 *   不足:
 *   Model模型:
 *   Dao层: (增删改  查)
 *   	1、建立数据库连接
 *   	2、预定义对象Preparestatement
 *   	3、执行查询
 *   	4、处理结果集
 *   通用分页解决了上面的问题
 *   View视图:
 *   		1、重复的HTML分页条代码
 *   		2、重复的JS代码
 *   		自定义Page标签
 *   controller控制层:
 *   			1、重写了doGet、doPost,并且doGet没有用
 *   			2、参数的封装代码沉余了
 *   				req.getParammeter("xxxx");
 * 				3.对于跳转页面的代码是重复的
 * 					req.getDispathtype("index.jsp").forward(req,resp);
 * 					resp.sendRedirect("bookList.jsp");
 * 解决方案:
 * 		自定义mvc就出现了
 * 		框架: 反射+设计模式   案例: 通用分页+自定义Page标签+自定义mvc的组合就是框架
 *  
 * @author My
 *
 */

//@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 {
		
		/**
		 * 增删改查缺陷
		 * 当需求发生改变,或者新增需求的时候,需要改动下面代码   load
		 * 
		 * 解决方案
		 * 		前台传递methodName到后台,实际就是想要调用当前类对象的methodName方法
		 * 		this.methodName
		 * 
		 */
		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();
		}
		//  methodName=load
//		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) {
		System.out.println("bookDao.add()...");
		
	}
	private void edit(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.edit()...");
		
	}
	private void delete(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.delete()...");
		
	}
	private void list(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.list()...");
		
	}
	private void ref(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.ref()...");
		
	}
	private void  load(HttpServletRequest req, HttpServletResponse resp) {
		//  修改页面数据回显
		System.out.println("bookDao. load()...");
		
	}

}

DeleteBookServlet2

package com.zking.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 DeleteBookServlet2  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()...");
	}
}

EditBookServlet2?

package com.zking.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 EditBookServlet2  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()...");
	}
}

ListBookServlet2

package com.zking.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 ListBookServlet2  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()...");
	}
}

3、运行代码

?

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-31 15:45:12  更:2021-08-31 15:45:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/17 22:20:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码