自定义MVC

自定义MVC

  • 什么是mvc?
  • mvc结构
  • mvc核心思想
  • mvc和三层架构的区别
  • 自定义mvc工作原理
  • model和model2的模式
使用mvc前要先导入的jar

在这里插入图片描述

一、什么是mvc?

mvc全名:Model (模型层) View (视图层)Controller(控制层)

mvc它是一种软件设计典范,用于业务逻辑处理,数据,界面显示分离。

二、mvc结构

M(模型层) 实体域模型entity(名称) 过程域模型dao(动词)

C(视图层) servlet/action/controller

V(控制层) jsp/ios/Android/微信公众号/小程序/HTML

注:1.不能跨层调用
2.只能出现由上而下的调用(View->Controller->Model)

三、mvc核心思想

各司其职(mvc可以减少后台代码)

四、mvc和三层架构的区别

1.mvc是一种设计模式,其目的是让html和业务逻辑分开
2.三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注增加擅长的模块即可

五、自定义mvc工作原理

在这里插入图片描述
在这里插入图片描述

控制器的父类 Action
package com.lishengnan.mvc.framework;

import java.io.IOException;

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

/**
 * 控制器的父类
 * @author 可乐
 *
 * 可乐的爱妃
 */
public abstract class Action {

	public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
	
}

中央控制器 ActionServlet
public class ActionServlet extends HttpServlet {
	
	private Map<String, Action> map;
	
	/**
	 * 初始化
	 */
	@Override
	public void init() throws ServletException {
		map = new HashMap<String, Action>();
		map.put("/helloAction", new HelloAction());
		map.put("/addAction", new AddAction());		
		
	}
	
	@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 {
		//1、获取请求路径
		String requestURI = req.getRequestURI();
		System.out.println(requestURI);
		//System.out.println(req.getRequestURL());
		
		int start = requestURI.lastIndexOf("/");
		int end = requestURI.lastIndexOf(".action");
		
		//2、截取请求路径中的 /*部分
		String actionName = requestURI.substring(start, end);
		
		//3、通过截取路径中的actionName找到对应的子控制器
		Action action = map.get(actionName);  //等于  new HelloAction()
		System.out.println(actionName);
		//  Action action =   new HelloAction();   里氏替换原则
		
		//4、执行execute方法
		action.execute(req, resp);
		
	}

}

六、model和model2的模式

model1:jsp+jdbc

model2:mvc(高内聚 低耦合 各司其职)

总结:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值