思维导图
?一、MVC
介绍
MVC:全名ModelViewController模型层 ?(Model) 视图层(view) ?控制层(controller)
演绎过程
1、一个方法建一个servlet
缺点:重复代码过多,费时费力
如下:
?
?2、一个servlet调用所有方法
优点:相较于前一种代码减少,有多个类减少到一个
缺点:每一次新增一个方法都需要改动原来的逻辑,是代码过于冗余
代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("methodName");
if("add".equals(method)) {
add(request,response);
}else if("edit".equals(method)) {
edit(request,response);
}else if("del".equals(method)) {
del(request,response);
}else if("list".equals(method)) {
list(request,response);
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("listbookdao.......");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delbookdao.......");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("editbookdao.......");
}
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("addbookdao.......");
}
}
方法调用:
<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>
3、反射优化
思考:不动原有逻辑也能实现需求 ? ? ? 解决方案: ? ? ? 调用哪一个方法实际上取决于methodname,if是不必要条件 ? ? ? 动态调用methodname方法,并且是当前类实例的methodname方法
总结: ? ? ? 反射可以修复上面改动代码才能解决需求问题的缺陷 ? ? ? 反射这段代码,相当于中央控制器,并不直接处理浏览器请求 ? ? ? 处理浏览器请求数子控制台
代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("methodName");
try {
Method m = this.getClass().getDeclaredMethod(method,HttpServletRequest.class, HttpServletResponse);
m.setAccessible(true);
m.invoke(this,request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("listbookdao.......");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delbookdao.......");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("editbookdao.......");
}
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("addbookdao.......");
}
}
测试:
?二、自定义MVC框架
1、思路及分析
中央控制器 ? ? 子控制器 ? ? ? 将自定义mvc框架原理形成代码,形成框架 ?? ? ? ? ActionServlet-->DispatchServlet(Springmvc) ?? ? ?why(思考): ?? ? ? ? ?1.BookServlet中要做增删改查,那么必须要在doPost方法中写反射动态调用新增方法 ? ? ? ? ? ?GoodsServlet要做增删改查,那么必须要在doPost方法中写反射动态调用新增方法 ? ? ? ? ? ? 结论:反射动态调用新增方法代码是重复的的,但是这段又是必须的,也就是还需要进一步优化 ? ? ? ? ? ? ? 优化上次所说的中央控制器 ? ? ? ? ?2.对于BookServlet中要做增删改查而言,依然是每一个Servlet都要写doget/doPost方法 ? ? ? ? ? ? ? 但是实际上对处理业务有用的代码,往往是我们新加的,只需关注业务(add/delete/...) ? ? ? ? ? ? 优化上次所说的子控制器 ? ? ? ? 3.解决遗留的两个问题 ? ? ? ? ? ? ? 实体类参数接受代码冗余(req.getParammeter(""),尤其当实体类属性多的情况,封装到实体类中) ? ? ? ? ? ? ? ? ?req.getParammeter("bid")/req.getParammeter("bname")/req.getParammeter("price")... ? ? ? ? ? ? 关于结果页面的跳转(转发、重定向) ? ? ? ? ? ? ? ? ?req.getdispather("/index.jsp").forward(req,resp); ? ? ? ? ? ? ? ? resp.sendredirect("/index.jsp");
2、MVC工作原理图
?2、优化中央控制器及子控制器(利用XML建模(具体参考以前笔记))
1、用到的包及jar包
用到的JSP界面?
?中央控制器? DispathServlet:
package com.DHM.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 org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 中央控制器
* Servlet implementation class DispatchServlet
*/
//以xxx.action结尾就调入
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
// 在当前中央控制器中必然会有所有的子控制器集合
// 缺陷:如果有商品的增删改查-->意味着要改动代码-->代码的设计不够灵活
// 思考:在不改动代码的情况下,中央控制器也能找到对应的子控制器去处理浏览器请求
// 方案:我把加子控制器的逻辑/动作,放到配置文件中完成(Dbutil改连接信息是放在代码中完成/现在是放在Properties文件中完成)
// 放在配置文件中完成的好处在于:代码更加灵活,修改相关信息不用动代码了
// private Map<String, ActionSupport> actions = new HashMap<>();
// configModel对象又通过建模的知识,把所有的配置信息给读取过来了
private configModel configModel=null;
private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
/**
* 初始化所有子控制器到当前中央控制器中
*/
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
// actions.put("/book",new BookAction());
try {
configModel=configModelFactory.build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把子控制器与浏览器请求关联起来,"寻找"能够处理请求的子控制器
// http://localhost:8080/book.action?methodName=add-->BookAction.add();
/*
* 思路
* 1.uri-->/book
* 2.通过/book字符串在actions找到BookAction
* 3.调用BookAction的add,想要调用add,实际上只要统一调用execute就可以了
*/
// 获取到浏览器的请求地址
String uri = request.getRequestURI();
// uri-->/books
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
// 通过/book字符串在actions找到BookAction
// ActionSupport actrion = actions.get(url);
// 在Map中寻找子控制器-->在配置文件中寻找子控制器
/*
* 1.通过/book找到对应的ActionModel对象
* 2.通过ActionModel对象拿到类的全路径名com.DHM.foramework.BookAction
* 3.反射实例化对象
*/
ActionModel actionModel=configModel.pop(uri);
// 类的全路径名
String type = actionModel.getType();
ActionSupport action=null;
try {
action=(ActionSupport) Class.forName(type).newInstance();
action.execute(request,response);
// 完成实体类参数封装
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- 在这里每加一个配置,就相当于actions.put("/goods", new GoodsAction());
这样就解决了代码灵活性的问题
-->
<action type="com.DHM.servlet.BookAction" path="/book">
<forward path="/bookList.jsp" redirect="false" name="list"/>
<forward path="/bookEdit.jsp" redirect="true" name="toEdit"/>
</action>
<action type="com.DHM.servlet.GoodsAction" path="/goods">
<forward path="/login.jsp" redirect="false" name="failed"/>
<forward path="/main.jsp" redirect="true" name="success"/>
</action>
<action type="com.DHM.servlet.OrderAction" path="/order">
<forward path="/login.jsp" redirect="false" name="failed"/>
<forward path="/main.jsp" redirect="true" name="success"/>
</action>
</config>
子控制器(Action 接口)
package com.DHM.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 处理浏览器请求
* 针对于add/ref/other进行向上抽取、抽象
*
* Servlet implementation class Action
*/
public interface Action {
//这一个方法就是add/ref/other进行向上抽取的抽象方法
public String execute(HttpServletRequest req,HttpServletResponse resp);
// private void list(HttpServletRequest request, HttpServletResponse response) {
// System.out.println("listbookdao.......");
//
// }
//
// private void del(HttpServletRequest request, HttpServletResponse response) {
// System.out.println("delbookdao.......");
//
// }
//
// private void edit(HttpServletRequest request, HttpServletResponse response) {
// System.out.println("editbookdao.......");
//
// }
}
ActionSupport(实现Action)
package com.DHM.framework;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 作用:能够处理浏览器的"所有"请求。包括add/ref/other
* @author T440s
*
*/
public class ActionSupport implements Action{
private static final Class<?> HttpServletResponse = null;
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) {
String method=req.getParameter("methodName");
String res = null;
try {
Method m = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
m.setAccessible(true);
res = (String) m.invoke(this, req, resp);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
BookAction(继承ActionSupport)
package com.DHM.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.DHM.entity.Book;
public class BookAction extends ActionSupport implements ModelDriver{
// 从父类继承了execute方法,就把反射动态调用的方法继承过来
// 当前自动控制器在哪里调用?把子控制器与浏览器请求关联起来
public Book book=new Book();
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("listbookdao.......");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delbookdao.......");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("editbookdao.......");
}
private String add(HttpServletRequest request, HttpServletResponse response) {
// book.setBid(req.getParameter("bid"));
// book.setBname(req.getParameter("bname"));
// book.setPrice(req.getParameter("price"));
// book.setAthor(req.getParameter("athor"));
// book.setPublish(req.getParameter("publish"));
System.out.println(book);
System.out.println("bookDao.add(book)...");
return "list";
}
@Override
public Object getModel() {
// TODO Auto-generated method stub
return null;
}
}
中央控制器及子控制器优化 <a href="${pageContext.request.contextPath }/book.action?methodName=add">新增</a> <a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a> <a href="${pageContext.request.contextPath }/book.action?methodName=delete">删除</a> <hr>
3、建立模型驱动接口(实体类参数接受代码冗余)
Book实体 类
package com.DHM.entity;
public class Book {
public String bid;
public String bname;
public String price;
public String athor;
public String publish;
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAthor() {
return athor;
}
public void setAthor(String athor) {
this.athor = athor;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + ", athor=" + athor + ", publish="
+ publish + "]";
}
}
DispathServlet?中央控制器
package com.DHM.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 org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 中央控制器
* Servlet implementation class DispatchServlet
*/
//以xxx.action结尾就调入
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet {
// 在当前中央控制器中必然会有所有的子控制器集合
// 缺陷:如果有商品的增删改查-->意味着要改动代码-->代码的设计不够灵活
// 思考:在不改动代码的情况下,中央控制器也能找到对应的子控制器去处理浏览器请求
// 方案:我把加子控制器的逻辑/动作,放到配置文件中完成(Dbutil改连接信息是放在代码中完成/现在是放在Properties文件中完成)
// 放在配置文件中完成的好处在于:代码更加灵活,修改相关信息不用动代码了
// private Map<String, ActionSupport> actions = new HashMap<>();
// configModel对象又通过建模的知识,把所有的配置信息给读取过来了
private configModel configModel=null;
private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
/**
* 初始化所有子控制器到当前中央控制器中
*/
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
// actions.put("/book",new BookAction());
try {
configModel=configModelFactory.build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 把子控制器与浏览器请求关联起来,"寻找"能够处理请求的子控制器
// http://localhost:8080/book.action?methodName=add-->BookAction.add();
/*
* 思路
* 1.uri-->/book
* 2.通过/book字符串在actions找到BookAction
* 3.调用BookAction的add,想要调用add,实际上只要统一调用execute就可以了
*/
// 获取到浏览器的请求地址
String uri = request.getRequestURI();
// uri-->/books
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
// 通过/book字符串在actions找到BookAction
// ActionSupport actrion = actions.get(url);
// 在Map中寻找子控制器-->在配置文件中寻找子控制器
/*
* 1.通过/book找到对应的ActionModel对象
* 2.通过ActionModel对象拿到类的全路径名com.DHM.foramework.BookAction
* 3.反射实例化对象
*/
ActionModel actionModel=configModel.pop(uri);
// 类的全路径名
String type = actionModel.getType();
ActionSupport action=null;
try {
action=(ActionSupport) Class.forName(type).newInstance();
// 完成实体类参数封装
if(action instanceof ModelDriver) {
// 当前子控制器实现了模型驱动接口
ModelDriver m=(ModelDriver) action;
Object bean=m.getModel();
// 所有的请求参数都在这,需要将所有的请求参数封装到Book/Goods/
BeanUtils.populate(bean, request.getParameterMap());
// 执行业务逻辑 bookAction.add方法的返回值 “list”
/*
* 1.书籍新增那么跳转书籍展示页面BookList.jsp(转发)
* 2.书籍编辑跳转编辑界面BookEdit.jsp(重定向)
*/
String res=action.execute(request, response);
ForWardModel forwarModel=actionModel.pop(res);
String path = forwarModel.getPath();
boolean redirect = forwarModel.isRedirect();
if(redirect) {
response.sendRedirect(request.getContextPath()+path);
}else {
request.getRequestDispatcher(path).forward(request, response);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?ModelDriver(模型驱动接口)
package com.DHM.framework;
/**
* 模型驱动接口
* 作用:帮助中央控制器完成参数封装的工程
*Book book = new Book();
*book.setBid(req.getParameter("bid"));
*book.setBname(req.getParameter("bname"));
*book.setPrice(req.getParameter("price"));
*book.setAthor(req.getParameter("athor"));
*book.setPublish(req.getParameter("publish"));
*System.out.println(book);
* @author Administrator
*
* @param <T>
*/
public interface ModelDriver<T> {
/**
* GoodsAction-->goods
* BookAction-->book
* ...
* @return
*/
T getModel();
}
BookAction(extends ActionSupport implements ModelDriver)
package com.DHM.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.DHM.entity.Book;
public class BookAction extends ActionSupport implements ModelDriver{
// 从父类继承了execute方法,就把反射动态调用的方法继承过来了
// 当前自动控制器在哪里调用?把子控制器与浏览器请求关联起来
public Book book=new Book();
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("listbookdao.......");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("delbookdao.......");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("editbookdao.......");
}
private String add(HttpServletRequest request, HttpServletResponse response) {
// book.setBid(req.getParameter("bid"));
// book.setBname(req.getParameter("bname"));
// book.setPrice(req.getParameter("price"));
// book.setAthor(req.getParameter("athor"));
// book.setPublish(req.getParameter("publish"));
System.out.println(book);
System.out.println("bookDao.add(book)...");
return "list";
}
@Override
public Object getModel() {
// TODO Auto-generated method stub
return null;
}
}
?界面
重现参数处理代码冗余的问题 <form action="${pageContext.request.contextPath }/book.action?methodName=add" method="post"> ?? ?书籍id:<input type="text" name="bid" value="2"><br> ?? ?书籍名称:<input type="text" name="bname" value="2x"><br> ?? ?书籍价格:<input type="text" name="price" value="2a"><br> ?? ?书籍作者:<input type="text" name="athor" value="2b"><br> ?? ?书籍出版社:<input type="text" name="publish" value="2c"><br> ?? ?<input type="submit"> </form>
运行结果
?4、关于结果页面的跳转(转发、重定向)
? ? 中央控制器? DispathServlet包 、? BookAction 、xml同上
?子控制器(作用:能够处理浏览器的“所有”请求。包括add/ref/other... ? ?通过返回值来决定跳转哪一个页面(至于是重定向/转发由中央控制器决定..))
package com.DHM.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 处理浏览器请求
* 针对于add/ref/other进行向上抽取、抽象
*
* Servlet implementation class Action
*/
public interface Action {
//这一个方法就是add/ref/other进行向上抽取的抽象方法
public String execute(HttpServletRequest req,HttpServletResponse resp);
}
ActionSupport(能够处理浏览器的"所有"请求。包括add/ref/other)
package com.DHM.framework;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 作用:能够处理浏览器的"所有"请求。包括add/ref/other
* @author T440s
*
*/
public class ActionSupport implements Action{
private static final Class<?> HttpServletResponse = null;
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) {
String method=req.getParameter("methodName");
String res = null;
try {
Method m = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
m.setAccessible(true);
res = (String) m.invoke(this, req, resp);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
界面代码
解决结果码页面跳转代码冗余问题 <a href="${pageContext.request.contextPath }/book.action?methodName=add">新增</a> <a href="${pageContext.request.contextPath }/book.action?methodName=toEdit">去往编辑界面</a>
没有啦!!!!
|