自定义mvc流程与概念

1.什么是MVC?

MVC全名:Model View Controller,其中Model(模型层)、View(视图层)、Controller(控制层)
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离,
两种常用模式:
model1:jsp+jdbc
model2:mvc

2.三层架构和MVC的区别?

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

3.MVC结构

V(视图层) --》JSP/HTML/freemarker
C(控制层) --》Servlet/Action/Controller
M(模型层) --》entity、dao
entity:实体域模型(名词)
dao:过程域模型(动词)

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

4.自定义MVC工作原理图

第一张个人理(仅供参考)
在这里插入图片描述
完整流程图
在这里插入图片描述

5.MVC小测试

中央控制器

package com.tanle.mvc.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.tanle.mvc.action.AsmdAction;
import com.tanle.mvc.action.HelloAction;

/**
 * 自定义mvc中的中央控制器
 * @author tanle
 *
 */
public class ActionServlet extends HttpServlet{
	
	private Map<String, Action> map;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		/**
		 * 初始化
		 */
		map=new HashMap<String, Action>();
		map.put("/helloAction", new HelloAction());
		map.put("/asmdAction", new AsmdAction());
	}
	
	
	@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 {
		// TODO Auto-generated method stub
		//1。获取请求路径
		///J2EE_MVC1/*.action
		String requestURI = req.getRequestURI();
		
		//找到最后一个斜杠
		int start=requestURI.lastIndexOf("/");
		//找到.action
		int end=requestURI.lastIndexOf(".action");
		
		//2.截取请求路径中的 J2EE_MVC1/*.action  /*部分
		String actionName = requestURI.substring(start, end);
		
		//3.通过截取路径中的actionName找到对应的子控制器
		//Action action = map.get(actionName);等同于  Action action = new Action();
		Action action = map.get(actionName);
		
		
		//4.执行execute方法
		action.execute(req, resp);
	}
}

控制器的子类

package com.tanle.mvc.framework;

import java.io.IOException;

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

/**
 * 控制器的父类  完成具体的业务逻辑
 * @author tanle
 *
 */
public abstract class Action{
	
	public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;

}

逻辑代码处理块

package com.tanle.mvc.action;

import java.io.IOException;

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

import com.tanle.mvc.framework.Action;

public class AsmdAction extends 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");
		
		String count= req.getParameter("count");
		int num3=0;
		if("+".equals(count)) {
			num3+=Integer.parseInt(num1)+Integer.parseInt(num2);
		}else if("-".equals(count)) {
			num3+=Integer.parseInt(num1)-Integer.parseInt(num2);
		}else if("*".equals(count)) {
			num3+=Integer.parseInt(num1)*Integer.parseInt(num2);
		}else if("/".equals(count)){
			num3+=Integer.parseInt(num1)/Integer.parseInt(num2);
		}
		
		req.setAttribute("num3", num3);
		req.getRequestDispatcher("close.jsp").forward(req, resp);
		return null;
	}

}

计算界面

<%@ 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>
	<form action="asmdAction.action" method="post">
		num1:<input type="text" name="num1" /><br/>
		num2:<input type="text" name="num2" /><br/>
		<input type="submit" value="+" name="count"/>
		<input type="submit" value="-" name="count"/>
		<input type="submit" value="*" name="count"/>
		<input type="submit" value="/" name="count"/>
	</form>
</body>
</html>

计算结果

<%@ 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>
<h2>计算结果:${num3 }</h2>
</body>
</html>

在这里插入图片描述

6.总结

理解性去学习这个自定义mvc,有什么不足的地方欢迎大家指正,谢谢观看.

猜你喜欢

转载自blog.csdn.net/Smootht/article/details/106498340