2.2 编码方式
- public class MyWebAppInitializer implements WebApplicationInitializer {
-
- @Override
- public void onStartup(ServletContext container) {
- // Create the 'root' Spring application context
- AnnotationConfigWebApplicationContext rootContext =
- new AnnotationConfigWebApplicationContext();
- rootContext.register(AppConfig.class);
-
- // Manage the lifecycle of the root application context
- container.addListener(new ContextLoaderListener(rootContext));
-
- // Create the dispatcher servlet's Spring application context
- AnnotationConfigWebApplicationContext dispatcherContext =
- new AnnotationConfigWebApplicationContext();
- dispatcherContext.register(DispatcherConfig.class);
-
- // Register and map the dispatcher servlet
- ServletRegistration.Dynamic dispatcher =
- container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
- dispatcher.setLoadOnStartup(1);
- dispatcher.addMapping("/");
- }
-
- }
内部实现
3.spring boot
继承了spring mvc的框架,实现SpringBootServletInitializer
- package com.mkyong;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.support.SpringBootServletInitializer;
- @SpringBootApplication
- public class SpringBootWebApplication extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(SpringBootWebApplication.class);
- }
- public static void main(String[] args) throws Exception {
- SpringApplication.run(SpringBootWebApplication.class, args);
- }
- }
然后controller
- package com.mkyong;
- import java.util.Map;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class WelcomeController {
- // inject via application.properties
- @Value("${welcome.message:test}")
- private String message = "Hello World";
- @RequestMapping("/")
- public String welcome(Map<String, Object> model) {
- model.put("message", this.message);
- return "welcome";
- }
- }
总结:
1.servlet的本质没有变化,从web框架的发展来看,web框架只是简化了开发servlet的工作,但还是遵循servlet规范的发展而发展的。
2.servlet的历史发展,从配置方式向编程方式到自动配置方式发展
3.spring mvc框架的分组:root和child(可以有多个dispatcherservlet),多个child可以共享root,child直接不共享
参考文献:
【1】https://en.wikipedia.org/wiki/Web_container
【2】https://baike.baidu.com/item/servlet/477555?fr=aladdin
【3】https://www.javatpoint.com/servlet-tutorial
【4】https://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#deployment-descriptor
【5】https://blog.csdn.net/qq_22075041/article/details/78692780
【6】http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example/
【7】http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/
(编辑:ASP站长网)
|