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表单提交数据,进行逻辑处理
获得结果: