自定义MVC的初步实现

这篇具有很好参考价值的文章主要介绍了自定义MVC的初步实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

在上一篇博客,我们介绍了MVC的演变过程,以及简单地实现了自定义MVC,在这篇博客中,我们进一步优化代码

一、 工作流程图

自定义MVC的初步实现,软件架构与设计模式,mvc

二、简单的实现自定义MVC

  • 创建一个能处理所有前端发送过来请求的Servle,即中央控制器,拿到所有方法的反射代码就在这里,并根据请求的类型调用相应的业务逻辑(去子控制器)
  • 创建一个子控制器,用于处理特定的用户请求或操作,这是真正处理用户请求的Servlet
  • 创建一个定义方法的Servlet,继承子控制器,供子控制器调用方法

Controller层——Servlet

中央控制器

package com.xqx.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;


/** 中央控制器 即工作流程图中的ActionServlet
 * @author W许潜行
 * 2023年6月29日 下午8:10:04
 */
@WebServlet("*.action")
public class DispatherServlet extends HttpServlet {
	Map<String,Action> mapAction=new HashMap<>();
	/**
	 * 初始化方法
	 */
	public void init() throws ServletException {
		mapAction.put("/book", new BookAction());
		super.init();
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到传过来的路径名
		String uri = request.getRequestURI();// /J2EE_MVC/book.action
		//得到请求的类
		uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));// /book
		//拿到对应的action
		Action action = mapAction.get(uri);
		//调用方法
		action.execute(request, response);
	}

}

当有请求进入时,我们首先获取请求的URI,并从中获取类似"/book"的路径名。然后,我们使用这个路径名作为键在mapAction中查找对应的Action对象。最后,执行该Action的execute方法来处理请求。

这个DispatcherServlet类的目的是根据传入的请求路径来分发请求给不同的Action类处理,通过这种方式实现请求的路由和控制,实现了基本的MVC模式

子控制器

package com.xqx.framework;

import java.io.IOException;
import java.lang.reflect.Method;

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

/**
 * 字控制器,真正处理请求的类
 * 
 * 
 * @author W许潜行 2023年6月29日 下午8:17:41
 */
public class Action {
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 拿到jsp传来的method
		String method = request.getParameter("method");
		try {
			Method m = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
			m.setAccessible(true);
			m.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

这个Action类的作用是通过反射机制根据传入的method参数值来调用具体的方法进行请求处理。每个实际的Action类都可以继承这个基类,并重写具体的方法来实现自己的业务逻辑。

具体Action类

package com.xqx.framework;

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

/**定义crud方法
 * @author W许潜行
 * 2023年6月29日 下午8:55:46
 */
public class BookAction extends Action{
	public void list(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("list");
	}

	public void upd(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("upd");
	}

	public void del(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("del");
	}

	public void add(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("add");		
	}
}

view层——JSP

<%@ 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>
<h1>极易MVC</h1>
<a href="bookAdd.action">新增</a>
<a href="bookDel.action">删除</a>
<a href="bookUpd.action">修改</a>
<a href="bookList.action">查看</a>
<hr>
<h1>简易MVC</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
<hr>
<h1>普易MVC</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
</body>
<h1>MVC架构初实现</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
</body>
</html>

打印结果:
自定义MVC的初步实现,软件架构与设计模式,mvc
简单的MVC架构就基本完成了,但还有很多优化之处,接下来我们来优化。

三、初步实现自定义MVC

简单MVC架构中的问题

既然要做到通用,那里面就不能出现写死的类呀
自定义MVC的初步实现,软件架构与设计模式,mvc
怎样可以灵活地拿到所有要初始化的地址/类呢?

答案是:通过反射建模完成,将所需要操作的类,在XML文件中配置即可

3.1 配置XML文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/order" type="com.xqx.framework.OrderAction">
		<forward name="list" path="res.jsp" redirect="false" />
		<forward name="toList" path="res.jsp" redirect="true" />
	</action>
	<action path="/book" type="com.xqx.framework.BookAction">
		<forward name="list" path="res.jsp" redirect="false" />
		<forward name="toList" path="res.jsp" redirect="true" />
	</action>
</config>

3.2 建模

ForwardModel

package com.xqx.framework.model;

public class ForwardModel {
	
	private String name;
	private String path;
	private boolean redirect;
	public ForwardModel() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Forward [name=" + name + ", path=" + path + ", redirect=" + 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;
	}
	public ForwardModel(String name, String path, boolean redirect) {
		super();
		this.name = name;
		this.path = path;
		this.redirect = redirect;
	}
	
	
}

ActionModel

package com.xqx.framework.model;

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

public class ActionModel {

	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 Map<String, ForwardModel> getfMap() {
		return fMap;
	}

	public void setfMap(Map<String, ForwardModel> fMap) {
		this.fMap = fMap;
	}

	public void push(ForwardModel fd) {
		fMap.put(fd.getName(), fd);

	}

	public ForwardModel pop(String name) {

		return fMap.get(name);
	}

}

ConfigModel

package com.xqx.framework.model;

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

public class ConfigModel {


	private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();

	public void push(ActionModel ac) {
		aMap.put(ac.getPath(), ac);

	}

	
	public ActionModel pop(String path) {

		return aMap.get(path);

	}
}

ConfigModelFactory

package com.xqx.framework.model;

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;

public class ConfigModelFactory {
	public static ConfigModel build() throws Exception {
		String xPath = "/config.xml";
		return build(xPath);

	}

	public static ConfigModel build(String xPath) throws Exception {

		ConfigModel cm = new ConfigModel();
		InputStream is = ConfigModelFactory.class.getResourceAsStream(xPath);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(is);
		List<Element> action = doc.selectNodes("//action");
		for (Element actionEle : action) {
			ActionModel am = new ActionModel();
			am.setPath(actionEle.attributeValue("path"));
			am.setType(actionEle.attributeValue("type"));
			List<Element> forward = actionEle.selectNodes("forward");
			for (Element forwardEle : forward) {
				ForwardModel fm = new ForwardModel();
				fm.setName(forwardEle.attributeValue("name"));
				fm.setPath(forwardEle.attributeValue("path"));
				fm.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
				am.push(fm);
			}
			cm.push(am);
		}
		return cm;
	}

建模的详细介绍——>建模详解

3.2 Servlet

中央控制器

package com.xqx.framework;

import java.io.IOException;
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.xqx.framework.model.ActionModel;
import com.xqx.framework.model.ConfigModel;
import com.xqx.framework.model.ConfigModelFactory;
import com.xqx.framework.model.ForwardModel;

/**
 * 中央控制器 即工作流程图中的ActionServlet
 * 
 * @author W许潜行 2023年6月29日 下午8:10:04
 */
@WebServlet("*.action")
public class DispatherServlet extends HttpServlet {
	// Map<String,Action> mapAction=new HashMap<>();
	// 之前子控制器在Map里,现在xml文件里
	private ConfigModel configModel;

	/**
	 * 初始化方法
	 */
	public void init() throws ServletException {
		// mapAction.put("/book", new BookAction());
		try {
			// 包含所有子控制器
			configModel = ConfigModelFactory.build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.init();
	}

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

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 得到传过来的路径名
		String uri = request.getRequestURI();// /J2EE_MVC/book.action
		// 得到请求的类
		uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));// /book
		// 拿到对应的type
		ActionModel actionModel = configModel.pop(uri);
		if (actionModel == null) {
			throw new RuntimeException("action is null");
		}
		String type = actionModel.getType();
		try {
			// 类实例
			Action action = (Action) Class.forName(type).newInstance();
			if (action instanceof ModelDriver) {
				ModelDriver md=(ModelDriver) action;
				Object model = md.getModel();
				Map<String, String[]> parameterMap = request.getParameterMap();
				BeanUtils.populate(model, parameterMap);
			}
			// 调用方法 list/toList
			String execute = action.execute(request, response);
			// 为了动态配置业务代码执行完毕将会转发/重定向到指定页面
			ForwardModel forwardModel = actionModel.pop(execute);
			if (forwardModel == null) {
				System.out.println("定义跳转一个错误页面...");
				return;
			}
			if (!forwardModel.isRedirect()) {
				response.sendRedirect(request.getContextPath() + "/" + forwardModel.getPath());
			} else {
				request.getRequestDispatcher(forwardModel.getPath()).forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

init()方法用于初始化,在该方法中,从配置文件中构建了一个ConfigModel实例,该实例包含了所有的子控制器的配置信息

在doPost()方法中,首先获取请求的URI,然后从URI中提取出请求的类名,即控制器名称。

根据控制器名称从configModel中获取相应的ActionModel对象,ActionModel对象包含了具体控制器的配置信息,包括控制器类的全名和该控制器的执行结果与转发/重定向的配置

根据ActionModel对象中的控制器类名实例化一个控制器对象,并判断控制器是否实现了ModelDriver接口,如果实现了,将请求参数封装到模型对象中。调用控制器的execute()方法执行具体的业务逻辑,返回执行结果。

根据执行结果从ActionModel对象中获取相应的ForwardModel对象,ForwardModel对象包含了执行结果对应的转发/重定向路径的配置信息

根据ForwardModel对象判断是进行转发还是重定向操作,然后将请求转发或重定向到相应的页面

如果没有找到对应的ActionModel或ForwardModel,则打印错误信息。

子控制器

package com.xqx.framework;

import java.io.IOException;
import java.lang.reflect.Method;

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

/**
 * 子控制器,真正处理请求的类
 * 
 * 
 * @author W许潜行 2023年6月29日 下午8:17:41
 */
public class Action {
	public String  execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 拿到jsp传来的method
		String method = request.getParameter("method");
		String res = null;
		try {
			Method m = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
			m.setAccessible(true);
			//拿到方法返回的值 list/toList
			res = (String) m.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;//
	}
}

该Action类的作用是通过动态调用不同的方法来执行不同的业务逻辑,并根据方法的返回值作为执行结果返回给调用者

模型驱动接口

package com.xqx.framework;

/**模型驅動接口
 * @author W许潜行
 * 2023年7月2日 下午7:17:02
 * @param <T>
 */
public interface ModelDriver<T> {
	T getModel();
}

BookAction

package com.xqx.framework;

import java.util.Map;

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

import com.xqx.entity.Book;

/**
 * 定义crud方法
 * 
 * @author W许潜行 2023年6月29日 下午8:55:46
 */
public class BookAction extends Action implements ModelDriver<Book>{
	Book book=new Book();
	public String list(HttpServletRequest request, HttpServletResponse response) {
		
//		String bid = request.getParameter("bid");
//		String bname = request.getParameter("bname");
//		String price = request.getParameter("price");
//		book.setBid(Integer.valueOf(bid));
//		book.setBname(bname);
//		book.setPrice(Float.valueOf(price));
		//得到所有参数
//		Map<String, String[]> bookMap = request.getParameterMap();
		request.setAttribute("content", "hello");
		System.out.println("BookActionlist");
		return "toList";
	}

	public String upd(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("content", "hello");

		System.out.println("BookActionupd");
		return "list";
	}

	public String del(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("content", "hello");

		System.out.println("BookActiondel");
		return "list";
	}

	public String add(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("content", "hello");

		System.out.println("BookActionadd");
		return "list";
	}

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

通过继承Action类和实现ModelDriver接口,BookAction类提供了具体的业务方法,并且通过实现getModel()方法,将创建的Book对象作为模型对象,方便在控制器中使用和操作。

这样,当请求调用BookAction类的方法时,可以进行相应的业务处理,并将结果封装到模型中,方便在JSP中展示或进行后续操作。

3.3 jsp

bookList.jsp

<%@ 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>
<h1>极易MVC</h1>
<a href="bookAdd.action">新增</a>
<a href="bookDel.action">删除</a>
<a href="bookUpd.action">修改</a>
<a href="bookList.action">查看</a>
<hr>
<h1>简易MVC</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
<hr>
<h1>普易MVC</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
</body>
<h1>MVC架构简单实现</h1>
<a href="book.action?method=add">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
</body>
<h1>MVC架构初实现</h1>
<a href="book.action?method=add&&bid=1&&bname=aa&&price=9.9">新增</a>
<a href="book.action?method=del">删除</a>
<a href="book.action?method=upd">修改</a>
<a href="book.action?method=list">查看</a>
</body>
</html>

res.jsp


<%@ 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>
res页面,参数为:${content}
</body>
</html>

打印结果:
点击新增:
自定义MVC的初步实现,软件架构与设计模式,mvc

点击查看:
自定义MVC的初步实现,软件架构与设计模式,mvc
自定义MVC的初步实现,软件架构与设计模式,mvc文章来源地址https://www.toymoban.com/news/detail-531313.html

到了这里,关于自定义MVC的初步实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 软考高级系统架构设计师系列论文八十三:论软件设计模式的应用

    软考高级系统架构设计师系列之:面向构件的软件设计,构件平台与典型架构

    2024年02月11日
    浏览(37)
  • 自定义MVC的初步实现

    在上一篇博客,我们介绍了MVC的演变过程,以及简单地实现了自定义MVC,在这篇博客中,我们进一步优化代码 创建一个能处理所有前端发送过来请求的Servle,即中央控制器,拿到所有方法的反射代码就在这里,并根据请求的类型调用相应的业务逻辑(去子控制器) 创建一个

    2024年02月12日
    浏览(23)
  • MVC设计模式

    在当今的软件开发领域,MVC(Model-View-Controller)设计模式已经成为了一种广泛使用的架构模式。它为应用程序提供了一种结构化的方法,将数据、用户界面和业务逻辑分开,从而使得应用程序更易于维护、扩展和重用。 一、MVC的概述 MVC是一种分层的设计,主要用于解决UI交互

    2024年02月02日
    浏览(37)
  • Spring MVC学习之——了解MVC设计模式

    MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。 Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。 数据模型:POJO 业务模型:Service,Mapper View(视图) - 视图代表模型包含的

    2024年01月21日
    浏览(39)
  • 自定义实现一个线程安全的arrylist,使用设计模式

    要自定义实现一个线程安全的ArrayList,可以使用设计模式中的代理模式。代理模式可以通过创建一个代理类来控制对原始对象的访问,并在访问时添加额外的功能或限制。 下面是一个使用代理模式实现线程安全ArrayList的示例代码: 首先,定义一个接口 `List`,该接口包含Arr

    2024年01月24日
    浏览(25)
  • 【设计模式与范式:行为型】71 | 命令模式:如何利用命令模式实现一个手游后端架构?

    设计模式模块已经接近尾声了,现在我们只剩下 3 个模式还没有学习,它们分别是:命令模式、解释器模式、中介模式。这 3 个模式使用频率低、理解难度大,只在非常特定的应用场景下才会用到,所以,不是我们学习的重点,你只需要稍微了解,见了能认识就可以了。 今天

    2024年02月09日
    浏览(30)
  • 【软件架构模式——MVC、MVP、MVVM】

    1. MVC(Model-View-Controller) 概念介绍: MVC是一种软件架构模式,用于组织应用程序的代码和逻辑。它将应用程序分为三个核心部分:模型(Model)、视图(View)和控制器(Controller)。 **模型(Model)**负责处理应用程序的数据逻辑和状态。它表示应用程序的数据源,并封装了与

    2024年02月11日
    浏览(40)
  • 设计模式 ~ 职责链、策略、适配器、MVC、MVVM

    一种行为型设计模式,它允许多个对象按照特定的顺序处理请求,直到其中一个对象能够处理该请求为止 一个流程,需要多个角色处理,通过 一个“链”串联起来,各个角色相互分离,互不干扰 如:promise.then、Jquery 的链式 如果一项业务有太多的 if else 或 switch case,那么可

    2024年02月16日
    浏览(32)
  • 安卓基础巩固(四):设计原则、安卓主流技术框架MVC/MVP/MVVM、设计模式

    模块化功能:使得程序模块化,即内部高聚合,模块之间低耦合 提高开发效率:开发人员只需要专注于一点(视图显示、业务逻辑、数据处理) 提高测试效率:后期测试时可以迅速根据报错反馈,定位到问题出现的位置。 六大设计原则是设计模式的理论,设计模式是设计原

    2024年02月06日
    浏览(39)
  • Python web实战之 Django 的 MVC 设计模式详解

      技术栈:Python、Django、HTML、CSS、JavaScript。 在 Web 开发中,MVC(Model-View-Controller)模式是一种非常常见的设计模式,它可以帮助我们更好地管理代码,提高代码的可维护性。今天就介绍如何使用 Django 框架实现 MVC 模式。 MVC 模式是一种软件设计模式,它将应用程序分为三个

    2024年02月14日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包