MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
V
jsp/ios/android
C
servlet/action
M
实体域模型(名词)
过程域模型(动词)
主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器
总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求
;没有就报错,就处理不了请求
子控制器:就是处理用户请求用的
首先我们得创建一个主控制器类
DispatcherSerlvet
package com.chenkang.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.chenkang.web.AddCalAtion;
import com.chenkang.web.ChengCalAtion;
import com.chenkang.web.ChuCalAtion;
import com.chenkang.web.DelCalAtion;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找处理的对应的自控器
* @author Administrator
*
*/
public class DispatcherSerlvet extends HttpServlet{
private static final long serialVersionUID = 1L;
private MapactionMap=new HashMap<>();
// 在configModel对象中包含了所有的子控制器信息
public void init() {
actionMap.put("/addCal", new AddCalAtion());
actionMap.put("/delCal", new DelCalAtion());
actionMap.put("/chengCal", new ChengCalAtion());
actionMap.put("/chuCal", new ChuCalAtion());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
init();
String url=req.getRequestURI();
// T226_mvc/addCal.action
// addCal
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
Action action = actionMap.get(url);
// Action action=(Action) Class.forName("com.chenkang.web.AddCalAtion").newInstance();
// Action action=(Action) new AddCalAtion();
action.execute(req, resp);
//
}
}
然后写一个子控制器接口类Action
package com.chenkang.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来直接处理浏览器发送过来的请求
* @author Administrator
*
*/
public interface Action {
String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
// TODO Auto-generated method stub
}
子控制器
我们用两个来举例
AddCalAtion
package com.chenkang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chenkang.entity.Cal;
import com.chenkang.framework.Action;
public class AddCalAtion 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");
Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
req.setAttribute("res", cal.getNum1()+cal.getNum2());
req.getRequestDispatcher("res.jsp").forward(req, resp);
return null;
}
}
DelCalAtion
package com.chenkang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chenkang.entity.Cal;
import com.chenkang.framework.Action;
public class DelCalAtion 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");
Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
req.getRequestDispatcher("res.jsp").forward(req, resp);
return null;
}
}
jsp的调用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here