思维导图
?自定义mvc的框架? 可以根据这个图操作
什么是mvc?
? ? index.jsp界面
? ? <a href="${pageContext.request.contextPath}/book/add">新增</a> ?? ?<a href="${pageContext.request.contextPath}/book/delete">删除</a> ?? ?<a href="${pageContext.request.contextPath}/book/edit">修改</a> ?? ?<a href="${pageContext.request.contextPath}/book/list">查询</a>
package com.hmf.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;
/** ?* mvc:模型层(model)? ?* 视图层:(view)? ?* 控制图(controller) ?* ?* ?*自定义mvc: ?*?? ?1.数据库处理Dao ? ?*?? ?2.servlet? ?* ? ? doget/dopost ?基本上用不上doget ?* ? ? 实体类参数接受代码(req.getparammeter(“”)) ?* ? ? req.getdispather("/index.jsp").forward(req,resp); ?* ? ? resp.sendredirect("/index.jsp"); ?*? ?3.jsp ?* ? ?* ?框架:反射+设计模式(极大的减少了代码量) ?* ? ? 1.通用分页指点+通用的增删改 ?* ? ? 2.各层(mc)数据Dao层 控制层代码缩减 ?* ? ? 3.前台代码的代码缩减 ?*/ @WebServlet("/book.action") public class BookServlet extends HttpServlet{ ?? ?protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ?? ??? ?doPost(req, resp); ?? ?}
}
?运行结果
?自定义mvc原理
少数人采用这种增删改查(servlet层) ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=add">新增</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=delete">删除</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=edit">修改</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=list">查询</a>
package com.hmf.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;
/** ?* mvc:模型层(model)? ?* 视图层:(view)? ?* 控制图(controller) ?* ?* ?*自定义mvc: ?*?? ?1.数据库处理Dao ? ?*?? ?2.servlet? ?* ? ? doget/dopost ?基本上用不上doget ?* ? ? 实体类参数接受代码(req.getparammeter(“”)) ?* ? ? req.getdispather("/index.jsp").forward(req,resp); ?* ? ? resp.sendredirect("/index.jsp"); ?* ?3.jsp ?* ? ?* ?框架:反射+设计模式(极大的减少了代码量) ?* ? ? 1.通用分页指点+通用的增删改 ?* ? ? 2.各层(mc)数据Dao层 控制层代码缩减 ?* ? ? 3.前台代码的代码缩减 ?*/ @WebServlet("/book.action") public class BookServlet extends HttpServlet{ ?? ?protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ?? ??? ?doPost(req, resp); ?? ?} ?? ? ?? ? ?? ?protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
?? ??? ? * 优点:代码减少 ?有之前的四个类变成一个类 ?? ??? ? * 缺点:每一次新增一个方法,都要改变原有的逻辑
? ? ? ? ? ? ? ? 一般修改时都需要数据回显load
<a href="${pageContext.request.contextPath}/book.action?methodName=load">回显</a>
?? ??? ?String methodName = req.getParameter("methodName"); ?? ??? ?if("add".equals(methodName)) { ?? ??? ??? ?add(req,resp); ?? ??? ?}else if("edit".equals(methodName)) { ?? ??? ??? ?edit(req,resp); ?? ??? ?}else if("delete".equals(methodName)) { ?? ??? ??? ?delete(req,resp); ?? ??? ?}else if("list".equals(methodName)) { ?? ??? ??? ?list(req,resp); ?? ??? ?} ?? ?}
?? ?private void delete(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.delete()..."); ?? ?}
?? ?private void list(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.list()..."); ?? ?}
?? ?private void edit(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.edit()..."); ?? ?}
?? ?private void add(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.add()..."); ?? ?} } ?
?
package com.hmf.web;
import java.io.IOException; import java.lang.reflect.InvocationTargetException; 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;
/** ?* mvc:模型层(model)? ?* 视图层:(view)? ?* 控制图(controller) ?* ?* ?*自定义mvc: ?*?? ?1.数据库处理Dao ? ?*?? ?2.servlet? ?* ? ? doget/dopost ?基本上用不上doget ?* ? ? 实体类参数接受代码(req.getparammeter(“”)) ?* ? ? req.getdispather("/index.jsp").forward(req,resp); ?* ? ? resp.sendredirect("/index.jsp"); ?* ?3.jsp ?* ? ?* ?框架:反射+设计模式(极大的减少了代码量) ?* ? ? 1.通用分页指点+通用的增删改 ?* ? ? 2.各层(mc)数据Dao层 控制层代码缩减 ?* ? ? 3.前台代码的代码缩减 ?*/ @WebServlet("/book.action") public class BookServlet extends HttpServlet{ ?? ?protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ?? ??? ?doPost(req, resp); ?? ?} ?? ? ?? ? ?? ?protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ?? ??? ?/** ?? ??? ? * 优点:代码减少 ?有之前的四个类变成一个类 ?? ??? ? *? ?? ??? ? * 缺点:每一次新增一个方法,都要改变原有的逻辑
?? ??? ? * 结论: ?? ??? ? * ?? ?反射可以修复上面改动代码才能解决需求问题的缺陷 ?? ??? ? * ?? ?反射代码 ?相当于中央控制器 ?并不是直接处理浏览请求 ?? ??? ? * ?? ??? ??? ?处理浏览请求的是子控制器 ?? ??? ? */ ?? ??? ?String methodName = req.getParameter("methodName"); ?? ??? ?try { ?? ??? ??? ?Method m= this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); ?? ??? ??? ?m.setAccessible(true); ?? ??? ??? ?m.invoke(this,req, resp); ?? ??? ?} catch (Exception e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ? ?? ? //?? ??? ?if("add".equals(methodName)) { //?? ??? ??? ?add(req,resp); //?? ??? ?}else if("edit".equals(methodName)) { //?? ??? ??? ?edit(req,resp); //?? ??? ?}else if("delete".equals(methodName)) { //?? ??? ??? ?delete(req,resp); //?? ??? ?}else if("list".equals(methodName)) { //?? ??? ??? ?list(req,resp); //?? ??? ?}else if("load".equals(methodName)) { //?? ??? ??? ?load(req,resp); //?? ??? ?} ?? ?}
?? ?private void load(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.load()..."); ?? ?}
?? ?private void delete(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.delete()..."); ?? ?}
?? ?private void list(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.list()..."); ?? ?}
?? ?private void edit(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.edit()..."); ?? ?}
?? ?private void add(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.add()..."); ?? ?} ?? ? ?? ?private void ref(HttpServletRequest req, HttpServletResponse resp) { ?? ??? ?System.out.println("bookDao.ref()..."); ?? ?} } ?
?<%@ 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> 大多人数开发增删改查 (servlet层) ?? ?<a href="${pageContext.request.contextPath}/book/add">新增</a> ?? ?<a href="${pageContext.request.contextPath}/book/delete">删除</a> ?? ?<a href="${pageContext.request.contextPath}/book/edit">修改</a> ?? ?<a href="${pageContext.request.contextPath}/book/list">查询</a> <hr> 少数人采用这种增删改查(servlet层) ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=add">新增</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=delete">删除</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=edit">修改</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=list">查询</a> <hr> 反射优化 ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=load">回显</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=ref">关联查询</a> ?? ?<a href="${pageContext.request.contextPath}/book.action?methodName=other">其他查询</a> </body> </html>
结束啦 好累? 拜拜 下期再见
|