1. What is the MVC
the MVC full name Model View Controller, the model (model) - view (view) - Abbreviation controller (Controller), and
it is a software design model, with a service logic, data, display interface method of separating an organization code
Model1 jsp+jdbc
Model2 ->MVC
The core idea: carry out their duties
2. MVC structure
V
JSP / iOS / Android
C
the servlet / Action
M
entity domain model (noun)
Process domain model (verb)
do distribute web browser requests a
service call processing dao project operations
dao database operations
Note 1: You can not cross-layer calls
Note 2: You can only call top-down appearance
3. FIG custom MVC works
main control sub-controller dynamically invoke specific service call completion logic
(trains, the console, the rail)
request, the main controller, the sub-controller
4. Schematic mvc custom
master controller:
package com.luoxiaogang.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.zhuyuncong.web.AddCalAction;
import com.zhuyuncong.web.CccCalAction;
import com.zhuyuncong.web.ChuCalAction;
import com.zhuyuncong.web.DelCalAction;
/**
* 中央控制器
* 作用:接收请求,通过请求寻找处理请求对应的子控制器
* @author MACHENIKE
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 9213618038567713003L;
private Map<String, Action> actionMap = new HashMap<>();
public void init() {
actionMap.put("/addCal", new AddCalAction());
actionMap.put("/delCal", new DelCalAction());
actionMap.put("/cccCal", new CccCalAction());
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);
}
}
Sub-controller:
package com.luoxiaogang.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来直接处理浏览器发送过来的请求
* @author MACHENIKE
*
*/
public interface Action {
String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException;
}
jsp page:
<%@ 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 mya(n) {
if(n == 1){
calForm.action="${pageContext.request.contextPath }/addCal.action";
}else if(n == 2){
calForm.action="${pageContext.request.contextPath }/delCal.action";
}else if(n == 3){
calForm.action="${pageContext.request.contextPath }/cccCal.action";
}else if(n == 4){
calForm.action="${pageContext.request.contextPath }/chuCal.action";
}
calForm.submit();
}
</script>
</head>
<body>
<form name="calForm" action="" method="post">
num1:<input type="text" name="num1"><br>
num2:<input type="text" name="num2"><br>
<input type="button" value="+" onclick="mya(1)">
<input type="button" value="-" onclick="mya(2)">
<input type="button" value="*" onclick="mya(3)">
<input type="button" value="/" onclick="mya(4)">
</form>
</body>
</html>
Adding processing of the sub-controller:
package com.luoxiaogang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuyuncong.framework.Action;
public class AddCalAction implements Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)+Integer.valueOf(num2));
req.getRequestDispatcher("res.jsp").forward(req, resp);;
return null;
}
}
The sub-controller processes the subtraction:
package com.luoxiaogang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuyuncong.framework.Action;
public class DelCalAction implements Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
req.getRequestDispatcher("res.jsp").forward(req, resp);;
return null;
}
}
The sub-controller processes the multiplication:
package com.luoxiaogang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuyuncong.framework.Action;
public class CccCalAction implements Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
req.getRequestDispatcher("res.jsp").forward(req, resp);;
return null;
}
}
The sub-division process controller:
package com.zhuyuncong.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuyuncong.framework.Action;
public class ChuCalAction implements Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
req.setAttribute("res", Float.parseFloat(num1)/Float.parseFloat(num2));
req.getRequestDispatcher("res.jsp").forward(req, resp);;
return null;
}
}
Summary:
The main controller: if there's a sub-controller to handle user requests, if you call the sub-controller to handle the request
is not on the error, it can not process the request
The sub-controller: is treated with a user request