zoukankan      html  css  js  c++  java
  • Atitit mvc框架的实现 mvc的原理demo v2 sbb.docx 目录 1. 原理流程, 1 1.1. 项目启动的时候启动mvc框架扫描,建立url 方法对应表 1 1.2. 执行ur

    Atitit  mvc框架的实现 mvc的原理demo v2 sbb.docx

     

    目录

    1. 原理流程, 1

    1.1. 项目启动的时候启动mvc框架扫描,建立url 方法对应表 1

    1.2. 执行url的时候,执行webfilter,获取的对应method 1

    1.3. 使用反射ai调用方法输出 2

    2. code 2

    2.1. class MvcCtrol { 2

    2.2. MvcFilter 2

    2.3. MvcUtil 5

     

    /springbootUpload/src/mvcpkg/MvcCtrol.java

    http://localhost:8080/mvctest

     

    1. 原理流程,
      1. 项目启动的时候启动mvc框架扫描,建立url 方法对应表

    public class Application {

     

    public static void main(String[] args) {

     

     //boot mvc framwork

    MvcUtil.get_url_method_mapper(MvcCtrol.class);

     

      1. 执行url的时候,执行webfilter,获取的对应method

    Method meth =MvcUtil. url_method_maps.get(uri);  

     

    Class<? > class1 = meth.getDeclaringClass();

    Object  classObj = ConstructorUtils.invokeConstructor(class1, null);

    Object[] args =MvcUtil.getArgs(meth,httpServletRequest,httpServletResponse); //new Object[] {};

    String r = (String) meth.invoke(classObj, args);

     

      1. 使用反射ai调用方法输出

     

    1. code
      1. class MvcCtrol {

    @MyComponent

    public class MvcCtrol {

        //  http://localhost:8080/mvctest

    @MyMapping("/mvctest")

    public String mvctestMeth(HttpServletRequest req) {

     

    return "data";

    }

     

    }

      1. MvcFilter 

     

     

    package mvcpkg;

     

    import java.io.File;

    import java.io.IOException;

    import java.lang.reflect.InvocationTargetException;

    import java.lang.reflect.Method;

    import java.lang.reflect.Parameter;

    import java.util.List;

    import java.util.Map;

    import java.util.function.Consumer;

     

    import javax.servlet.Filter;

    import javax.servlet.FilterChain;

    import javax.servlet.FilterConfig;

    import javax.servlet.ServletException;

    import javax.servlet.ServletOutputStream;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.annotation.WebFilter;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

     

    import org.apache.commons.io.FileUtils;

    import org.apache.commons.lang3.ObjectUtils;

    import org.apache.commons.lang3.reflect.ConstructorUtils;

    import org.springframework.stereotype.Component;

     

    import com.google.common.base.Objects;

    import com.google.common.collect.Lists;

    import com.google.common.collect.Maps;

     

    @Component

    @WebFilter(urlPatterns = "/*", filterName = "mvcFilter")

    public class MvcFilter implements Filter {

     

    @Override

    public void destroy() {

    // TODO Auto-generated method stub

     

    }

     

    @Override

    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) {

    try {

     

    // get url out mapper

       // Map<String, Method> url_method_maps = MvcUtil.get_url_out_mapper();

     

    HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;

    HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;

    String uri = httpServletRequest.getRequestURI();

    System.out.println(uri);

     

     if(MvcUtil.url_method_maps.get(uri)==null)

     {

    arg2.doFilter(arg0, arg1);

    return;

     }

     

     

    Method meth =MvcUtil. url_method_maps.get(uri);  

     

    Class<? > class1 = meth.getDeclaringClass();

    Object  classObj = ConstructorUtils.invokeConstructor(class1, null);

    Object[] args =MvcUtil.getArgs(meth,httpServletRequest,httpServletResponse); //new Object[] {};

    String r = (String) meth.invoke(classObj, args);

    outputHtml(httpServletResponse, r);

    return;  //not to next ,beirs springmvc show err,cause cant this mappering  

    // arg2.doFilter(arg0, arg1);

     

    } catch (Exception e) {

        throw new RuntimeException(e);

    }

    }

     

     

     

    // private List getMycomponentClassList() {

    // List li_allclass=Lists.newArrayList();

    // li_allclass.add(MvcCtrol.class);

    //

    // List li= Lists.newArrayList();

    // classList.add();

    //

    // }

     

    private void outputHtml(HttpServletResponse httpServletResponse, String txt) throws IOException {

    byte[] s = txt.getBytes();

    ServletOutputStream outputStream = httpServletResponse.getOutputStream();

    outputStream.write(s);

      outputStream.flush();

    }

     

    @Override

    public void init(FilterConfig arg0) throws ServletException {

    // TODO Auto-generated method stub

     

    }

     

    }

     

      1. MvcUtil 

     

      package mvcpkg;

     

    import java.lang.reflect.Method;

    import java.lang.reflect.Parameter;

    import java.util.List;

    import java.util.Map;

    import java.util.function.Consumer;

     

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServletRequest;

     

    import com.google.common.collect.Lists;

    import com.google.common.collect.Maps;

     

    public class MvcUtil {

     

     

     

     

    public static  Map<String, Method> url_method_maps = Maps.newLinkedHashMap();

    public static  Map<String, Method> get_url_method_mapper(Class class1) {

    // MyComponent MyComponentAnno = (MyComponent) class1.getAnnotation(MyComponent.class);

    // if (MyComponentAnno == null)

    // return;

     

    // 获取一个类的注解,根据注解名

    Method[] methds = class1.getMethods();

    for (Method method : methds) {

    MyMapping anno1 = (MyMapping) method.getAnnotation(MyMapping.class);

    if (anno1 == null)

    continue;

     

    String uri_mapping = anno1.value();  

    url_method_maps.put(uri_mapping, method);

    }

    return url_method_maps;

    }

     

    public static Object[] getArgs(Method meth, ServletRequest arg0, ServletResponse arg1) {

    List li=Lists.newArrayList();

    Parameter[] Parameters_arr= meth.getParameters();

    for (Parameter parameter : Parameters_arr) {

    Class<?> type1 = parameter.getType();

    if  (  type1 ==HttpServletRequest.class)

    {

    li.add(arg0);

    }

    }

    return li.toArray();

    }

     

    private static void ClassListForeach(Consumer<Class<?>> consumer) {

    List<Class<?>> li_allclass = Lists.newArrayList();

    li_allclass.add(MvcCtrol.class);

    for (Class class1 : li_allclass) {

    consumer.accept(class1);

    }

     

    }

     

     

    // public static  Map<String, MethodObj> get_url_out_mapper() {

    // Map<String, MethodObj> url_method_maps = Maps.newLinkedHashMap();

    // ClassListForeach(new Consumer<Class<?>>() {

    //

    // @Override

    // public void accept(Class<?> cls) {

    //

    // MyComponent MyComponentAnno = (MyComponent) cls.getAnnotation(MyComponent.class);

    // if (MyComponentAnno == null)

    // return;

    //

    // // 获取一个类的注解,根据注解名

    // Method[] methds = cls.getMethods();

    // for (Method method : methds) {

    // MyMapping anno1 = (MyMapping) method.getAnnotation(MyMapping.class);

    // if (anno1 == null)

    // continue;

    //  

    // String uri_mapping = anno1.value();

    // MethodObj MethodObj1 = new MethodObj();

    // MethodObj1.classProp = cls;

    // MethodObj1.methodProp = method;

    // url_method_maps.put(uri_mapping, MethodObj1);

    // }

    //

    // }

    // });

    //

    // return url_method_maps;

    //}

     

    }

     

  • 相关阅读:
    关于hibernate的缓存使用(转)
    Webservice 实践
    三大电商注册登录表单分析
    网上好文搜集
    git简介
    Python程序中的进程操作--—--开启多进程
    进程的创建和结束
    同步异步阻塞和非阻塞
    进程的并行和并发
    进程调度
  • 原文地址:https://www.cnblogs.com/attilax/p/15197386.html
Copyright © 2011-2022 走看看