• 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 还可以配置拦截器和视图解析器

4. springmvc底层原理2的更多相关文章

  1. 深入源码分析SpringMVC底层原理(二)

    原文链接:深入源码分析SpringMVC底层原理(二) 文章目录 深入分析SpringMVC请求处理过程 1. DispatcherServlet处理请求 1.1 寻找Handler 1.2 没有找到 ...

  2. SpringMVC底层执行原理

    一个简单的HelloSpringMVC程序 先在web,xml中注册一个前端控制器(DispatcherServlet) <?xml version="1.0" encodi ...

  3. 漫画 | Spring AOP的底层原理是什么?

    1.Spring中配置的bean是在什么时候实例化的? 2.描述一下Spring中的IOC.AOP和DI IOC和AOP是Spring的两大核心思想 3.谈谈IOC.AOP和DI在项目开发中的应用场景 ...

  4. Spring_day01--课程安排_Spring概念_IOC操作&IOC底层原理&入门案例_配置文件没有提示问题

    Spring_day01 Spring课程安排 今天内容介绍 Spring概念 Spring的ioc操作 IOC底层原理 IOC入门案例 配置文件没有提示问题 Spring的bean管理(xml方式) ...

  5. springMVC课程笔记(一)springMVC架构原理分析

    一.springMVC架构原理分析 1.先搞清楚什么是springMVC: 其实springMVC是spring框架中的一个模块,springMVC和spring无需通过中间整合层整合,SpringM ...

  6. [转帖]Spring Cloud底层原理

    拜托!面试不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA 毫无疑问,Spring Cloud 是目前微服 ...

  7. Spring Boot-自动配置之底层原理

    一.SpringBoot启动的时候加载主配置类,开启了自动配置的功能 @SpringBootApplication public class SpringBoot02Application { pub ...

  8. Neo4j图数据库简介和底层原理

    现实中很多数据都是用图来表达的,比如社交网络中人与人的关系.地图数据.或是基因信息等等.RDBMS并不适合表达这类数据,而且由于海量数据的存在,让其显得捉襟见肘.NoSQL数据库的兴起,很好地解决了海 ...

  9. springmvc工作原理和环境搭建

    SpringMVC工作原理     上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServle ...

随机推荐

  1. Team Queue UVA - 540

      Queues and Priority Queues are data structures which are known to most computer scientists. The Te ...

  2. Day09_46_Set集合_SortedSet03

    SortedSet03 让SortedSet集合完成比较,还有另外一种方法,那就是单独编写一个比较器. java.util.comparator 在TreeSet集合创建的时候可以在集合中传入一个比较 ...

  3. K8S(18)容器环境下资源限制与jvm内存回收

    K8S(18)容器环境下资源限制与jvm内存回收 目录 K8S(18)容器环境下资源限制与jvm内存回收 一.k8s中的java资源限制与可能的问题 方案1:通过JVM的Xms和Xmx参数限制 方案2 ...

  4. springboot 配置将info、error、debug 分别输出到不同文件

    在resource下创建名为 logback-spring.xml 的配置文件,内容如下: <?xml version="1.0" encoding="UTF-8& ...

  5. vue.js中使用set方法 this.$set

    vue教程中有这样一个注意事项: 第一种具体情况如下: 运行结果: 当利用索引改变数组某一项时,页面不会刷新.解决方法如下: 运行结果: 三种方式都可以解决,使用Vue.set.vm.$set()或者 ...

  6. 【SpringBoot】SpringBoot 处理后端返回的小数(全局配置 + 定制化配置)

    一.抛出问题: 现在的项目中,存在这样的几个问题: 问题一.数据库存的数据类型是BigDecimal,或者代码中计算需要返回BigDecimal的值,由于BigDecimal返回给前端可能存在精度丢失 ...

  7. IT培训软件测试怎么样,问问“过来人”!

    经常看到有人在网上发帖子问:"XX培训(IT培训机构)怎么样,学过的老哥可以出来讲讲真话吗?"问这种问题的同学,来,站起来!我不得不在这儿说你两句:你要想知道一家IT培训机构到底怎 ...

  8. HTML中的JavaScript

    HTML中的JavaScript 1.<script>元素 defer:可选.表示脚本可以延迟到文档完全被解析和显示之后再执行.只对外部脚本文件有效. 脚本会被延迟到整个页面都解析完毕后再 ...

  9. C++ primer plus读书笔记——第17章 输入、输出和文件

    第17章 输入.输出和文件 1. 对键盘进行输入缓冲可以让用户在将输入传输给程序之前返回并更正.C++程序通常在用户按下回车键时刷新输入缓冲区. 2. 一些I/O类 streambuf类为缓冲区提供了 ...

  10. OOP第三章博客

    OO第三单元博客 • (1)梳理JML语言的理论基础.应用工具链情况: 理论基础: 网络资料上面介绍JML有两种主要的用法: 开展规格化设计.这样交给代码实现人员的将不是可能带有内在模糊性.二义性的自 ...