• 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. CPF 入门教程 - 设计器和模板库的使用(五)

    CPF netcore跨平台UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF ...

  2. 【原创】【基础】一文搞懂严蔚敏数据结构SqList &L和SqList L、ElemType &e和ElemType e

    旁白 最近小渔夫在看严蔚敏.李冬梅<数据结构 c语言版>(第2版),学到第二章顺序表的实现时,看到函数参数一会是SqList &L.一会又是SqList L.一会ElemType ...

  3. 10行C++代码实现高性能HTTP服务

    前言 是不是觉得C++写个服务太累,但又沉迷于C++的真香性能而无法自拔?作为一个老牌C++程序员(可以看我 github 上十几年前的C++项目:https://github.com/kevwan ...

  4. 996. Number of Squareful Arrays

    Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...

  5. 病毒木马查杀实战第010篇:QQ盗号木马之十六进制代码分析

    前言 按照我的个人习惯,在运用诸如IDA Pro与OllyDBG对病毒进行逆向分析之前,我都会利用一些自动化的工具,通过静态或动态的分析方法(参见<病毒木马查杀第008篇:熊猫烧香之病毒查杀总结 ...

  6. UVA10340子序列

    题意:       给你两个串,问你第二个第一个串是否是第一个串删除0个或多个字母得到的? 思路:       直接模拟就行了,在第二个串中去按顺序更新第一个串的下标,好像没说明白啊,不管了,水题,不 ...

  7. hdu 5063 不错的小想法题(逆向处理操作)

    题意:       刚开始的时候给你一个序列,长度为n,分别为a[1]=1,a[2]=2,a[3]=3,a[4]=4...a[n]=n,然后有4种操作如下: Type1: O 1 call fun1( ...

  8. Linux中的防火墙(Netfilter、Iptables、Firewalld)

    目录 Netfilter Iptables iptables做本地端口转发 Firewalld Netfilter Netfilter是Linux 2.4内核引入的全新的包过滤引擎,位于Linux内核 ...

  9. 基于react hooks,antd4 配置生成表单并自动排列

    react后台项目,大多都是表单处理,比如下列4种常见1*n布局 (如果手工编码,大量的Row,Col, Form.Item的嵌套,排列,如果加上联动处理,代码将十分臃肿,不易维护) 一行一列 一行两 ...

  10. 二分查找确定lower_bound和upper_bound

    lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序. 代码如下: int lower_bound(int* ...