什么是MVC?
MVC全名:Model View Controller,其中Model(模型层)、View(视图层)、Controller(控制层),
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离。
常用模式:
model1:jsp+jdbc
model2:mvc
高内聚,低耦合,各司其职。
三层架构和MVC的区别?
三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注自己擅长模块即可
MVC是一种设计模式,其目的是让html和业务逻辑分开
MVC结构
V(视图层) --》JSP/HTML/freemarker
C(控制层) --》Servlet/Action/Controller
M(模型层) --》entity、dao
entity:实体域模型(名词)
dao:过程域模型(动词)
注:
1)不能跨层调用;
2)只能由上往下进行调用;View -> Controller -> Model
自定义MVC工作原理:
MVC实现
案例1:
1)创建抽象类Action,定义抽象方法execute(处理具体逻辑)
package com.wangcaihua.mvc.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 控制器父类,完成具体的业务逻辑
* @author wangcaihua
*
* 2020年6月2日 下午2:06:21
*/
public abstract class Action {
public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
2)创建HelloAction并继承抽象类Action,重写execute方法
package com.wangcaihua.mvc.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangcaihua.mvc.framework.Action;
public class HelloAction extends Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("就当是一场梦");
return null;
}
}
3)在ActionServlet中定义私有Map<String,Action>(根据不同请求路径名调用不同逻辑处理Action类)
private Map<String, Action> map;
4)在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());
}
5)在ActionServlet中的doPost方法中处理请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求
String requestURI = req.getRequestURI();
System.out.println(requestURI);
System.out.println(req.getRequestURL());
int start=requestURI.lastIndexOf("/");
int end=requestURI.lastIndexOf(".action");
//截取请求路径中的 /mvc/*.action 的/*部分
String actionName = requestURI.substring(start,end);
//通过截取路径中的actionName找到对应的子控制器
Action action = map.get(actionName);//通过key,拿到值,等于new helloAction
//Action action= new HellowAction();
//执行execute方法
action.execute(req, resp);
}
案例2:
演示完成的MVC流程,form表单提交数据,进行逻辑处理后将结果返回页面显示
1)在ActionServlet中的init方法中添加map.put(’/AddAction’,new AddAction());
@Override
public void init() throws ServletException {
map =new HashMap<String, Action>();
map.put("/helloAction",new HelloAction());
}
2)创建AddAction继承抽象类Action,重写execute方式,处理逻辑后,返回结果
package com.wangcaihua.mvc.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangcaihua.mvc.framework.Action;
public class AddAction extends Action{
@Override
public 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("results.jsp").forward(req, resp);;
return null;
}
}
jsp页面:
<form action="addAction.action" method="post">
num1:<input name="num1"><br>
num2:<input name="num2"><br>
<input type="submit" value="+">
</form>
xml文件配置
<!--中央控制器 -->
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>com.zking.mvc.framework.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
效果演示: