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 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 自定义mvc02 -> 正文阅读

[开发测试]自定义mvc02

思维导图

?Book

package com.zhj.Servlet;

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.它的运行原理
 *   	
 *   
 *   思考:
 *   	什么是mvc
 *   	MODEL模型 、view视图 、controller控制层
 *  	mvc的出现原因:各司其职
 * 
 * MODEL模型不足:(通用分页解决了上面的问题)
 * 	1.建立数据库连接
 * 	2.预定义对象
 *  3.执行查询
 *  4.处理结果集
 *
 *view视图不足:(自定义page标签)
 *	1.重复的HTML分页条代码
 *	2.重复的js代码
 *	
 *controller控制层:(自定义mvc解决)
 *	1.重写了doget。dopost 并且doget没有用
 *	2.参数的封装代码冗余了
 *		req.getParammeter("xxx");
 *	3.对于跳转页面的代码是重复的
 *		req.getDispathtype("index.jsp").forward(req,resp)
 *
 *	框架:反射+设计模式  案例:通用分页+自定义page标签+自定义mvc的组合就是框架
 */
//@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 {
		/**
		 * 当前代码缺陷
		 * 		当需求发生改变,或者新增需求时,需要改动代码
		 * tongguo
		 * 解决方法
		 * 前台传递methodName到后台,实际机就是想要调用当前类对象的method方法
		 * 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();
		} 
		
		
		
		//有缺陷方法
		/*if("add".equals(methodName)) {
			add(req, resp);
		}else if("delete".equals(methodName)) {
			delete(req, resp);
		}else if("edit".equals(methodName)) {
			edit(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("add()");
	}
	
	private void delete(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("delete()");
	}
	
	private void edit(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("edit()");
	}
	
	private void list(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("list()");
	}
	
	private void ref(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("ref()");
	}
	
	private void load(HttpServletRequest req,HttpServletResponse resp) {
		//修改数据回显
		System.out.println("load()");
	}*/
}

?Action

package com.zhj.framework;

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

/**
 * 子控制器
 * 1.处理前台的浏览器请求
 * 2.运行jsp传递到后台的方法字符串所代表的方法
 * @author Administrator
 *
 */
public interface Action {
	public String execute(HttpServletRequest req,HttpServletResponse resp) ;
	
	
	
}

ActionModel

package com.zhj.framework;

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
//	<action path="/regAction" type="test.RegAction">
	private String path;
	private String type;
	private Map<String, ForwardModel> fMap = new HashMap<>();
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void push(ForwardModel forwardModel) {
		fMap.put(forwardModel.getName(), forwardModel);
	}
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
}

ActionSupport

package com.zhj.framework;

import java.lang.reflect.Method;

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

public class ActionSupport implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) {
		String methodName = req.getParameter("methodName");
		String res=null;
		try {
			Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.setAccessible(true);
			res=(String) m.invoke(this, req,resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return res;
		
	}

}

ConfigModelFactory

package com.zhj.framework;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author Administrator
 *
 */
public class ConfigModelFactory {
	public static ConfigModel build() throws Exception {
		return build("config.xml");
	}

	public static ConfigModel build(String resourcepath) throws Exception {
		InputStream in = ConfigModelFactory.class.getResourceAsStream(resourcepath);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(in);
		ConfigModel configModel = new ConfigModel();
		List<Element> actionEles = doc.selectNodes("/config/action");
		for (Element actionEle : actionEles) {
			ActionModel actionModel = new ActionModel();
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
			List<Element> forwardEles = actionEle.selectNodes("forward");
			for (Element forwardEle : forwardEles) {
				ForwardModel forwardModel = new ForwardModel();
				forwardModel.setName(forwardEle.attributeValue("name"));
				forwardModel.setPath(forwardEle.attributeValue("path"));
				forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
				actionModel.push(forwardModel );
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
}

DispatchServlet

package com.zhj.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 org.apache.commons.beanutils.BeanUtils;

import com.zhj.Servlet.BookAction;
import com.zhj.Servlet.GoodsAction;

/**
 * 目标:
 * 根据自定义mvc框架的原理图 完成 框架地研发
 * @author zhj
 * 中央控制器
 *	寻找子控制器
 */
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet{
	//专门存放子控制器的容器
//	private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
	private ConfigModel configModel=null;
	//初始化子控制器(集合),经过初始化,actions容器里面就有的子控制器
	/**
	 * 需求:在增加一个商品类增删查改
	 * 步骤:
	 * 	改动init中代码
	 * 思考
	 *   能不能不改动代码完成这个需求
	 *   参考DBAccess的数据配置文件config.properties
	 *   	1.减少代码改动的风险
	 *   	2.减少代码的编译次数(对于已经部署到服务器后)
	 *   解决方案:
	 *   改成控制器可配置
	 *   解决步骤:
	 *   	1.必须有配置文件config.xml
	 *   	2.配置文件config.xml中要包含处理业务的子控制器
	 *   	3.读取到配置文件config.xml中对应的处理浏览器请求的子控制器
	 */
	@Override
	public void init() throws ServletException {
		try {
			configModel = ConfigModelFactory.build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@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/javaEE_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("."));
		ActionModel actionModel = configModel.pop(uri);
		String type = actionModel.getType();
		ActionSupport action;
		try {
			action = (ActionSupport) Class.forName(type).newInstance();
			/**
			 * 解决参数封装问题
			 */
			if(action instanceof ModelDriver) {
				//有对象
				ModelDriver m = (ModelDriver) action;
				Object obj = m.getModel();
				//接受所有的前端JSP传递到后台的参数
				Map<String, String[]> parameterMap = req.getParameterMap();
				//对 对象 赋值
				//PropertyUtils.getProperty(obj, "")
				BeanUtils.populate(obj, parameterMap);
			}
			//execute->delete
			String res = action.execute(req, resp);
			/*
			 * 思路
			 * 1.方法执行完毕必须有一个返回值
			 * 2.通过返回值决定是否重定向,还是转发
			 * 3.通过返回值决定跳转哪一个页面
			 */
			ForwardModel forwardModel = actionModel.pop(res);
			if(null!=forwardModel) {
				if(forwardModel.isRedirect()) {
					resp.sendRedirect(req.getContextPath()+forwardModel.getPath());
					System.out.println(req.getContextPath());
				}else {
					req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
	}
}

ForwardModel

package com.zhj.framework;

public class ForwardModel {
//	<forward name="success" path="/login.jsp" redirect="true" />
	private String name;
	private String path;
	private boolean redirect;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public boolean isRedirect() {
		return redirect;
	}
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}
	
}

ModelDriver

package com.zhj.framework;

public interface ModelDriver<T> {
	/**
	 * 是中央控制器来做实体类封装的
	 * @return
	 */
	T getModel();
}

config

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/book" type="com.lhw.Servlet.BookAction">
		<forward name="list" path="/bookList.jsp" redirect="false" />
<!-- 		增删改操作之后要再一次查询数据库的新的数据做展示 -->
		<forward name="toList" path="/book.action?methodName=list" redirect="true" />
	</action>
</config>

BookAction

package com.zhj.Servlet;

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

import com.zhj.entity.Book;
import com.zhj.framework.ActionSupport;
import com.zhj.framework.ModelDriver;

public class BookAction extends ActionSupport implements ModelDriver<Book>{
	private Book book = new Book();
	
	private void add(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("BookAction调用add()");
	}
	
	private String delete(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("BookAction调用delete()");
		/*
		 * 需求:删除数据后跳转到书籍展示页面
		 * 缺陷:1.出现大量重复代码	2.不好管理(增删改重新跳转到查询界面)
		 * 解决方案:在一个地方统一管理重定向还是转发,并且是定位跳转到哪一个页面
		 * 预测结果
		 * 	BookAction调用bookDao.delete()...
		 * 	BookAction调用bookDao.list()...
		 * 
		 */
		return "toList";
		
	}
	
	private void edit(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("BookAction调用edit()");
	}
	
	private String list(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("BookAction调用list()");
		return "list";
	}
	
	private void ref(HttpServletRequest req,HttpServletResponse resp) {
		
		
		System.out.println(book);
		System.out.println("BookAction调用ref()");
	}

	@Override
	public Book getModel() {
		// TODO Auto-generated method stub
		return book;
	}

	
	
}

BookServlet

package com.zhj.Servlet;

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.它的运行原理
 *   	
 *   
 *   思考:
 *   	什么是mvc
 *   	MODEL模型 、view视图 、controller控制层
 *  	mvc的出现原因:各司其职
 * 
 * MODEL模型不足:(通用分页解决了上面的问题)
 * 	1.建立数据库连接
 * 	2.预定义对象
 *  3.执行查询
 *  4.处理结果集
 *
 *view视图不足:(自定义page标签)
 *	1.重复的HTML分页条代码
 *	2.重复的js代码
 *	
 *controller控制层:(自定义mvc解决)
 *	1.重写了doget。dopost 并且doget没有用
 *	2.参数的封装代码冗余了
 *		req.getParammeter("xxx");
 *	3.对于跳转页面的代码是重复的
 *		req.getDispathtype("index.jsp").forward(req,resp)
 *
 *	框架:反射+设计模式  案例:通用分页+自定义page标签+自定义mvc的组合就是框架
 */
//@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 {
		/**
		 * 当前代码缺陷
		 * 		当需求发生改变,或者新增需求时,需要改动代码
		 * tongguo
		 * 解决方法
		 * 前台传递methodName到后台,实际机就是想要调用当前类对象的method方法
		 * 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();
		} 
		
		
		
		//有缺陷方法
		/*if("add".equals(methodName)) {
			add(req, resp);
		}else if("delete".equals(methodName)) {
			delete(req, resp);
		}else if("edit".equals(methodName)) {
			edit(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("add()");
	}
	
	private void delete(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("delete()");
	}
	
	private void edit(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("edit()");
	}
	
	private void list(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("list()");
	}
	
	private void ref(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("ref()");
	}
	
	private void load(HttpServletRequest req,HttpServletResponse resp) {
		//修改数据回显
		System.out.println("load()");
	}*/
}

GoodsAction

package com.zhj.Servlet;

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

import com.zhj.framework.ActionSupport;

public class GoodsAction extends ActionSupport{
	public void add(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("goodsDao调用add()");
	}
	
	public void delete(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("goodsDao调用delete()");
	}
	
	public void edit(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("goodsDao调用edit()");
	}
	
	public void list(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println("goodsDao调用list()");
	}
}

rebel

<?xml version="1.0" encoding="UTF-8"?>

<!--
  This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
  Refer to https://manuals.zeroturnaround.com/jrebel/standalone/config.html for more information.
-->
<application generated-by="eclipse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_1.xsd">

	<classpath>
		<dir name="F:/作业/Y1/J2ee-15/build/classes">
		</dir>
	</classpath>

	<web>
		<link target="/">
			<dir name="F:/作业/Y1/J2ee-15/WebContent">
			</dir>
		</link>
	</web>

</application>

booklist

<%@ 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>
书籍展示页面
</body>
</html>

index

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

二代增删改查代码
<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>
三代增删改查代码
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联</a>
<hr>
演示原有初始化子控制器的缺陷
<a href="${pageContext.request.contextPath }/goods.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=delete">删除</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/goods.action?methodName=list">查询</a>
<hr>
解决参数实体类封装问题
<form action="${pageContext.request.contextPath }/book.action?methodName=ref" method="post">
<input type="text" name="bid" value="11">
	<input type="text" name="bname" value="zhj">
	<input type="text" name="price" value="222">
	<input type="text" name="author" value="333">
	<input type="text" name="publish" value="444">
 	<input type="text" name="remark" value="555"> 
	<input type="submit">


</form>


</body>
</html>

运行

?

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-09-01 12:14:13  更:2021-09-01 12:14:17 
 
开发: 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年5日历 -2024/5/10 22:55:36-

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