目录
一、什么是MVC?
MVC(Model View Controller)是一种软件设计的框架模式,它采用模型(Model)-视图(View)-控制器(controller)的方法把业务逻辑、数据与界面显示分离。把众多的业务逻辑聚集到一个部件里面,当然这种比较官方的解释是不能让我们足够清晰的理解什么是MVC的。用通俗的话来讲,MVC的理念就是把数据处理、数据展示(界面)和程序/用户的交互三者分离开的一种编程模式
二、mvc的作用
MVC 属架构于模式的一种,所谓架构就是如何设计一个程序的结构。MVC 将程序结构划分为三层,每一层都对外提供了可供上层调用的接口,既能维系三层之间的联系,也能保持相对的独立性
三、最初的增删改查
最初版的增删改查每一个功能对应了一个servlet。
package com.dgl.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/add")
public class AddBookServlet extends HttpServlet{
@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 {
System.out.println("处理书籍的增加业务,调用BookBiz");
}
package com.dgl.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/del")
public class DelBookServlet extends HttpServlet{
@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 {
System.out.println("处理书籍的删除业务,调用BookBiz");
}
package com.dgl.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/edit")
public class EditBookServlet extends HttpServlet{
@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 {
System.out.println("处理书籍的编辑业务,调用BookBiz");
}
}
package com.dgl.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book/list")
public class ListBookServlet extends HttpServlet{
@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 {
System.out.println("处理书籍的查询业务,调用BookBiz");
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>目前的增删改查</h3>
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/del">删除</a>
<a href="${pageContext.request.contextPath }/book/update">修改</a>
<a href="${pageContext.request.contextPath }/book/select">查看</a>
</body>
</html>
对于每一个功能都需要一个单独的servelt我们可以进行优化。
<%@ 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>
<h3>类数量过多问题的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
</body>
</html
package com.dgl.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book.action")
public class BookServlet extends HttpServlet{
@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 {
// 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
String methodName = req.getParameter("methodName");
if("add".equals(methodName)){
// 如果前台传递到后台的是一个新增的请求,那么后台就调用新增方法
add(req,resp);
}
else if("del".equals(methodName)) {
del(req,resp);
}
else if("edit".equals(methodName)) {
edit(req,resp);
}
else if("list".equals(methodName)) {
list(req,resp);
}
// else if("load".equals(methodName)) {
// load(req,resp);
// }
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
// private void load(HttpServletRequest req, HttpServletResponse resp) {
// System.out.println("在用一个servlet中调用 list 方法");
// }
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
四、反射版增删查改
package com.dgl.web;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/book.action")
public class BookServlet extends HttpServlet{
@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 {
// 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
String methodName = req.getParameter("methodName");
// methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
// 前台传递什么方法,就调用当前类的对应方法
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// 调用当前类实例的methodName方法
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
<%@ 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>
<h3>类数量过多问题的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
</body>
</html>
五、实现
首先看一下原理图:
思路如下:
先建立中央控制器也就是ActionServlet
对应处理人就是Action
ActionSupport继承于Action
BooKAction实现ActionSupport
package com.dgl.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dgl.web.BooKAction;
/**
* 中央控制器:
* 主要职能:接受浏览器请求,找到对应的处理人
*
* @author Administrator
*
*/
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
private Map<String, Action> actions=new HashMap<String, Action>();
// 程序启动时,只会加载一次
@Override
public void init() throws ServletException {
actions.put("/book", new BooKAction());
// actions.put("/order", new BooKAction());
}
@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 {
//http://localhost:8080/mvc/book.aciton?methodName=list
String uri = req.getRequestURI();
// 要拿到/book,就是最后一个/到最后一个点的位置
uri=uri.substring(uri.lastIndexOf("/")
, uri.lastIndexOf("."));
Action action = actions.get(uri);
System.out.println(action);
action.execute(req, resp);
}
}
package com.dgl.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器:
* 对应请求的处理人
* @author Administrator
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp);
}
package com.dgl.framework;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action{
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) {
String methodName = req.getParameter("methodName");
// methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
// 前台传递什么方法,就调用当前类的对应方法
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// 调用当前类实例的methodName方法
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void list(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void load(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 list 方法");
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 edit 方法");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 del 方法");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在用一个servlet中调用 add 方法");
}
}