zoukankan      html  css  js  c++  java
  • 4. springmvc底层原理2

    • Spring mvc 是子容器
    • Spring 是 父容器

    ===================================================================

    public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { RootConfig.class };
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { App1Config.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] { "/app1/*" };
        }
    }
    
    
    

    刚才我们是写个类实现人家的接口,现在我们写个类继承人家的接口

    ===================================================================

    ===================================================================

    • 继承AbstractAnnotationConfigDispatcherServletInitializer
    package com.min.demo1;
    
    import com.min.config.App1Config;
    import com.min.config.RootConfig;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public  class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { RootConfig.class };
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { App1Config.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] { "*.do" };
        }
    }
    

    ===================================================================

    • 编写配置类
      **App1Config **
    package com.min.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(value = "com.min.controller")
    public class App1Config {
    }
    

    **RootConfig **

    package com.min.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(value = "com.min.service")
    public class RootConfig {
    
    }
    
    

    ===================================================================

    • 编写Controller
    package com.min.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class TestController {
    
        @RequestMapping(value = "/hello2.*")
        @ResponseBody
        public String hello(){
            return "hello";
        }
    }
    
    
    • 访问hello2.do

    ===================================================================

    ===================================================================

    @Configuration
    @EnableWebMvc
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addFormatters(FormatterRegistry registry) {
            // ...
        }
    }
    

    ===================================================================
    默认实现方法

    ===================================================================

    • 添加跳转jsp视图(配置视图解析器)

    package com.min.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    @ComponentScan(value = "com.min.controller")
    @EnableWebMvc
    public class App1Config implements WebMvcConfigurer {
    
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            /*
             *  public UrlBasedViewResolverRegistration jsp() {
             *     return this.jsp("/WEB-INF/", ".jsp");
             *  }
             */
            //registry.jsp();
    
            registry.jsp("/WEB-INF/jsps/",".jsp");
        }
    }
    
    • 编写controlelr
    
    package com.min.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class TestController {
    
        @RequestMapping(value = "/hello.do")
        @ResponseBody
        public String hello(){
            return "hello";
        }
    
        @RequestMapping(value = "/hello2.do")
        public String hello2(){
            return "a";
        }
    }
    
    
    • /web-inf/jsps/下编写a.jsp页面
    <%--
      Created by IntelliJ IDEA.
      User: lenovo
      Date: 2020/6/5
      Time: 9:20
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>我是JSP页面视图</h1>
    </body>
    </html>
    
    

    • App1Config 还可以配置拦截器和视图解析器

  • 相关阅读:
    PyTorch学习笔记7--案例3:基于CNN的CIFAR10数据集的图像分类
    [转载]使用 Pandoc 与 Markdown 生成 PDF 文件
    [转载]论文笔记:目标检测算法(R-CNN,Fast R-CNN,Faster R-CNN,FPN,YOLOv1-v3)
    [转载]论文笔记:CNN经典结构2(WideResNet,FractalNet,DenseNet,ResNeXt,DPN,SENet)
    [转载]论文笔记:CNN经典结构1(AlexNet,ZFNet,OverFeat,VGG,GoogleNet,ResNet)
    [转载]ImageNet历年冠军和相关CNN模型
    神经网络模型Tricks归纳
    Python生成windows可执行的exe文件
    如何让chrome浏览器自动打开Adobe Flash Player?
    PyTorch学习笔记6--案例2:PyTorch神经网络(MNIST CNN)
  • 原文地址:https://www.cnblogs.com/m987/p/13047911.html
Copyright © 2011-2022 走看看