springMVC学习之-------DispatcherServlet类

springMVC学习之-DispatcherServlet类

Dispatcher类结构如下:

  • 类结构
  • 属性
  • 方法

类结构

DispatcherServlet作为主控制器,它的继承结构是FrameworkServlet-->HttpServletBean-->HttpServlet-->GenericServlet

public class DispatcherServlet extends FrameworkServlet{}

public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware{}

public abstract class HttpServletBean extends HttpServlet
implements EnvironmentCapable, EnvironmentAware {}

public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable{}

属性

属性也描述了具体的功能,包含上传multipartResolver,国际化localeResolver,主题themeResolver,映射handlerMapping,适配handlerAdapter,映射异常handlerExceptionResolver,视图名称转换viewNameTranslator,视图解析viewResolver,flashMapManager
/* 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#getWebApplicationContext
 */
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 an
 * {@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";


/** 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 ‘DispatcherServlet.properties’: ” + ex.getMessage());
}
}

main method

protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
private void initMultipartResolver(ApplicationContext context) {
try {
this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
if (logger.isDebugEnabled()) {
logger.debug(“Using MultipartResolver [” + this.multipartResolver + “]”);
}
}
catch (NoSuchBeanDefinitionException ex) {
// Default is no multipart resolver.
this.multipartResolver = null;
if (logger.isDebugEnabled()) {
logger.debug(“Unable to locate MultipartResolver with name ‘” + MULTIPART_RESOLVER_BEAN_NAME +
“’: no multipart request handling provided”);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值