博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义mvc(一、二)
阅读量:4974 次
发布时间:2019-06-12

本文共 8317 字,大约阅读时间需要 27 分钟。

1. 什么是MVC

   MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
   它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码

 

  Model1 jsp+jdbc

 

    Model2 ->MVC

 

 

2. 自定义MVC工作原理图

  工具类:

ActionModel.java

ConfigModel.java

ConfigModelFactory.java

ForwardModel.java

mvc.xml

 

3. 通过XML对自定义mvc框架进行增强

3.1将子控制器信息动态配置到mvc.xml中

DispatcherServlet.java

package com.huang.framework;import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;/** * 中央控制器 *         作用:接收请求,通过请求寻找处理请求的对应的自控器 * @author Admin * */public class DispatcherServlet extends HttpServlet {    private static final long serialVersionUID = 7565258196310251777L;//    private Map
actionMap=new HashMap<>();// 在configModel对象中包含了所有的子控制器 private ConfigModel configModel; public void init() {// actionMap.put("/addCal", new AddCalAction());// actionMap.put("/delCal", new DelCalAction());// actionMap.put("/cheCal", new CheCalAction());// actionMap.put("/chuCal", new ChuCalAction()); try { configModel=ConfigModelFactory.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @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 { init(); String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));// Action action=actionMap.get(url); ActionModel actionModel=configModel.get(url); if(actionModel==null) { throw new RuntimeException("你没有配置action标签,找不到对应的子控制器来处理浏览器发送的请求!!"); } try { Action action= (Action) Class.forName(actionModel.getType()).newInstance(); if(action instanceof ModelDrivern) { ModelDrivern mdDrivern = (ModelDrivern) action;// 此时的model的所有属性值是null的 Object model=mdDrivern.getModel(); BeanUtils.populate(model, req.getParameterMap()); // 我们可以req.getParameterMap()的值通过反射的方式将其塞进model实例// Map
parameterMap=req.getParameterMap();// Set
> entrySet=parameterMap.entrySet();// Class
clz= model.getClass();// for (Entry
entry : entrySet) {// Field field=clz.getField(entry.getKey());// field.setAccessible(true);// field.set(model, entry.getValue());// // } } String code=action.execute(req, resp); ForwardModel forwardModel= actionModel.get(code); if(forwardModel!=null) { String jspPath=forwardModel.getPath(); if("false".equals(forwardModel.getRedirect())) { //做转发的处理 req.getRequestDispatcher(jspPath).forward(req, resp); } else { resp.sendRedirect(req.getContextPath()+"/"+jspPath); } } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

3.2针对mvc框架的结果码进行处理

cal.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here
第一个数:
第二个数:

3.3将一组操作放到一个子控制器中

ActionSupport.java

package com.huang.framework;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 增强版的子控制器 *         原来的子控制器只能一个用户请求 *         有时候,用户请求是多个,但是都是操作同一张表,那么原有的子控制器代码编写繁琐 *         增强版的作用 *         将一组相关的操作放到一个Action中 * @author Admin * */public class ActionSupport implements Action {    @Override    public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//        add/del/che/chu        String methodName=req.getParameter("methodName");        String code=null;//        class CalAction extends ActionSupport//        this在这里指的是CalAction它的一个类实例        try {            Method m=this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);            m.setAccessible(true);             code=(String) m.invoke(this, req,resp);        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {            e.printStackTrace();        }        return code;    }        }

3.4利用模型驱动接口对mvc框架进行增强

CalAction.java

package com.huang.web;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huang.entity.Cal;

import com.huang.framework.ActionSupport;
import com.huang.framework.ModelDrivern;

public class CalAction extends ActionSupport  implements ModelDrivern<Cal>{

 private Cal cal=new Cal();

 public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  String num1=req.getParameter("num1");
//  String num2=req.getParameter("num2");
//  Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
  req.setAttribute("res", cal.getNum1() + cal.getNum2());
//  req.getRequestDispatcher("res.jsp").forward(req, resp);
  return "res";
 }
 
 public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  String num1=req.getParameter("num1");
//  String num2=req.getParameter("num2");
//  Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
  req.setAttribute("res", cal.getNum1() - cal.getNum2());
//  req.getRequestDispatcher("res.jsp").forward(req, resp);
  return "res";
 }
 
 public String che(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  String num1=req.getParameter("num1");
//  String num2=req.getParameter("num2");
//  Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
  req.setAttribute("res", cal.getNum1() * cal.getNum2());
//  req.getRequestDispatcher("res.jsp").forward(req, resp);
  return "res";
 }
 
 public String chu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  String num1=req.getParameter("num1");
//  String num2=req.getParameter("num2");
//  Cal cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2));
  req.setAttribute("res", cal.getNum1() / cal.getNum2());
//  req.getRequestDispatcher("res.jsp").forward(req, resp);
  return "res";
 }

 @Override

 public Cal getModel() {
  return cal;
 }
 
}

3.5解决框架配置文件的冲突问题

web.xml

T226_mvc
dispatcherServlet
com.huang.framework.DispatcherServlet
xmlPath
/mvc.xml
dispatcherServlet
*.action

 

转载于:https://www.cnblogs.com/bf6rc9qu/p/11084491.html

你可能感兴趣的文章
GMA Round 1 新程序
查看>>
node anyproxy ssi简易支持
查看>>
编译预处理指令:文件包含指令、宏定义指令、条件编译指令
查看>>
PHP函数 ------ ctype_alnum
查看>>
网站安全
查看>>
WS-Addressing 初探
查看>>
.NET+模块编排+数据库操作类的封装+分层架构+实体类+Ajax.net+Athem.NET+javascript+Activex组件+用户权限等...
查看>>
Markdown不常见功能
查看>>
(二)NUnit单元测试心得
查看>>
hdu_2604Queuing(快速幂矩阵)
查看>>
frame.bounds和center
查看>>
HDU 1102 Constructing Roads
查看>>
android StaticLayout参数解释
查看>>
多线程之ThreadLocal类
查看>>
Qt-读取文本导出word
查看>>
OC语言description方法和sel
查看>>
C#中得到程序当前工作目录和执行目录的五种方法
查看>>
扫描线与悬线
查看>>
用队列和链表的方式解决约瑟夫问题
查看>>
python 迭代器与生成器
查看>>