什么是mvc模式
1、MVC全名是 Model View Controller ,是模型(model)——视图(view)——控制器
(controller)的缩写,它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
2、MVC结构
M: 实体域模型(名词) 过程域模型(动词)
V :servlet/action
C: jsp/ios/android
web :做浏览器请求分发
service: 调用dao处理项目业务的
dao :操作数据库
注:不能跨层调用,只能出现由上而下的调用
3、自定义MVC工作原理
主控制动态调用子控制器调用完成具体的业务逻辑
请求、主控制器、子控制器
总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求;没有就报错,就处理不了请求
子控制器:就是处理用户请求用的
MVC模式案例
1、创建实体类
- 实体类含两个属性文本框。
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、创建子控制器类
- 专门用来出来业务逻辑的并且发送请求
//子控制器类
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
- 执行你所运行方法的类并且要实现Action类
//增加方法类
public class AddCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 =req.getParameter("num1");
String num2 =req.getParameter("num1");
req.setAttribute("res", Integer.valueOf(num1)+Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
//减去方法类
public class DelCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 =req.getParameter("num1");
String num2 =req.getParameter("num1");
req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
//想乘方法类
public class ChenCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 =req.getParameter("num1");
String num2 =req.getParameter("num1");
req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
//相除方法类
public class ChuCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1 =req.getParameter("num1");
String num2 =req.getParameter("num1");
req.setAttribute("res", Integer.valueOf(num1)/Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
3、创建主控制器的DispathcherServlet类
- 用来处理子控制器发出来的请求判断是否执行
//主控制器类
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 723689920988912828L;
private Map<String, Action> ActionMap =new HashMap<>();
@Override
public void init() throws ServletException {
ActionMap.put("/addCal", new AddCalAction());
ActionMap.put("/delCal", new DelCalAction());
ActionMap.put("/chenCal", new ChenCalAction());
ActionMap.put("/chuCal", new ChuCalAction());
}
@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 stubd
init();
String url = req.getRequestURI();
url =url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
Action action = ActionMap.get(url);
action.execute(req, resp);
}
}
4、运行效果
- 前台控制的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 doSb(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 }/chenCal.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="doSb(1)">+</button>
<button onclick="doSb(2)">-</button>
<button onclick="doSb(3)">*</button>
<button onclick="doSb(4)">/</button>
</form>
</body>
</html>
- 结果代码
<%@ 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 }<br>
<input type="button" onclick="location.href='cal.jsp' " value="返回" >
</body>
</html>
运行截图
- cal.jsp界面运行
- 加法
- 减法
- 乘法
- 除法