自定义MVC--02

简介: 自定义MVC--02

1.什么是自定义MVC:

自定义MVC(Model-View-Controller)是一种软件架构模式,用于组织和管理软件应用程序的代码和逻辑。MVC模式将应用程序分为三个主要部分:

  1. 模型(Model):模型代表应用程序的数据和业务逻辑。它负责处理与数据相关的操作,例如读取、写入、更新和删除数据,以及执行业务规则和验证。
  2. 视图(View):视图负责呈现数据给用户并处理用户的输入交互。它负责展示模型数据的外观和格式,并将用户的操作传递给控制器进行处理。
  3. 控制器(Controller):控制器接收来自视图的用户输入,并根据输入更新模型数据或执行其他逻辑操作。它作为模型和视图之间的中间人,负责协调它们之间的通信和交互。

2.利用建模反射优化:

利用建模反射优化是一种通过分析和调整软件系统的结构和行为来提高性能和效率的优化方法。它基于建模反射的概念,即通过对系统进行建模和分析,了解系统的不足之处,并通过对模型进行调整来改善系统的性能。

编写子控制器:

优化:这段代码是死的

通过XML将它写活:所有的子控制器都放在MVC.XML中

以前所以的子控制器是放到map集合中,现在所有的子控制器在mvc.xml中,其实就是放到comfigModel中

package com.xiaoye.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.xiaoye.framework.model.ActionModel;
import com.xiaoye.framework.model.ConfigModel;
import com.xiaoye.framework.model.ConfigModelFactory;
import com.xiaoye.web.BookAction;
import com.xiaoye.web.OrderAction;
/**
 * 对应图中的ActionServlet:中央控制器
 * @author bing人
 * @com.xiaoye.framework
 * @DispathServlet
 */
