一、什么是MVC?
MVC的全名:Model View Controller,其中Model是(模型层)、View是(视图层)、Controller是(控制层);是一种使用“模型-视图-控制器”设计创建Web应用程序的模式,同时提供了对HTML、CSS和JavaScript的完全控制;它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离。
● Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。通常负责在数据库中存取数据。
● View(视图)是应用程序中处理数据显示的部分。通常是依据模型数据创建的。
● Controller(控制器)是应用程序中处理用户交互的部分。通常负责从视图读取数据,控制用户输入,并向模型发送数据。
2、为什么要使用MVC?
主要目的是将 Model 和 View 的实现代码分离,从而使同一个程序可以使用不同的表现形式。
3、三层架构和MVC的区别?
三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注自己擅长模块即可;
MVC是一种设计模式,其目的是让html和业务逻辑分开。
4、MVC结构?
V(视图层--View)
C(控制层--Controller)
M(模型层--Model)
注:1)不能跨层调用;
2)只能由上往下进行调用;View -> Controller -> Model
5、自定义MVC工作原理图

*.action 调度 截取*(请求路径名) 处理具体业务逻辑
JSP -----------> Servlet(中央控制器)--------------------->Action(子控制器)--->Model(Dao、DB)
6、MVC实现
案例1:
1)创建抽象类Action,定义抽象方法execute(处理具体逻辑)
- package com.zking.mvc.framework;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * 子控制类,做具体的核心业务逻辑处理
- * @author gss
- *
- */
- public abstract class Action {
- public abstract String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException;
- }
2)创建HelloAction并继承抽象类Action(子类继承父类),重写execute方法
- package com.zking.mvc.action;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.zking.mvc.framework.Action;
-
- public class HelloAction extends Action {
- @Override
- public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("Hello,World!自定义MVC");
- return null;
- }
- }
3)在ActionServlet中定义私有Map
(根据不同请求路径名调用不同逻辑处理Action类) 4)在ActionServlet中的init方法初始化Map集合
action.put('请求路径','逻辑处理Action类')
5)在ActionServlet中的doPost方法中处理请求6) 配置web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mvcdisplay-name> <servlet> <servlet-name>actionServletservlet-name> <servlet-class>com.zking.mvc.framework.ActionServletservlet-class> <init-param> <param-name>configparam-name> <param-value>/config.xmlparam-value> init-param> servlet> <servlet-mapping> <servlet-name>actionServletservlet-name> <url-pattern>*.actionurl-pattern> servlet-mapping> web-app>
- package com.zking.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 org.apache.commons.beanutils.BeanUtils;
- import com.zking.mvc.action.AddAction;
- import com.zking.mvc.action.HelloAction;
- import com.zking.mvc.entity.ActionModel;
- import com.zking.mvc.entity.ConfigModel;
- import com.zking.mvc.entity.ForwardModel;
- import com.zking.mvc.util.ConfigModelFactory;
- /**
- * ActionServlet:中央控制器,用于接受请求,分发请求,不做具体的核心业务逻辑处理
- * @author gss
- *
- */
- public class ActionServlet extends HttpServlet {
- //定义全局变量
- //该集合用于定义请求路径与子控制器类Action之间的绑定关系
- //例如:/hellAction ---> HellAction extends Action
- //String代表请求路径名,也就是*(包含/)
- //Action代表请求路径所对应的子控制器类Action
- private Map
actions = new HashMap<>(); -
- @Override
- public void init() throws ServletException {
- super.init();
- //初始化
- actions.put("/helloAction", new HelloAction());
- try {
- String path = this.getInitParameter("config");
- configModel= ConfigModelFactory.createConfigModel(path);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doPost(req, resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.获取请求路径*.action
- String requestURL = req.getServletPath();
- //2.获取请求路径名 * 例如:/hellAction.action ---> /hellAction
- int start = requestURL.indexOf("/");
- int end = requestURL.lastIndexOf(".");
- //截取
- String requestName = requestURL.substring(start, end);
- //3.根据请求路径名获取对应的子控制器类Action
- Action action = actions.get(requestName);
- //4.将请求委托给具体的子看着器类Action去处理
- action.execute(req, resp);
- }
- }
8) 启动Tomcat,运行helloAction.action
案例2:演示完成的MVC流程,form表单提交数据,进行逻辑处理后将结果返回页面显示
1)在ActionServlet中的init方法中添加
action.put('/AddAction',new AddAction());
2)创建AddAction继承抽象类Action,重写execute方法,处理逻辑后,返回结果,同时创建一个相加页面和结果页面
add.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- 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 heretitle>
- head>
- <body>
- <form action="addAction.action" method="post">
- num1:<input type="text" name="num1"/><br/>
- num2:<input type="text" name="num2"/><br/>
- <input type="submit" value="+"/>
- form>
- body>
- html>
AddAction.java
- package com.zking.mvc.action;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.zking.mvc.framework.Action;
-
- public class AddAction extends Action {
- @Override
- public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- Integer num1 = Integer.parseInt(req.getParameter("num1"));
- Integer num2 = Integer.parseInt(req.getParameter("num2"));
- int rs = num1+num2;
- //将结果存储到request作用域中
- req.setAttribute("rs", rs);
- //跳转路径
- req.getRequestDispatcher("/rs.jsp").forward(req, resp);
- }
- }
rs.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- 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 heretitle>
- head>
- <body>
- 结果:${rs }
- body>
- html>