Servlet和springMVC

来源:https://www.cnblogs.com/july7/archive/2023/03/25/17255981.html
-Advertisement-
Play Games

什麼是Servlet? Servlet是使用Java語言編寫的運行在伺服器端的程式。狹義的Servlet是指Java語言實現的一個介面,廣義的Servlet是指任何實現了這個Servlet介面的類,一般情況下,人們將Servlet理解為後者。Servlet 主要用於處理客戶端傳來的 HTTP 請求, ...


什麼是Servlet?

  1. Servlet是使用Java語言編寫的運行在伺服器端的程式。狹義的Servlet是指Java語言實現的一個介面,廣義的Servlet是指任何實現了這個Servlet介面的類,一般情況下,人們將Servlet理解為後者。Servlet 主要用於處理客戶端傳來的 HTTP 請求,並返回一個響應,它能夠處理的請求有doGet()和doPost()等方法
  2. Servlet由Servlet容器提供,所謂的Servlet容器是指提供了Servlet 功能的伺服器(本書中指Tomcat),Servlet容器將Servlet動態地載入到伺服器上。與HTTP 協議相關的Servlet使用HTTP請求和HTTP響應與客戶端進行交互。因此,Servlet容器支持所有HTTP協議的請求和響應。Servlet應用程式的體繫結構如圖所示。<<javaWeb程式設計教程>>

什麼是SpringMVC?

  1. Spring MVC一開始就定位於一個較為鬆散的組合,展示給用戶的視圖(View)、控制器返回的數據模型(Model)、定位視圖的視圖解析器(ViewResolver)和處理適配器(HandlerAdapter)等內容都是獨立的。換句話說,通過Spring MVC很容易把後臺的數據轉換為各種類型的數據,以滿足移動互聯網數據多樣化的要求。例如,Spring MVC可以十分方便地轉換為目前最常用的JSON數據集,也可以轉換為PDF、Excel和XML等。加之Spring MVC是基於Spring基礎框架派生出來的Web框架,所以它天然就可以十分方便地整合到Spring框架中,而Spring整合Struts2還是比較繁複的.
  2. mvc架構設計:處理請求先到達控制器(Controller),控制器的作用是進行請求分發,這樣它會根據請求的內容去訪問模型層(Model);在現今互聯網系統中,數據主要從資料庫和NoSQL中來,而且對於資料庫而言往往還存在事務的機制,為了適應這樣的變化,設計者會把模型層再細分為兩層,即服務層(Service)和數據訪問層(DAO);當控制器獲取到由模型層返回的數據後,就將數據渲染到視圖中,這樣就能夠展現給用戶了
  3.  

     圖取自於<<深入淺出springboot2.x>>書籍

 

思考和疑問

早些年的時候,使用servlet開發web程式, 一般都是繼承HttpServlet介面,請求訪問時直接根據類名調用.但這樣寫的結果是,一個類只能處理一個請求.項目結構大概長這樣

使用SpringMVC框架後,只需要配置簡單的@RequestMapping("")就可以找到對應的方法,原來的servlet呢? 對springMVC的瞭解還是不夠詳細,所以

繼續探討以下幾個問題

1.SpringMVC如何代替Servlet?

 

 

 可以看到DispatcherServlet繼承自HttpServlet,  前端控制器其實就相當於一個Servlet.

 2. SpringMVC的工作流程?

 

圖取自於<<深入淺出springboot2.x>>書籍

