Spring MVC
1.原理
http://localhost:8080/myapp/app/test
1.localhost:8080
通过8080端口找到tomcat
2./myapp
找到tomcat下myapp应用程序
3./app
①tomcat解析/webapp/WEB-INF/web.xml,
②实例化DispatcherServlet,
③初始化DispatcherServlet.init()并创建Spring容器XmlWebApplicationContext,将spring.xml配置到Spring容器中
④servlet-mapping为/app/*
4./test
遍历Spring容器下面的所有Controller bean对象,找到@GetMapping为/test的方法执行
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
2.SpringMVC的零配置
官网上有介绍,https://docs.spring.io/spring-framework/docs/current/reference/html/web.html
可以直接用java实现
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}