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

4. springmvc底层原理2的更多相关文章
- 深入源码分析SpringMVC底层原理(二)
原文链接:深入源码分析SpringMVC底层原理(二) 文章目录 深入分析SpringMVC请求处理过程 1. DispatcherServlet处理请求 1.1 寻找Handler 1.2 没有找到 ...
- SpringMVC底层执行原理
一个简单的HelloSpringMVC程序 先在web,xml中注册一个前端控制器(DispatcherServlet) <?xml version="1.0" encodi ...
- 漫画 | Spring AOP的底层原理是什么?
1.Spring中配置的bean是在什么时候实例化的? 2.描述一下Spring中的IOC.AOP和DI IOC和AOP是Spring的两大核心思想 3.谈谈IOC.AOP和DI在项目开发中的应用场景 ...
- Spring_day01--课程安排_Spring概念_IOC操作&IOC底层原理&入门案例_配置文件没有提示问题
Spring_day01 Spring课程安排 今天内容介绍 Spring概念 Spring的ioc操作 IOC底层原理 IOC入门案例 配置文件没有提示问题 Spring的bean管理(xml方式) ...
- springMVC课程笔记(一)springMVC架构原理分析
一.springMVC架构原理分析 1.先搞清楚什么是springMVC: 其实springMVC是spring框架中的一个模块,springMVC和spring无需通过中间整合层整合,SpringM ...
- [转帖]Spring Cloud底层原理
拜托!面试不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA 毫无疑问,Spring Cloud 是目前微服 ...
- Spring Boot-自动配置之底层原理
一.SpringBoot启动的时候加载主配置类,开启了自动配置的功能 @SpringBootApplication public class SpringBoot02Application { pub ...
- Neo4j图数据库简介和底层原理
现实中很多数据都是用图来表达的,比如社交网络中人与人的关系.地图数据.或是基因信息等等.RDBMS并不适合表达这类数据,而且由于海量数据的存在,让其显得捉襟见肘.NoSQL数据库的兴起,很好地解决了海 ...
- springmvc工作原理和环境搭建
SpringMVC工作原理 上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServle ...
随机推荐
- Day01_03_Java名词
java名词 SDK 软件开发工具包 JDK Java的软件开发工具包,其中包括Java虚拟机(JVM),Java运行环境(即jre),Java工具(编译器,运行工具等)和Java基础类库等. JRE ...
- Django+Vue+Docker搭建接口测试平台实战
一. 开头说两句 大家好,我叫林宗霖,是一位测试工程师,也是全栈测开训练营中的一名学员. 在跟着训练营学习完Docker容器技术系列的课程后,理所应当需要通过实操来进行熟悉巩固.正好接口自动化测试平台 ...
- win10 添加全局快捷键
前言 好久没写博客了,今天水一下 如何在win10 下添加一个全局唤醒的快捷键(打开截图软件) 步骤 win + Q 输入 管理工具 添加快捷方式 这里没有管理员权限,可以直接把创建好的 快捷方式 粘 ...
- MYSQL中TIMESTAMP类型的默认值理解
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样. 1.自动UPDATE 和INSERT 到当前的时间:表:----------- Table Create Table ...
- Python 爬虫之Scrapy框架
Scrapy框架架构 Scrapy框架介绍: 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自己从零 ...
- jpa查找数据库最新一条消息
主要字段说明: pid:指导记录主键 user_pid:用户主键 competition_project_pid:用户作品 Mysql表 Repository /** * 指导记录 * @date 2 ...
- Servlet三大域对象
Servlet三大域对象的应用 request.session.application(ServletContext) ServletContext是一个全局的储存信息的空间,服务器开始就存在,服务器 ...
- cetnos中nmap端口扫描工具的使用
1:安装: yum -y install nmap 2:使用方法: nmap -p 1-65535 1.1.1.1 #扫描1.1.1.1此IP地址的所有端口 nmap -p 80,443 1.1.1. ...
- Mybatis最终搭建
框架搭建的流程1. 导入jar2. 准备属性文件和配置文件3. 编写数据库的表和类4. 为类编写一个XxxMapper接口5. 编写接口对应的映射文件XxxMapper.xml6. 根据接口的方法, ...
- Gentoo 后的几个细节的完善
Gentoo 后的几个细节的完善 目录 Gentoo 后的几个细节的完善 细节一:引导分区与 cdrom 开机正确挂载 细节二:可预见的命名规则的网络接口名称改为传统的 eth0 细节三:为管理员用户 ...