自定义mvc1

1、什么是MVC?
mvc全名:Model View Controller,其中Model(模型层)、View(视图层)、Controller(控制层)
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离

常用模式为:
model1:jsp+jdbc
model2:mvc
高内聚 低耦合 各司其职

三层架构和mvc的区别:
三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注自己擅长模块即可
MVC是一种设计模式,其目的是让html和业务逻辑分开

2、MVC结构
V(视图层) --> JSP/HTML/freemarker
C(控制层) --> Servlet/Action/Controller
M(模型层) --> entity、dao

entity:实体域模型(名词)
dao:过程域模型(动词)

注:1)不能跨层调用;
2)只能由上往下进行调用;View --> Controller --> Model

3.自定义MVC工作原理图

    *.action           调度           截取*(请求路径名) 处理具体业务逻辑

JSP -----------> Servlet(中央控制器)------->Action(子控制器)—>Model(Dao、DB)

4、MVC的实现
a、创建一个抽象类,定义一个抽象的方法execute(处理具体逻辑)

package com.xieying.framework;

import java.io.IOException;

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

/**
 * 控制器的父类,完成具体的业务逻辑
 * @author 莹酱
 *
 */
public abstract class Action {

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

b、创建HelloAction并继承抽象类Action,重写execute方法

package com.xieying.action;

import java.io.IOException;

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

import com.xieying.framework.Action;

public class HelloAction extends Action{

	@Override
	protected String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("温柔一刀什么时候发货");
		return null;
	}

	
}

c、在ActionServlet中定义私有Map<String,Action>(根据不同请求路径名调用不同逻辑处理Action类)

private Map<String , Action> map;

d、在ActionServlet中的init方法初始化Map集合
map.put(‘请求路径’,‘逻辑处理Action类’)

@Override
	public void init() throws ServletException {
		map=new HashMap<String, Action>();
		map.put("/.helloAction", new HelloAction());
		map.put("/addAction", new AddAction());
	}

e、在ActionServlet中的doPost方法中处理请求

@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());
		//   /mvc/addAction.action
		int start=requestURI.lastIndexOf("/");
		int end=requestURI.lastIndexOf(".action");
		
		//2、截取请求路径中的 /*action 的 /*部分
		String actionName = requestURI.substring(start, end);
		System.out.println(actionName);
		
		//3、通过截取路径中的actionName找到对应的子控制器
		Action action = map.get(actionName);   //等于 new HelloAction();
		//Action  action=new HelloAction();
		
		//执行execute方法
		action.execute(req, resp);
		
	}

5、MVC流程演示
a、在ActionServlet中的init方法中添加
map.put(’/AddAction’,new AddAction());

@Override
	public void init() throws ServletException {
		map=new HashMap<String, Action>();
		map.put("/.helloAction", new HelloAction());
		map.put("/addAction", new AddAction());
	}

b、创建AddAction继承抽象类Action,重写execute方式,处理逻辑后,返回结果

package com.xieying.action;

import java.io.IOException;

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

import com.xieying.framework.Action;

public class AddAction extends Action{

	@Override
	protected String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		
		int num3=Integer.parseInt(num1)+Integer.parseInt(num2);
		req.setAttribute("num3", num3);
		req.getRequestDispatcher("result.jsp").forward(req, resp);
		
		return null;
	}

}

c、jsp界面演示:
form表单提交数据,进行逻辑处理
在这里插入图片描述
获得结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值