zoukankan      html  css  js  c++  java
  • Spring文档阅读之Web on Servlet Stack

    1.Spring Web MVC

    Spring MVC是基于Servlet API开发的原生web框架,其名字来源于在Spring项目的模块名称(spring-webmvc)。

    Spring还推出了基于reactive栈的web框架 Spring WebFlux。
    Parallel to Spring Web MVC, Spring Framework 5.0 introduced a reactive-stack web framework whose name, “Spring WebFlux,” is also based on its source module (spring-webflux). This section covers Spring Web MVC. The next section covers Spring WebFlux.

    1.1. DispatcherServlet

    Spring MVC围绕一个Servlet(DispatcherServlet)设计,提供对于不同请求的分享算法,实际的工作由框架配置的组件完成。

    DispatcherServlet需要通过Java代码或者web.xml进行配置,同时DispatcherServlet通过Sprong配置来发现业务组如请求路由匹配,视图解析器,异常处理等等。

    以下代码示范了如何通过代码注册并初始化一个DispatcherServlet,它会被Servlet容器自动检测到:

    public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletCxt) {
    
        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
       // You need a Spring context configuration class name APPConfig.class
        ac.register(AppConfig.class);
        ac.refresh();
    
        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");
    }
    

    }
    可以使用web.xml配置DispatcherServlet:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- you need a Spring context configuration file name app-context.xml -->
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>
    
    <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.Spring Boot 有哪些优点?
    3.什么是 JavaConfig?
    4.如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    Java中的异常处理机制的简单原理和应用。
    垃圾回收的优点和原理。并考虑2种回收机制。
    我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
    Request对象的主要方法:
    JSP的内置对象及方法。
    Servlet执行时一般实现哪几个方法?
    说说你所熟悉或听说过的j2ee中的几种常用模式?及对设计模式的一些看法
  • 原文地址:https://www.cnblogs.com/Simon-cat/p/10116415.html
Copyright © 2011-2022 走看看