📄 dispatcherservlet.java
字号:
/*
* Copyright 2002-2004 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
*
* http://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.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
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.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.OrderComparator;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
import org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter;
import org.springframework.web.servlet.theme.FixedThemeResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* Concrete front controller for use within the web MVC framework.
* Dispatches to registered handlers for processing a web request.
*
* <p>This class and the MVC approach it delivers is discussed in Chapter 12 of
* <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>
* by Rod Johnson (Wrox, 2002).
*
* <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 MVC frameworks:
*
* <ul>
* <li>It is based around a JavaBeans configuration mechanism.
*
* <li>It can use any HandlerMapping implementation - whether standard, or provided
* as part of an application - to control the routing of requests to handler objects.
* Default is BeanNameUrlHandlerMapping. HandlerMapping objects can be define as beans
* in the servlet's application context that implement the HandlerMapping interface.
* HandlerMappings can be given any bean name (they are tested by type).
*
* <li>It can use any HandlerAdapter; this allows to use any handler interface.
* Default is SimpleControllerHandlerAdapter, for Spring's Controller interface.
* Additional HandlerAdapter objects can be added through the application context.
* Like HandlerMappings, HandlerAdapters can be given any bean name (tested by type).
*
* <li>Its exception resolution strategy can be specified via a HandlerExceptionResolver,
* for example mapping certain exceptions to error pages. Default is none.
* Additional HandlerExceptionResolvers can be added through the application context.
* HandlerExceptionResolver can be given any bean name (tested by type).
*
* <li>Its view resolution strategy can be specified via a ViewResolver implementation,
* resolving symbolic view names into View objects. Default is InternalResourceViewResolver.
* Additional ViewResolver objects can be added through the application context.
* ViewResolvers can be given any bean name (tested by type).
*
* <li>Its strategy for resolving multipart requests is determined by a MultipartResolver
* implementation. Implementations for Jakarta Commons FileUpload and Jason Hunter's COS
* are included. The MultipartResolver bean name is "multipartResolver"; default is none.
*
* <li>Its locale resolution strategy is determined by a LocaleResolver implementation.
* Out-of-the-box implementations work via HTTP accept header, cookie, or session.
* The LocaleResolver bean name is "localeResolver"; default is AcceptHeaderLocaleResolver.
*
* <li>Its theme resolution strategy is determined by a ThemeResolver implementation.
* Implementations for a fixed theme and for cookie and session storage are included.
* The ThemeResolver bean name is "themeResolver"; default is FixedThemeResolver.
* </ul>
*
* <p>A web application can use any number of dispatcher servlets. Each servlet will
* operate in its own namespace. Only the root application context will be shared.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see HandlerMapping
* @see HandlerAdapter
* @see ViewResolver
* @see MultipartResolver
* @see LocaleResolver
* @see ThemeResolver
* @see org.springframework.web.context.WebApplicationContext
* @see org.springframework.web.context.ContextLoaderListener
*/
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 #setDetectAllViewResolvers
*/
public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping";
/**
* Well-known name for the HandlerExceptionResolver object in the bean factory for this
* namespace. Only used when "detectAllHandlerExceptionResolvers" is turned off.
* @see #setDetectAllViewResolvers
*/
public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver";
/**
* 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";
/**
* Request attribute to hold current web application context.
* Otherwise only the global web app context is obtainable by tags etc.
*/
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT";
/**
* Request attribute to hold current multipart resolver, retrievable by views/binders.
* @see org.springframework.web.servlet.support.RequestContextUtils
*/
public static final String MULTIPART_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".MULTIPART";
/**
* Request attribute to hold current locale, retrievable by views.
* @see org.springframework.web.servlet.support.RequestContext
*/
public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE";
/**
* Request attribute to hold current theme, retrievable by views.
* @see org.springframework.web.servlet.support.RequestContext
*/
public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME";
/**
* 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";
/**
* 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);
/** Detect all HandlerMappings or just expect "handlerMapping" bean? */
private boolean detectAllHandlerMappings = 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;
/** MultipartResolver used by this servlet */
private MultipartResolver multipartResolver;
/** LocaleResolver used by this servlet */
private LocaleResolver localeResolver;
/** ThemeResolver used by this servlet */
private ThemeResolver themeResolver;
/** List of HandlerMappings used by this servlet */
private List handlerMappings;
/** List of HandlerAdapters used by this servlet */
private List handlerAdapters;
/** List of HandlerExceptionResolvers used by this servlet */
private List handlerExceptionResolvers;
/** List of ViewResolvers used by this servlet */
private List viewResolvers;
/**
* Set whether to detect all HandlerMapping beans in this servlet's context.
* Else, 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 HandlerExceptionResolver beans in this servlet's context.
* Else, 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.
* Else, 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;
}
/**
* Overridden method, invoked after any bean properties have been set and the
* WebApplicationContext and BeanFactory for this namespace is available.
* <p>Loads HandlerMapping and HandlerAdapter objects, and configures a
* ViewResolver and a LocaleResolver.
*/
protected void initFrameworkServlet() throws ServletException, BeansException {
initMultipartResolver();
initLocaleResolver();
initThemeResolver();
initHandlerMappings();
initHandlerAdapters();
initHandlerExceptionResolvers();
initViewResolvers();
}
/**
* Initialize the MultipartResolver used by this class.
* If no bean is defined with the given name in the BeanFactory
* for this namespace, no multipart handling is provided.
*/
private void initMultipartResolver() throws BeansException {
try {
this.multipartResolver =
(MultipartResolver) getWebApplicationContext().getBean(MULTIPART_RESOLVER_BEAN_NAME);
if (logger.isInfoEnabled()) {
logger.info("Using MultipartResolver [" + this.multipartResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// default is no multipart resolver
this.multipartResolver = null;
if (logger.isInfoEnabled()) {
logger.info("Unable to locate MultipartResolver with name [" + MULTIPART_RESOLVER_BEAN_NAME +
"]: no multipart handling provided");
}
}
}
/**
* Initialize the LocaleResolver used by this class.
* If no bean is defined with the given name in the BeanFactory
* for this namespace, we default to AcceptHeaderLocaleResolver.
*/
private void initLocaleResolver() throws BeansException {
try {
this.localeResolver = (LocaleResolver) getWebApplicationContext().getBean(LOCALE_RESOLVER_BEAN_NAME);
if (logger.isInfoEnabled()) {
logger.info("Using LocaleResolver [" + this.localeResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// we need to use the default
this.localeResolver = new AcceptHeaderLocaleResolver();
if (logger.isInfoEnabled()) {
logger.info("Unable to locate LocaleResolver with name '" + LOCALE_RESOLVER_BEAN_NAME +
"': using default [" + this.localeResolver + "]");
}
}
}
/**
* Initialize the ThemeResolver used by this class.
* If no bean is defined with the given name in the BeanFactory
* for this namespace, we default to a FixedThemeResolver.
*/
private void initThemeResolver() throws BeansException {
try {
this.themeResolver = (ThemeResolver) getWebApplicationContext().getBean(THEME_RESOLVER_BEAN_NAME);
if (logger.isInfoEnabled()) {
logger.info("Using ThemeResolver [" + this.themeResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// we need to use the default
this.themeResolver = new FixedThemeResolver();
if (logger.isInfoEnabled()) {
logger.info("Unable to locate ThemeResolver with name '" + THEME_RESOLVER_BEAN_NAME +
"': using default [" + this.themeResolver + "]");
}
}
}
/**
* Initialize the HandlerMappings used by this class.
* If no HandlerMapping beans are defined in the BeanFactory
* for this namespace, we default to BeanNameUrlHandlerMapping.
*/
private void initHandlerMappings() throws BeansException {
if (this.detectAllHandlerMappings) {
// find all HandlerMappings in the ApplicationContext,
// including ancestor contexts
Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
getWebApplicationContext(), HandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList(matchingBeans.values());
// we keep HandlerMappings in sorted order
Collections.sort(this.handlerMappings, new OrderComparator());
}
}
else {
try {
Object hm = getWebApplicationContext().getBean(HANDLER_MAPPING_BEAN_NAME);
this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
}
// Ensure we have at least one HandlerMapping, by registering
// a default HandlerMapping if no other mappings are found.
if (this.handlerMappings == null) {
BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
hm.setApplicationContext(getWebApplicationContext());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -