什么是MVC?
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码。
核心思想:各司其职
MVC结构
M:jsp/ios/android
V: servlet/action
C: 实体域模型(名词) 过程域模型(动词)
注1:不能跨层调用
注2:只能出现由上而下的调用
自定义MVC工作原理图
主控制动态调用子控制器调用完成具体的业务逻辑
用加减乘除案例来初识自定义MVC:
实体类:Cal.java:
package com.liyi.entity;
public class Cal {
private String num1;
private String num2;
public String getNum1() {
return num1;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public String getNum2() {
return num2;
}
public void setNum2(String num2) {
this.num2 = num2;
}
public Cal() {
super();
}
public Cal(String num1, String num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
}
主控制器:DispatcherServlet.java :
package com.liyi.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.liyi.web.AddCalAction;
import com.liyi.web.ChenCalAction;
import com.liyi.web.ChuCalAction;
import com.liyi.web.DelAction;
/**
* 主控制器
* @author 李毅
*
*/
public class DispatcherServlet extends HttpServlet{
private static final long serialVersionUID = -7500451840558220628L;
private Map<String, Action> actionMap = new HashMap<>();
public void init() {
actionMap.put("/addCal", new AddCalAction());
actionMap.put("/delCal", new DelAction());
actionMap.put("/chenCal", new ChenCalAction());
actionMap.put("/chuCal", new ChuCalAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
子控制器 :Action.java
package com.liyi.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 专门用来处理业务逻辑的
* @author 李毅
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
实现子控制器接口的AddCalAction.java
package com.liyi.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liyi.framework.Action;
public class AddCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.parseInt(num1)+Integer.parseInt(num2));
req.getRequestDispatcher("claRes.jsp").forward(req, resp);
}
}
实现子控制器接口的DelAction.java
package com.liyi.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liyi.framework.Action;
public class DelAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.parseInt(num1)-Integer.parseInt(num2));
req.getRequestDispatcher("claRes.jsp").forward(req, resp);
}
}
实现子控制器接口的ChenCalAction.java
package com.liyi.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liyi.framework.Action;
public class ChenCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.parseInt(num1)*Integer.parseInt(num2));
req.getRequestDispatcher("claRes.jsp").forward(req, resp);
}
}
实现子控制器接口的ChuCalAction.java
package com.liyi.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liyi.framework.Action;
public class ChuCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.parseInt(num1)/Integer.parseInt(num2));
req.getRequestDispatcher("claRes.jsp").forward(req, resp);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" id="WebApp_ID" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>liyi_mvc</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.liyi.framework.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
cal.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 " method="post">
num1:<input name="num1"><br>
num2:<input 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>
界面如下:
结果界面claRes.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>
</head>
<body>
结果:${res }
</body>
</html>
结果如下:
总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求;没有就报错,就处理不了请求。
子控制器:就是处理用户请求用的
好啦,今天的更新就到这里,喜欢点赞+转发~