自定义MVC(一)

1. 什么是MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码

Model1 jsp+jdbc

Model2 ->MVC

核心思想:各司其职

2. MVC结构

V
jsp/ios/android
C
servlet/action
M
实体域模型(名词)
过程域模型(动词)

web 做浏览器请求分发
service 调用dao处理项目业务的
dao 操作数据库

注1:不能跨层调用
注2:只能出现由上而下的调用

3. 自定义MVC工作原理图
主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器

4.自定义mvc原理图
在这里插入图片描述
主控制器:

package com.luoxiaogang.framework;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

import com.zhuyuncong.web.AddCalAction;
import com.zhuyuncong.web.CccCalAction;
import com.zhuyuncong.web.ChuCalAction;
import com.zhuyuncong.web.DelCalAction;

/**
 * 中央控制器
 *  作用:接收请求,通过请求寻找处理请求对应的子控制器
 * @author MACHENIKE
 *
 */
public class DispatcherServlet extends HttpServlet {

	private static final long serialVersionUID = 9213618038567713003L;

	private Map<String, Action> actionMap = new  HashMap<>();
	
	public void init() {
		actionMap.put("/addCal", new AddCalAction());
		actionMap.put("/delCal", new DelCalAction());
		actionMap.put("/cccCal", new CccCalAction());
		actionMap.put("/chuCal", new ChuCalAction());
	}
	
	@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 {
		init();
		String url = req.getRequestURI();
		url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
		Action action = actionMap.get(url);
		action.execute(req, resp);
	}
}

子控制器:

package com.luoxiaogang.framework;

import java.io.IOException;

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

/**
 * 子控制器
 * 	作用:用来直接处理浏览器发送过来的请求
 * @author MACHENIKE
 *
 */
public interface Action {
   String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException; 
}

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>
<script type="text/javascript">
	function mya(n) {
		if(n == 1){
			calForm.action="${pageContext.request.contextPath }/addCal.action";
		}else if(n == 2){
			calForm.action="${pageContext.request.contextPath }/delCal.action";
		}else if(n == 3){
			calForm.action="${pageContext.request.contextPath }/cccCal.action";
		}else if(n == 4){
			calForm.action="${pageContext.request.contextPath }/chuCal.action";
		}
		calForm.submit();
	}
</script>
</head>
<body>
<form name="calForm" action="" method="post">
	num1:<input type="text" name="num1"><br>
	num2:<input type="text" name="num2"><br>
	<input type="button" value="+" onclick="mya(1)">
	<input type="button" value="-" onclick="mya(2)">
	<input type="button" value="*" onclick="mya(3)">
	<input type="button" value="/" onclick="mya(4)">
</form>
</body>
</html>

处理加法的子控制器:

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class AddCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)+Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	} 
}

处理减法的子控制器:

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class DelCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

处理乘法的子控制器:

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class CccCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

处理除法的子控制器:

package com.zhuyuncong.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class ChuCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Float.parseFloat(num1)/Float.parseFloat(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求
没有就报错,就处理不了请求

子控制器:就是处理用户请求用的

猜你喜欢

转载自blog.csdn.net/weixin_45177212/article/details/93741304