手写实现springmvc框架

实现Controller、RequestMapping注解

@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
    String value() default "";
}

@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
    String value() default "";
}

实现ModelAndView


public class ModelAndView {
    private String viewName;

    public String getViewName() {
        return viewName;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

}

实现DispatcherServlet

public class DispatcherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * 解析器前缀
     */
    private String ViewResolver_prefix="/";
    /**
     * 解析器后缀
     */
    private String ViewResolver_suffix=".jsp";
    /**
     * 指定扫描的包路径
     */
    String packagePath="com.controllers";
    /**
     * 将controller对象放入到此map中
     * 类上的RequestMapping注解的value作为key
     */
    Map<String,Object> controllerMap=new HashMap();
    /**
     * 类上的RequestMapping注解的value,加上方法上的RequestMapping注解的value,作为key
     */
    Map<String,Method> methodMap=new HashMap();

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String contextPath=request.getContextPath();
        String uri=request.getRequestURI();
        String url=uri.replace(contextPath, "");
//        StringBuffer url=request.getRequestURL();
        System.out.println(url);
        String classURL=url.substring(0, url.lastIndexOf("/"));
        Object obj=controllerMap.get(classURL);
        Method method=(Method)methodMap.get(url);
        try {
            ModelAndView mv=(ModelAndView)method.invoke(obj);
            String fullMV=ViewResolver_prefix+mv.getViewName()+ViewResolver_suffix;
            request.getRequestDispatcher(fullMV).forward(request, response);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


    }

    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        try {
            URL url = Thread.currentThread().getContextClassLoader().getResource(packagePath.replace(".", "/"));
            File file=new File(url.getFile());
            File[] array=file.listFiles();
            for(File fileTemp:array){
                String fileName=fileTemp.getName();
                String fileNameShort=fileName.substring(0, fileName.indexOf("."));
                Class clazz=Class.forName(packagePath+"."+fileNameShort);
                if(clazz.isAnnotationPresent(Controller.class)){
                    Object obj=clazz.newInstance();
//                    Controller controller = (Controller) clazz.getAnnotation(Controller.class);  
//                    String key = controller.value(); //必须先实例化才能获取注解信息
                    RequestMapping rm=(RequestMapping)clazz.getAnnotation(RequestMapping.class);
                    String classURL=rm.value();
                    controllerMap.put(classURL, obj);
                    Method[] methordArray=clazz.getMethods();
                    for(Method method:methordArray){
                        if(method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping methodRM=method.getAnnotation(RequestMapping.class);
                            String methodURL=methodRM.value();
                            String requestURL=classURL+methodURL;
                            System.out.println(requestURL);
                            methodMap.put(requestURL, method);
                        }
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

配置该servlet

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.mvc.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

实现controller类

@Controller
@RequestMapping("/test")
public class TestCtrl {
    @RequestMapping("/test")
    public ModelAndView test(){
        System.out.println("111");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("test2");
        return mv;
    }
}

打完收工!

感谢https://blog.csdn.net/chaoyueygw/article/details/53393952
对此贴做了大量简化以及优化


原文出处:/show_1EFutw6imbI.html