1.什么是MVC
MVC: MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
核心思想:各司其职
2.MVC结构
M: 1. 实体域模型(名词)
2.过程域模型(动词)
V:做浏览器请求分发 jsp/ios/android
C:操作数据库 servlet/action
3.思维导图
4.案例
做一个加减乘除的案例
下面咋们就开始了
1.从实体类开始
package com.yinyi.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(String num1, String num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
public Cal() {
super();
}
}
2.定义主控制器
package com.yinyi.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.yinyi.web.AddCalAction;
import com.yinyi.web.DelCalAction2;
import com.yinyi.web.chengfaCalAction3;
import com.yinyi.web.chufaCalAction3;
/**
* 主控制器
* @author yinyi
*
*/
public class DispathcherServlet extends HttpServlet {
private static final long serialVersionUID = 1718695479595952099L;
private Map<String , Action> actionMap = new HashMap<>();
public void init() {
actionMap.put("/addCal", new AddCalAction());//加
actionMap.put("/delCal", new DelCalAction2());//减
actionMap.put("/chengfaCal", new chengfaCalAction3());//乘
actionMap.put("/chufalCal", new chufaCalAction3());//除
}
@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();//T224_mvc/xxx/action
url = url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
Action action = actionMap.get(url);
action.execute(req, resp);
}
}
3.定义子控制器
package com.yinyi.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 专门用来处理业务逻辑的
* @author yinyi
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
4.写加减乘除
1.加
@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.valueOf(num1)+Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
2.减
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
3.乘
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
4.除
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)/Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
5.发送请求
<%@ 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=ISO-8859-1">
<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 }/chengfaCal.action";
}
else{
calForm.action = "${pageContext.request.contextPath }/chufalCal.action";
}
calForm.submit();
}
</script>
</head>
<body>
<form id="calForm" name="calForm" action="${pageContext.request.contextPath }/addCal.action">
num1:<input type="text" name="num1"><br>
num2:<input type="text" name="num2"><br>
<button οnclick="dosub(1)">+</button>
<button οnclick="dosub(2)">-</button>
<button οnclick="dosub(3)">X</button>
<button οnclick="dosub(4)">/</button>
</form>
</body>
</html>
6.跳转页面
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
结果: ${res }
</body>
</html>
7.配置XML
<servlet>
<servlet-name>dispathcherServlet</servlet-name>
<servlet-class>com.yinyi.framework.DispathcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispathcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
5.总结
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求;
没有就报错,就处理不了请求
子控制器:就是处理用户请求用的