MVC框架自定义实现过程
短信预约 -IT技能 免费直播动态提醒
1、思维导图
2、什么是MVC?
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码。
3、运行原理
用户发送请求 → 中央控制器接受用户请求 → 分析请求连接/获取到用户需要的类+方法 → 调用相对应的Model → 访问数据库服务器
4、演绎过程
4.1.控制层
BookServlet:
package com.tyf.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;
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 name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DispatchServlet:
package com.tyf.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.tyf.web.BookAction;
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//存放子控制器的容器
private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
//初始化子控制器容器(集合),经过初始化,action容器内部就有了子控制器
//init(初始化方法),service(服务),destroy(销毁)
@Override
public void init() throws ServletException {
actions.put("/book", 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/J2ee12/book.action?name=add
//目标:BookAction.add()...
String url = req.getRequestURI();
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(url);
action.execute(req, resp);
}
}
4.2.模型层
ActionSupport:
package com.tyf.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 name = req.getParameter("name");
try {
Method m = this.getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DispatchServlet:
package com.tyf.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.tyf.web.BookAction;
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
//存放子控制器的容器
private Map<String , ActionSupport> actions = new HashMap<String , ActionSupport>();
//初始化子控制器容器(集合),经过初始化,action容器内部就有了子控制器
//init(初始化方法),service(服务),destroy(销毁)
@Override
public void init() throws ServletException {
actions.put("/book", 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/J2ee12/book.action?name=add
//目标:BookAction.add()...
String url = req.getRequestURI();
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(url);
action.execute(req, resp);
}
}
4.3视图层
<%@ 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>
目前多数人增删改查的代码:
<a href="${pageContext.request.contextPath}/book/add" rel="external nofollow" >增加</a>
<a href="${pageContext.request.contextPath}/book/delete" rel="external nofollow" >删除</a>
<a href="${pageContext.request.contextPath}/book/edit" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath}/book/list" rel="external nofollow" >查询</a>
<hr>
增删改查的代码2.0
<a href="${pageContext.request.contextPath}/book.action?name=add" rel="external nofollow" >增加</a>
<a href="${pageContext.request.contextPath}/book.action?name=delete" rel="external nofollow" >删除</a>
<a href="${pageContext.request.contextPath}/book.action?name=edit" rel="external nofollow" >修改</a>
<a href="${pageContext.request.contextPath}/book.action?name=list" rel="external nofollow" >查询</a>
<hr>
增删改查的代码3.0
<a href="${pageContext.request.contextPath}/book.action?name=load" rel="external nofollow" >回显</a>
<a href="${pageContext.request.contextPath}/book.action?name=ref" rel="external nofollow" >关联</a>
</body>
</html>
5、运行结果
以上就是MVC框架自定义实现过程的详细内容,更多关于MVC框架的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341