一、什么是MVC
mvc全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。他的核心思想就是各司其职,互不干扰。
使用自定义MVC和之前的MVC对比:
1、之前的MVC,BookServlet中的if语句判断非常多
2、自定义MVC可以省去jsp传递到后台封装成对象的过程
3、自定义MVC可以省去结果集的处理。
有几点要注意
注1:不能跨层调用
注2:只能出现由上而下的调用
为了更好的理解自定义MVC模式 所以用图解说
只要理解这幅图基本就了解了自定义MVC的运用了
对此我做了一个简单的Demo,首先我们来写一个实体类:
Cal 类
package com.wangshaoyang.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() {
super();
}
public Cal(String num1, String num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
}
然后我们就定义一个父类的总控制器 DispatcherServlet
package com.wangshaoyang.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.wangshaoyang.web.AddCalAction;
import com.wangshaoyang.web.ChenCalAction;
import com.wangshaoyang.web.ChuCalAction;
import com.wangshaoyang.web.DelCalAction;
public class DispatcherServlet extends HttpServlet{
private static final long serialVersionUID = 6979031270890161633L;
private Map<String, Action> actionMap=new HashMap<>();
public void init() {
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 {
init();
String url = req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));//获取请求的路径
Action action = actionMap.get(url);//获取请求对应的子控制器
action.execute(req, resp);//调用子控制器处理请求
}
}
定义一个子控制室,专门用来处理业务逻辑的:Action
package com.wangshaoyang.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 专门用来处理业务逻辑的
* @author Administrator
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
然后我们再写实现加减乘除的方法
加
package com.wangshaoyang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangshaoyang.framework.Action;
public class AddCalAction implements Action {
/**
* 加
*/
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
减
package com.wangshaoyang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangshaoyang.framework.Action;
public class DelCalAction implements Action {
/**
* 减
*/
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
乘
package com.wangshaoyang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangshaoyang.framework.Action;
public class ChenCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
除
package com.wangshaoyang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangshaoyang.framework.Action;
public class ChuCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
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);
}
}
最后我们写一个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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function doSub(value){
if(value==1){
calForm.action="${pageContext.request.contextPath}/addCal.action";
}
else if(value==2){
calForm.action="${pageContext.request.contextPath}/delCal.action";
}
else if(value==3){
calForm.action="${pageContext.request.contextPath}/chenCal.action";
}
else{
calForm.action="${pageContext.request.contextPath}/chuCal.action";
}
calForm.submit();
}
</script>
</head>
<body>
<form id="claForm" name="calForm" action="">
num1:<input type="text" name="num1" /><br/>
num2:<input type="text" name="num2" /><br/>
<input type="button" onclick="doSub(1)" value="+" />
<input type="button" onclick="doSub(2)" value="-" />
<input type="button" onclick="doSub(3)" value="x" />
<input type="button" onclick="doSub(4)" value="/" />
</form>
</body>
</html>
运行可得
点击除法就得到我们所需要的答案
今天就更新到这里
喜欢的可以关注我
不定时更新