自定义MVC模式

自定义MVC模式

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
其中核心的思想是:各司其职
自定义MVC的工作原理图在这里插入图片描述
自定义MVC模式的控制器会有两个种类,
1、主控制器:查看是否有对应的子控制器来处理用户请求,如果有就调用子控制器来处理请求,没有就报错,处理不了请求。
2、子控制器:就是处理用户请求用的。
主控制动态调用子控制器调用完成具体的业务逻辑。
3、一个主控制器下有多个子控制器

做一个加减乘除来检验自定义MVC模式
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 doSub(val){
		if(val==1){
			calForm.action="${pageContext.request.contextPath}/addCal.action";//绝对路径
		}
		else if(val==2){
			calForm.action="${pageContext.request.contextPath}/delCal.action";
		}
		else if(val==3){
			calForm.action="${pageContext.request.contextPath}/chenCal.action";
		}
		else if(val==4){
			calForm.action="${pageContext.request.contextPath}/chuCal.action";
		}
		calForm.submit();
	}
</script>
</head>
<body>
	<form id="calForm" name="calForm" action="${pageContext.request.contextPath}/addCal.action">
		num1:<input type="text" name="num1"><br>
		num2:<input type="text" name="num2"><br>
		<button onclick="doSub(1)">+</button>
		<button onclick="doSub(2)">-</button>
		<button onclick="doSub(3)">*</button>
		<button onclick="doSub(4)">%</button>
		
	</form>
</body>
</html>

Action接口

package com.shl.framework;

import java.io.IOException;

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

/**
 * 子控制器
 *   专门用来处理业务逻辑的
 * @author Administrator
 *
 */
public interface Action {
	void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}



DispatcherServlet(主控制器)

package com.shl.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.shl.web.AddCalAction;
import com.shl.web.ChenCalAction;
import com.shl.web.ChuCalAction;
import com.shl.web.DelCalAction;

public class DispatcherServlet extends HttpServlet{

	private static final long serialVersionUID = -3419628442945899931L;
	private Map<String, Action> actionMap=new HashMap<>();
	
	public void init() {
		actionMap.put("/addCal", new AddCalAction());
		actionMap.put("/delCal", new DelCalAction());
		actionMap.put("/chenCal", new ChenCalAction());
		actionMap.put("/chuCal", new ChuCalAction());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		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);//调用子控制器处理请求
	}
}


AddCalAction(子控制器的子类,实现请求)

public class AddCalAction implements Action {
	/**
	 * 加
	 */
	@Override
	public void 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("calRes.jsp").forward(req, resp);
	}
}

/**
 * 减
 */
@Override
public void 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("calRes.jsp").forward(req, resp);
}


	/**
	 * 乘
	 */
	@Override
	public void 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("calRes.jsp").forward(req, resp);
	}


	/**
	 * 除
	 */
	@Override
	public void 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("calRes.jsp").forward(req, resp);
	}

原文链接:加载失败,请重新获取