自定义mvc框架(1)
1. 什么是MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
Model1 jsp+jdbc
Model2 ->MVC
核心思想:各司其职
2. MVC结构
V
jsp/ios/android
C
servlet/action
M
实体域模型(名词)
过程域模型(动词)
3. 自定义MVC工作原理图
主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器
下面展示一些 内联代码片
接口Action
package com.lrc.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
void execute(HttpServletRequest req,HttpServletResponse resp) throws Exception;
}
实体类Cal
package com.lrc.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
package com.lrc.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.lrc.web.AddCalAction;
import com.lrc.web.CheCalAction;
import com.lrc.web.ChuCalAction;
import com.lrc.web.DelCalAction;
public class DispatcherServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Map<String, Action> map=new HashMap<String, Action>();
public void init() {
map.put("/addCal", new AddCalAction());
map.put("/delCal", new DelCalAction());
map.put("/cheCal", new CheCalAction());
map.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=new AddCalAction
Action action=map.get(url);//这行代码就相当于上面的那行
try {
action.execute(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>mvc</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.lrc.framework.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
AddCalAction(加法)实现接口(Action)
package com.lrc.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lrc.framework.Action;
public class AddCalAction implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
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);
}
}
DelCalAction(减法)实现接口(Action)
package com.lrc.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lrc.framework.Action;
public class DelCalAction implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
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);
}
}
ChrCalAction(乘法)实现接口(Action)
package com.lrc.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lrc.framework.Action;
public class CheCalAction implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
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);
}
}
chuCalAction(除法)实现接口(Action)
package com.lrc.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lrc.framework.Action;
public class ChuCalAction implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
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);
}
}
calRes.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>
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 }/cheCal.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">
num1<input type="text" name="num1"><br>
num2<input type="text" 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>
代码效果显示如下
加法
减法
乘法
除法
总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求;没有就报错,就处理不了请求
子控制器:就是处理用户请求用的