自定义MVC1
自定义mvc
1.什么是MVC
2.MVC结构
3.案例
4.总结
1.什么是MVC
MVC: MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
2.MVC结构
M: 1. 实体域模型(名词)
2.过程域模型(动词)
V:做浏览器请求分发 jsp/ios/android
C:操作数据库 servlet/action
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 }/chengCal.action";
} else if(val == 4){//除法
calForm.action = "${pageContext.request.contextPath }/chuCal.action";
}
calForm.submit();
}
</script>
</head>
<body>
<form id="calForm" name="calFrom" action="${pageContext.request.contextPath }/addCal.action">
num1:<input type="text" name="num1"><br>
num2:<input type="text" name="num2"><br>
<!-- <input type="submit"> -->
<button onclick="doSub(1)">+</button>
<button onclick="doSub(2)">-</button>
<button onclick="doSub(3)">*</button>
<button onclick="doSub(4)">/</button>
</form>
</body>
</html>
Action接口
/**
* 子控制器
* 专门用来处理业务逻辑的
* @author shiyi
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ;
}
实体类
package com.dl.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();
}
}
主控制器(DispatcherServlet)
/**
-
主控制器
*/public class DispatcherServlet extends HttpServlet{
private static final long serialVersionUID = 6716422786850137569L; private Map<String,Action> actionMap = new HashMap<>(); //初始化方法 public void init() { //加法 actionMap.put("/addCal", new CalAction()); //减法 actionMap.put("/delCal", new CalAction()); //乘法 actionMap.put("/chengCal", new CalAction()); //除法 actionMap.put("/chuCal", new CalAction()); } @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 = new AddCalAction();
// class Student extends Person
// Person p = new Student();
Action action = actionMap.get(url);
req.setAttribute(“uri”, url);
action.execute(req, resp);
}
}
子控制器:控制请求(CalAction)
public class CalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
String uri = (String) req.getAttribute("uri");
if("/addCal".equals(uri)) {//加法
req.setAttribute("res", Integer.valueOf(num1)+ Integer.valueOf(num2));
}else if("/delCal".equals(uri)){//减法
req.setAttribute("res", Integer.valueOf(num1)- Integer.valueOf(num2));
}else if("/chengCal".equals(uri)){//乘法
req.setAttribute("res", Integer.valueOf(num1)* Integer.valueOf(num2));
}else if("/chuCal".equals(uri)){//除法
req.setAttribute("res", Integer.valueOf(num1)/ Integer.valueOf(num2));
}
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}