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 ...
随机推荐
- Day05_25_Super关键字
Super关键字 Super关键字的所用 Super关键字的用法有三种: 在子类的成员方法中,访问父类的成员变量. 在子类的成员方法中,访问父类的成员方法. 在子类的构造方法中,访问父类的构造方法. ...
- IdentityServer4+OAuth2.0+OpenId Connect 详解
一 Oauth 2.0 1 定义 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. ...
- 手机改 user模式为debug模式
logcat 是Android中一个命令行工具,可用于监控手机应用程序的log信息.网上相关的教学很多,这里只想把自己折腾 2 部手机(一个是三星S4 I9500 港水,Android 5.01,一个 ...
- IPC$共享和其他共享(C$、D$)
目录 net use共享命令的用法 IPC$ IPC空连接 ipc$使用的端口 关闭IPC$共享 net use共享命令的用法 net use #查看连接 net share ...
- cf534D 枚举握手次数
题意: 有n个学生进教室,先后顺序不同,每个人进去后会和当前在教室里的人握手,并且记录人数,而且当教室里有超过三个人的时候 他们有可能组队去参加比赛,后来的人看不到他们. 思路: ...
- CVE-2013-2551:Internet Explore VML COALineDashStyleArray 整数溢出漏洞简单调试分析
0x01 2013 Pwn2Own 黑客大赛 在 Pwn2Own 的黑客大赛上,来自法国的 VUPEN 安全团队再一次利用 0day 漏洞攻破 Windows8 环境下的 IE10 浏览器,这一次问题 ...
- (邹博ML)矩阵和线性代数
主要内容 矩阵 特征值和特征向量 矩阵求导 矩阵 SVD的提法 奇异值分解(Singular Value Decomposition)是一种重要的矩阵分解方法,可以看做对称方阵在任意矩阵上的推广. 假 ...
- spring-boot-maven-plugin not found的解决方案
spring-boot-maven-plugin not found 在maven测试的生命周期都没有错,但是就是爆红 参考了很多的链接,没有成功解决,最后得到真正有帮助的方法,添加springboo ...
- 【前端】vue2.x 配合 bootstrapTable 动态添加元素和绑定点击事件,事件无效 解决
背景: 使用bootstrap-table 表格插件时,每一行的最后一班会加操作按钮列.如果不加入vue的话,使用插件自己的列属性formatter:function(value, row, inde ...
- Postman(接口自动化测试)
1.Postman 接口测试参数化可能大家都非常的熟悉,但是很多人很难处理参数化后如何断言的问题,特别是当参数中出现中文时,很容易导致在 Runner 页面引入外部文件时导致中文乱码的问题,今天这篇文 ...