@WebServlet("*.action")
public class DispathServlet extends HttpServlet{
//  public Map<String, Action> actionMap =new HashMap<String, Action>();
//  以前所以的子控制器是放到map集合中,现在所有的子控制器在mvc.xml中,其实就是放到comfigModel中
  private ConfigModel configModel;
  @Override
  public void init() throws ServletException {
//    actionMap.put("/book", new BookAction());
//    actionMap.put("/order", new OrderAction());
    try {
//      configModel是空的意味着需要初始化然后就有内容了通过build就有内容了
      configModel =ConfigModelFactory.build();
//      configModel包含了所有的子控制器
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String uri = req.getRequestURI();
    uri =uri.substring(uri.lastIndexOf("/"),
        uri.lastIndexOf("."));
//    Action action = actionMap.get(uri);
    //要通过uri=/book在configModel对象中找
    ActionModel actionModel = configModel.pop(uri);
    if(actionModel==null) 
      throw new RuntimeException("action not config");
    String type = actionModel.getType();
    Action action;
    try {
      action = (Action) Class.forName(type).newInstance();
      action.execut(req, resp);
    } catch (Exception  e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  }

输出结果:

 

记得调配置:

如果报这个错代表没有调试配置:

优化方法调用结果集跳转问题:

当一个方法返回一个结果集(如列表、数组等),并且在方法内部对该结果集进行了修改或排序等操作后,如果继续在方法内对结果集进行迭代或操作,可能会导致结果集的索引或位置发生变化,从而产生错误的结果或逻辑错误。

常识:查询必然转发,增删改使用重定向

package com.xiaoye.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.xiaoye.framework.model.ActionModel;
import com.xiaoye.framework.model.ConfigModel;
import com.xiaoye.framework.model.ConfigModelFactory;
import com.xiaoye.framework.model.ForwardModel;
import com.xiaoye.web.BookAction;
import com.xiaoye.web.OrderAction;
/**
 * 对应图中的ActionServlet:中央控制器
 * @author bing人
 * @com.xiaoye.framework
 * @DispathServlet
 */
@WebServlet("*.action")
public class DispathServlet extends HttpServlet{
//  public Map<String, Action> actionMap =new HashMap<String, Action>();
//  以前所以的子控制器是放到map集合中,现在所有的子控制器在mvc.xml中,其实就是放到comfigModel中
  private ConfigModel configModel;
  @Override
  public void init() throws ServletException {
//    actionMap.put("/book", new BookAction());
//    actionMap.put("/order", new OrderAction());
    try {
//      configModel是空的意味着需要初始化然后就有内容了通过build就有内容了
      configModel =ConfigModelFactory.build();
//      configModel包含了所有的子控制器
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String uri = req.getRequestURI();
    uri =uri.substring(uri.lastIndexOf("/"),
        uri.lastIndexOf("."));
//    Action action = actionMap.get(uri);
    //要通过uri=/book在configModel对象中找
    ActionModel actionModel = configModel.pop(uri);
    if(actionModel==null) 
      throw new RuntimeException("action not config");
    String type = actionModel.getType();
    Action action;
    try {
      action = (Action) Class.forName(type).newInstance();
      //具体业务代码执行后的返回值 add/upd/del/list的返回值==》list/toList
      String res = action.execut(req, resp);
      //要通过返回值拿到该方法结果是重定向还是转发,还是跳转那个页面
      ForwardModel forwardModel = actionModel.pop(res);
      if(forwardModel !=null) {
        boolean redirect = forwardModel.isRedirect();
        String path = forwardModel.getPath();
        if(redirect) {
          resp.sendRedirect(req.getContextPath() +"/"+ path);
        }else {
          req.getRequestDispatcher(path).forward(req, resp);
        }
      }
    } catch (Exception  e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  }

用转发代码跳转:

package com.xiaoye.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xiaoye.framework.Action;
public class BookAction extends Action{
  public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.add");
    req.setAttribute("content", "蚊子来了");
    //不用转发代码可以实现转发吗
    resp.sendRedirect("res.jsp");
    //return "toList";
  }
  public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.del");
    req.setAttribute("content", "蚊子来了");
    resp.sendRedirect("res.jsp");
    //配个结果
    //return "toList";
  }
  public void upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.upd");
    req.setAttribute("content", "蚊子来了");
    resp.sendRedirect("res.jsp");
    //return "toList";
  }
  public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.list");
    req.setAttribute("content", "蚊子来了");
        req.getRequestDispatcher("res.jsp").forward(req, resp);
    //return "list";
  }
}

 

 

点击删除:

不用转发代码跳转:

package com.xiaoye.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xiaoye.framework.Action;
public class BookAction extends Action{
  public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.add");
    req.setAttribute("content", "蚊子来了");
    //不用转发代码可以实现转发吗
//    resp.sendRedirect("res.jsp");
    return "toList";
  }
  public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.del");
    req.setAttribute("content", "蚊子来了");
//    resp.sendRedirect("res.jsp");
    //配个结果
    return "toList";
  }
  public String upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.upd");
    req.setAttribute("content", "蚊子来了");
//    resp.sendRedirect("res.jsp");
    return "toList";
  }
  public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("bookAddServlet.list");
    req.setAttribute("content", "蚊子来了");
//        req.getRequestDispatcher("res.jsp").forward(req, resp);
    return "list";
  }
}

 

 

点击删除:

上面两个输出结果:

优化参数封装

以前的增删改查代码:

优化:

1.要有表对应的类属性对象 Book book

//      2.获取到所有的参数及参数值    

3.创建一个模型驱动接口ModelDriver

4.要做到所有的子控制器通用 :

这段代码中这一行是最重要的

 

 

目录
相关文章
|
10月前
|
设计模式 前端开发 搜索推荐
自定义MVC系列(一)之自定义MVC简介
自定义MVC系列(一)之自定义MVC简介
|
9月前
|
存储 前端开发 数据可视化
自定义MVC(上)
自定义MVC(上)
56 1
|
9月前
|
存储 设计模式 前端开发
自定义MVC介绍
自定义MVC介绍
25 0
|
8月前
|
XML 前端开发 Java
自定义MVC的初步实现
自定义MVC的初步实现
41 0
自定义MVC的初步实现
|
8月前
|
设计模式 前端开发 搜索推荐
自定义MVC
自定义MVC
50 0
|
8月前
|
存储 设计模式 前端开发
自定义MVC实现
自定义MVC实现
|
8月前
|
设计模式 前端开发
自定义mvc
自定义mvc
40 0
|
9月前
|
前端开发
mvc配置指定参数处理
mvc配置指定参数处理
42 0
|
9月前
|
设计模式 前端开发 搜索推荐
认识什么是自定义MVC
认识什么是自定义MVC
36 0
|
10月前
|
前端开发 Java 数据库
自定义MVC--03
自定义MVC--03
27 0