基於springboot開發使得SpringMVC的使用更為簡便,可以通過Spring Boot的配置來定製這些組件的初始化

  1. /*
     * Copyright 2002-2019 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      https://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package org.springframework.web.servlet;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    import java.util.stream.Collectors;
    
    import javax.servlet.DispatcherType;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import org.springframework.beans.factory.BeanFactoryUtils;
    import org.springframework.beans.factory.BeanInitializationException;
    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.i18n.LocaleContext;
    import org.springframework.core.annotation.AnnotationAwareOrderComparator;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    import org.springframework.core.log.LogFormatUtils;
    import org.springframework.http.server.ServletServerHttpRequest;
    import org.springframework.lang.Nullable;
    import org.springframework.ui.context.ThemeSource;
    import org.springframework.util.ClassUtils;
    import org.springframework.util.StringUtils;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.request.ServletWebRequest;
    import org.springframework.web.context.request.async.WebAsyncManager;
    import org.springframework.web.context.request.async.WebAsyncUtils;
    import org.springframework.web.multipart.MultipartException;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.springframework.web.multipart.MultipartResolver;
    import org.springframework.web.util.NestedServletException;
    import org.springframework.web.util.WebUtils;
    
    /**
     * Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers
     * or HTTP-based remote service exporters. Dispatches to registered handlers for processing
     * a web request, providing convenient mapping and exception handling facilities.
     *
     * <p>This servlet is very flexible: It can be used with just about any workflow, with the
     * installation of the appropriate adapter classes. It offers the following functionality
     * that distinguishes it from other request-driven web MVC frameworks:
     *
     * <ul>
     * <li>It is based around a JavaBeans configuration mechanism.
     *
     * <li>It can use any {@link HandlerMapping} implementation - pre-built or provided as part
     * of an application - to control the routing of requests to handler objects. Default is
     * {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping} and
     * {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping}.
     * HandlerMapping objects can be defined as beans in the servlet's application context,
     * implementing the HandlerMapping interface, overriding the default HandlerMapping if
     * present. HandlerMappings can be given any bean name (they are tested by type).
     *
     * <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface.
     * Default adapters are {@link org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter},
     * {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter}, for Spring's
     * {@link org.springframework.web.HttpRequestHandler} and
     * {@link org.springframework.web.servlet.mvc.Controller} interfaces, respectively. A default
     * {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter}
     * will be registered as well. HandlerAdapter objects can be added as beans in the
     * application context, overriding the default HandlerAdapters. Like HandlerMappings,
     * HandlerAdapters can be given any bean name (they are tested by type).
     *
     * <li>The dispatcher's exception resolution strategy can be specified via a
     * {@link HandlerExceptionResolver}, for example mapping certain exceptions to error pages.
     * Default are
     * {@link org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver},
     * {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver}, and
     * {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}.
     * These HandlerExceptionResolvers can be overridden through the application context.
     * HandlerExceptionResolver can be given any bean name (they are tested by type).
     *
     * <li>Its view resolution strategy can be specified via a {@link ViewResolver}
     * implementation, resolving symbolic view names into View objects. Default is
     * {@link org.springframework.web.servlet.view.InternalResourceViewResolver}.
     * ViewResolver objects can be added as beans in the application context, overriding the
     * default ViewResolver. ViewResolvers can be given any bean name (they are tested by type).
     *
     * <li>If a {@link View} or view name is not supplied by the user, then the configured
     * {@link RequestToViewNameTranslator} will translate the current request into a view name.
     * The corresponding bean name is "viewNameTranslator"; the default is
     * {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}.
     *
     * <li>The dispatcher's strategy for resolving multipart requests is determined by a
     * {@link org.springframework.web.multipart.MultipartResolver} implementation.
     * Implementations for Apache Commons FileUpload and Servlet 3 are included; the typical
     * choice is {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}.
     * The MultipartResolver bean name is "multipartResolver"; default is none.
     *
     * <li>Its locale resolution strategy is determined by a {@link LocaleResolver}.
     * Out-of-the-box implementations work via HTTP accept header, cookie, or session.
     * The LocaleResolver bean name is "localeResolver"; default is
     * {@link org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}.
     *
     * <li>Its theme resolution strategy is determined by a {@link ThemeResolver}.
     * Implementations for a fixed theme and for cookie and session storage are included.
     * The ThemeResolver bean name is "themeResolver"; default is
     * {@link org.springframework.web.servlet.theme.FixedThemeResolver}.
     * </ul>
     *
     * <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a
     * corresponding {@code HandlerMapping} (for type-level annotations) and/or
     * {@code HandlerAdapter} (for method-level annotations) is present in the dispatcher.</b>
     * This is the case by default. However, if you are defining custom {@code HandlerMappings}
     * or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
     * {@code RequestMappingHandlerMapping} and/or {@code RequestMappingHandlerAdapter}
     * is defined as well - provided that you intend to use {@code @RequestMapping}.
     *
     * <p><b>A web application can define any number of DispatcherServlets.</b>
     * Each servlet will operate in its own namespace, loading its own application context
     * with mappings, handlers, etc. Only the root application context as loaded by
     * {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
     *
     * <p>As of Spring 3.1, {@code DispatcherServlet} may now be injected with a web
     * application context, rather than creating its own internally. This is useful in Servlet
     * 3.0+ environments, which support programmatic registration of servlet instances.
     * See the {@link #DispatcherServlet(WebApplicationContext)} javadoc for details.
     *
     * @author Rod Johnson
     * @author Juergen Hoeller
     * @author Rob Harrop
     * @author Chris Beams
     * @author Rossen Stoyanchev
     * @see org.springframework.web.HttpRequestHandler
     * @see org.springframework.web.servlet.mvc.Controller
     * @see org.springframework.web.context.ContextLoaderListener
     */
    @SuppressWarnings("serial")
    public class DispatcherServlet extends FrameworkServlet {
    
        /** Well-known name for the MultipartResolver object in the bean factory for this namespace. */
        public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
    
        /** Well-known name for the LocaleResolver object in the bean factory for this namespace. */
        public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver";
    
        /** Well-known name for the ThemeResolver object in the bean factory for this namespace. */
        public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver";
    
        /**
         * Well-known name for the HandlerMapping object in the bean factory for this namespace.
         * Only used when "detectAllHandlerMappings" is turned off.
         * @see #setDetectAllHandlerMappings
         */
        public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping";
    
        /**
         * Well-known name for the HandlerAdapter object in the bean factory for this namespace.
         * Only used when "detectAllHandlerAdapters" is turned off.
         * @see #setDetectAllHandlerAdapters
         */
        public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter";
    
        /**
         * Well-known name for the HandlerExceptionResolver object in the bean factory for this namespace.
         * Only used when "detectAllHandlerExceptionResolvers" is turned off.
         * @see #setDetectAllHandlerExceptionResolvers
         */
        public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver";
    
        /**
         * Well-known name for the RequestToViewNameTranslator object in the bean factory for this namespace.
         */
        public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator";
    
        /**
         * Well-known name for the ViewResolver object in the bean factory for this namespace.
         * Only used when "detectAllViewResolvers" is turned off.
         * @see #setDetectAllViewResolvers
         */
        public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver";
    
        /**
         * Well-known name for the FlashMapManager object in the bean factory for this namespace.
         */
        public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager";
    
        /**
         * Request attribute to hold the current web application context.
         * Otherwise only the global web app context is obtainable by tags etc.
         * @see org.springframework.web.servlet.support.RequestContextUtils#findWebApplicationContext
         */
        public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT";
    
        /**
         * Request attribute to hold the current LocaleResolver, retrievable by views.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getLocaleResolver
         */
        public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE_RESOLVER";
    
        /**
         * Request attribute to hold the current ThemeResolver, retrievable by views.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getThemeResolver
         */
        public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_RESOLVER";
    
        /**
         * Request attribute to hold the current ThemeSource, retrievable by views.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getThemeSource
         */
        public static final String THEME_SOURCE_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_SOURCE";
    
        /**
         * Name of request attribute that holds a read-only {@code Map<String,?>}
         * with "input" flash attributes saved by a previous request, if any.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getInputFlashMap(HttpServletRequest)
         */
        public static final String INPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".INPUT_FLASH_MAP";
    
        /**
         * Name of request attribute that holds the "output" {@link FlashMap} with
         * attributes to save for a subsequent request.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getOutputFlashMap(HttpServletRequest)
         */
        public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP";
    
        /**
         * Name of request attribute that holds the {@link FlashMapManager}.
         * @see org.springframework.web.servlet.support.RequestContextUtils#getFlashMapManager(HttpServletRequest)
         */
        public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER";
    
        /**
         * Name of request attribute that exposes an Exception resolved with a
         * {@link HandlerExceptionResolver} but where no view was rendered
         * (e.g. setting the status code).
         */
        public static final String EXCEPTION_ATTRIBUTE = DispatcherServlet.class.getName() + ".EXCEPTION";
    
        /** Log category to use when no mapped handler is found for a request. */
        public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound";
    
        /**
         * Name of the class path resource (relative to the DispatcherServlet class)
         * that defines DispatcherServlet's default strategy names.
         */
        private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties";
    
        /**
         * Common prefix that DispatcherServlet's default strategy attributes start with.
         */
        private static final String DEFAULT_STRATEGIES_PREFIX = "org.springframework.web.servlet";
    
        /** Additional logger to use when no mapped handler is found for a request. */
        protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);
    
        private static final Properties defaultStrategies;
    
        static {
            // Load default strategy implementations from properties file.
            // This is currently strictly internal and not meant to be customized
            // by application developers.
            try {
                ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
                defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
            }
            catch (IOException ex) {
                throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());
            }
        }
    
        /** Detect all HandlerMappings or just expect "handlerMapping" bean?. */
        private boolean detectAllHandlerMappings = true;
    
        /** Detect all HandlerAdapters or just expect "handlerAdapter" bean?. */
        private boolean detectAllHandlerAdapters = true;
    
        /** Detect all HandlerExceptionResolvers or just expect "handlerExceptionResolver" bean?. */
        private boolean detectAllHandlerExceptionResolvers = true;
    
        /** Detect all ViewResolvers or just expect "viewResolver" bean?. */
        private boolean detectAllViewResolvers = true;
    
        /** Throw a NoHandlerFoundException if no Handler was found to process this request? *.*/
        private boolean throwExceptionIfNoHandlerFound = false;
    
        /** Perform cleanup of request attributes after include request?. */
        private boolean cleanupAfterInclude = true;
    
        /** MultipartResolver used by this servlet. */
        @Nullable
        private MultipartResolver multipartResolver;
    
        /** LocaleResolver used by this servlet. */
        @Nullable
        private LocaleResolver localeResolver;
    
        /** ThemeResolver used by this servlet. */
        @Nullable
        private ThemeResolver themeResolver;
    
        /** List of HandlerMappings used by this servlet. */
        @Nullable
        private List<HandlerMapping> handlerMappings;
    
        /** List of HandlerAdapters used by this servlet. */
        @Nullable
        private List<HandlerAdapter> handlerAdapters;
    
        /** List of HandlerExceptionResolvers used by this servlet. */
        @Nullable
        private List<HandlerExceptionResolver> handlerExceptionResolvers;
    
        /** RequestToViewNameTranslator used by this servlet. */
        @Nullable
        private RequestToViewNameTranslator viewNameTranslator;
    
        /** FlashMapManager used by this servlet. */
        @Nullable
        private FlashMapManager flashMapManager;
    
        /** List of ViewResolvers used by this servlet. */
        @Nullable
        private List<ViewResolver> viewResolvers;
    
    
        /**
         * Create a new {@code DispatcherServlet} that will create its own internal web
         * application context based on defaults and values provided through servlet
         * init-params. Typically used in Servlet 2.5 or earlier environments, where the only
         * option for servlet registration is through {@code web.xml} which requires the use
         * of a no-arg constructor.
         * <p>Calling {@link #setContextConfigLocation} (init-param 'contextConfigLocation')
         * will dictate which XML files will be loaded by the
         * {@linkplain #DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext}
         * <p>Calling {@link #setContextClass} (init-param 'contextClass') overrides the
         * default {@code XmlWebApplicationContext} and allows for specifying an alternative class,
         * such as {@code AnnotationConfigWebApplicationContext}.
         * <p>Calling {@link #setContextInitializerClasses} (init-param 'contextInitializerClasses')
         * indicates which {@code ApplicationContextInitializer} classes should be used to
         * further configure the internal application context prior to refresh().
         * @see #DispatcherServlet(WebApplicationContext)
         */
        public DispatcherServlet() {
            super();
            setDispatchOptionsRequest(true);
        }
    
        /**
         * Create a new {@code DispatcherServlet} with the given web application context. This
         * constructor is useful in Servlet 3.0+ environments where instance-based registration
         * of servlets is possible through the {@link ServletContext#addServlet} API.
         * <p>Using this constructor indicates that the following properties / init-params
         * will be ignored:
         * <ul>
         * <li>{@link #setContextClass(Class)} / 'contextClass'</li>
         * <li>{@link #setContextConfigLocation(String)} / 'contextConfigLocation'</li>
         * <li>{@link #setContextAttribute(String)} / 'contextAttribute'</li>
         * <li>{@link #setNamespace(String)} / 'namespace'</li>
         * </ul>
         * <p>The given web application context may or may not yet be {@linkplain
         * ConfigurableApplicationContext#refresh() refreshed}. If it has <strong>not</strong>
         * already been refreshed (the recommended approach), then the following will occur:
         * <ul>
         * <li>If the given context does not already have a {@linkplain
         * ConfigurableApplicationContext#setParent parent}, the root application context
         * will be set as the parent.</li>
         * <li>If the given context has not already been assigned an {@linkplain
         * ConfigurableApplicationContext#setId id}, one will be assigned to it</li>
         * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to
         * the application context</li>
         * <li>{@link #postProcessWebApplicationContext} will be called</li>
         * <li>Any {@code ApplicationContextInitializer}s specified through the
         * "contextInitializerClasses" init-param or through the {@link
         * #setContextInitializers} property will be applied.</li>
         * <li>{@link ConfigurableApplicationContext#refresh refresh()} will be called if the
         * context implements {@link ConfigurableApplicationContext}</li>
         * </ul>
         * If the context has already been refreshed, none of the above will occur, under the
         * assumption that the user has performed these actions (or not) per their specific
         * needs.
         * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
         * @param webApplicationContext the context to use
         * @see #initWebApplicationContext
         * @see #configureAndRefreshWebApplicationContext
         * @see org.springframework.web.WebApplicationInitializer
         */
        public DispatcherServlet(WebApplicationContext webApplicationContext) {
            super(webApplicationContext);
            setDispatchOptionsRequest(true);
        }
    
    
        /**
         * Set whether to detect all HandlerMapping beans in this servlet's context. Otherwise,
         * just a single bean with name "handlerMapping" will be expected.
         * <p>Default is "true". Turn this off if you want this servlet to use a single
         * HandlerMapping, despite multiple HandlerMapping beans being defined in the context.
         */
        public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings) {
            this.detectAllHandlerMappings = detectAllHandlerMappings;
        }
    
        /**
         * Set whether to detect all HandlerAdapter beans in this servlet's context. Otherwise,
         * just a single bean with name "handlerAdapter" will be expected.
         * <p>Default is "true". Turn this off if you want this servlet to use a single
         * HandlerAdapter, despite multiple HandlerAdapter beans being defined in the context.
         */
        public void setDetectAllHandlerAdapters(boolean detectAllHandlerAdapters) {
            this.detectAllHandlerAdapters = detectAllHandlerAdapters;
        }
    
        /**
         * Set whether to detect all HandlerExceptionResolver beans in this servlet's context. Otherwise,
         * just a single bean with name "handlerExceptionResolver" will be expected.
         * <p>Default is "true". Turn this off if you want this servlet to use a single
         * HandlerExceptionResolver, despite multiple HandlerExceptionResolver beans being defined in the context.
         */
        public void setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers) {
            this.detectAllHandlerExceptionResolvers = detectAllHandlerExceptionResolvers;
        }
    
        /**
         * Set whether to detect all ViewResolver beans in this servlet's context. Otherwise,
         * just a single bean with name "viewResolver" will be expected.
         * <p>Default is "true". Turn this off if you want this servlet to use a single
         * ViewResolver, despite multiple ViewResolver beans being defined in the context.
         */
        public void setDetectAllViewResolvers(boolean detectAllViewResolvers) {
            this.detectAllViewResolvers = detectAllViewResolvers;
        }
    
        /**
         * Set whether to throw a NoHandlerFoundException when no Handler was found for this request.
         * This exception can then be caught with a HandlerExceptionResolver or an
         * {@code @ExceptionHandler} controller method.
         * <p>Note that if {@link org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler}
         * is used, then requests will always be forwarded to the default servlet and a
         * NoHandlerFoundException would never be thrown in that case.
         * <p>Default is "false", meaning the DispatcherServlet sends a NOT_FOUND error through the
         * Servlet response.
         * @since 4.0
         */
        public void setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound) {
            this.throwExceptionIfNoHandlerFound = throwExceptionIfNoHandlerFound;
        }
    
        /**
         * Set whether to perform cleanup of request attributes after an include request, that is,
         * whether to reset the original state of all request attributes after the DispatcherServlet
         * has processed within an include request. Otherwise, just the DispatcherServlet's own
         * request attributes will be reset, but not model attributes for JSPs or special attributes
         * set by views (for example, JSTL's).
         * <p>Default is "true", which is strongly recommended. Views should not rely on request attributes
         * having been set by (dynamic) includes. This allows JSP views rendered by an included controller
         * to use any model attributes, even with the same names as in the main JSP, without causing side
         * effects. Only turn this off for special needs, for example to deliberately allow main JSPs to
         * access attributes from JSP views rendered by an included controller.
         */
        public void setCleanupAfterInclude(boolean cleanupAfterInclude) {
            this.cleanupAfterInclude = cleanupAfterInclude;
        }
    
    
        /**
         * This implementation calls {@link #initStrategies}.
         */
        @Override
        protected void onRefresh(ApplicationContext context) {
            initStrategies(context);
        }
    
        /**
         * Initialize the strategy objects that this servlet uses.
         * <p>May be overridden in subclasses in order to initialize further strategy objects.
         */
        protected void initStrategies(ApplicationContext context) {
            initMultipartResolver(context);
            initLocaleResolver(context);
            initThemeResolver(context);
            initHandlerMappings(context);
            initHandlerAdapters(context);
            initHandlerExceptionResolvers(context);
            initRequestToViewNameTranslator(context);
            initViewResolvers(context);
            initFlashMapManager(context);
        }
    
        /**
         * Initialize the MultipartResolver used by this class.
         * <p>If no bean is defined with the given name in the BeanFactory for this namespace,
         * no multipart handling is provided.
         */
        private void initMultipartResolver(ApplicationContext context) {
            try {
                this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
                if (logger.isTraceEnabled()) {
                    logger.trace("Detected " + this.multipartResolver);
                }
                else if (logger.isDebugEnabled()) {
                    logger.debug("Detected " + this.multipartResolver.getClass().getSimpleName());
                }
            }
            catch (NoSuchBeanDefinitionException ex) {
                // Default is no multipart resolver.
                this.multipartResolver = null;
                if (logger.isTraceEnabled()) {
                    logger.trace("No MultipartResolver '" + MULTIPART_RESOLVER_BEAN_NAME + "' declared");
                }
            }
        }
    
        /**
         * Initialize the LocaleResolver used by this class.
         * <p>If no bean is defined with the given name in the BeanFactory for this namespace,
         * we default to AcceptHeaderLocaleResolver.
         */
        private void initLocaleResolver(ApplicationContext context) {
            try {
                this.localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);
                if (logger.isTraceEnabled()) {
                    logger.trace("Detected " + this.localeResolver);
                }
                else if (logger.isDebugEnabled()) {
                    logger.debug("Detected " + this.localeResolver.getClass().getSimpleName());
                }
            }
            catch (NoSuchBeanDefinitionException ex) {
                // We need to use the default.
                  
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 定義 代理是一個中間者的角色,如生活中的中介,出於種種考慮/限制,一個對象不能直接訪問另一個對象,需要一個第三者(中間代理)牽線搭橋從而間接達到訪問目的,這樣的就是代理模式。 es6 中的代理 es6 的 proxy 就是上面說的代理模式的實現,es6 幫我們在語法層面提供了這個新的api,讓我們可 ...
  • 使用工具: IDEA2022 Tomcat9.0.4 1.下載Tomcat: 官網:https://tomcat.apache.org/ 找到需要的版本下載即可,下載完成解壓即可用: Tomcat目錄介紹: 1.1.Tomcat啟動、關閉。卸載: 啟動:雙擊bin\startup.bat 關閉:直接 ...
  • 委托模式(Delegation pattern):將一個對象的某個方法委托給另一個對象來執行,它可以幫助我們將對象之間的關係更加靈活地組織起來,從而提高代碼的可維護性和復用性。 在委托模式中,一個對象(稱為委托對象)將一些特定的任務委托給另一個對象(稱為代理對象)來執行。代理對象通常具有和委托對象相 ...
  • 1.聲明與變數 let聲明的變數可以多次賦值 let 變數名 = 值; const修飾叫常量,只能賦值一次,但是引用的值可以改變 var聲明的變數可以多次賦值 結論:能用let不用var ,因為作用域的問題 2.基本類型和對象類型 undefined 和 null undefined 指 未定義的對 ...
  • 代理模式(Proxy Pattern)是一種結構型設計模式,結構型模式描述如何將類或對象按某種佈局組成更大的結構。它允許你提供一個代理對象來控制對另一個對象的訪問。代理對象擁有與實際對象相同的介面,因此它可以被用來代替實際對象。 ...
  • 在軟體行業,對於什麼是架構,都有很多的爭論,每個人都有自己的理解。在不同的書籍上, 不同的作者, 對於架構的定義也不統一, 角度不同, 定義不同。此君說的架構和彼君理解的架構未必是一回事。因此我們在討論架構之前,我們先討論架構的概念定義, 因為概念是人認識這個世界的基礎和用來溝通的手段,如果對架構概... ...
  • 超級大的數做加減乘除 java有八大數據類型: 1、byte(位),最大存儲數據量是255; 2、short(短整數),最大數據存儲量是65536; 3、int(整數),最大數據存儲容量是2的32次方減1; 4、long(長整數),最大數據存儲容量是2的64次方減1; 5、float(單精度浮動數) ...
  • Java已經誕生20多年了,依然是企業級開發中使用最廣泛的語言,也是挨罵最多的語言。Java廣受批評的四個缺點是:性能差、記憶體消耗大、GUI弱、代碼啰嗦,我們應該如何看待這幾個問題呢?在微服務的背景下,提倡圍繞業務能力而非技術來構建應用,允許由不同的語言構建應用程式。一個超大的集群,往往有上萬個微服... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...