一、Spring DispatcherServlet 教程示例 在本教程中, 我们将学习 Spring 的DispatcherServlet类, 它的职责以及如何用示例来配置它。
二、DispatcherServlet是什么 DispatcherServlet充当基于 Spring 的 web 应用程序的前端控制器。它为请求处理提供了一种机制, 通过可配置的委托组件来执行实际工作。它是从javax.servlet.http.HttpServlet继承的, 它通常在web.xml文件中进行配置。
web 应用程序可以定义任意数量的DispatcherServlet实例。每个 servlet 都将在其自己的命名空间中运行, 并使用映射、处理程序等加载自己的应用程序上下文。只有ContextLoaderListener加载的根应用程序上下文 (如果有) 将被共享。在大多数情况下, 应用程序只具有上下文根 URL(/)的单个DispatcherServlet , 即所有进入该域的请求都将由它处理。 DispatcherServlet使用 Spring 配置类来发现它需要的委托组件, 如请求映射、视图解析、异常处理等。 三、如何使用 WebApplicationContext 在基于 Spring 的应用程序中, 我们的应用程序对象位于对象容器中。此容器在对象之间创建对象和关联, 并管理它们的完整生命周期。这些容器对象称为 spring 管理的 bean (或简单的 bean), 并且容器被称为应用程序上下文(通过类ApplicationContext) 在 spring 世界中。 WebApplicationContext是一个普通ApplicationContext的延伸。它是网络感知ApplicationContext , 它具有 Servlet 上下文信息。加载DispatcherServlet时, 它会查找WebApplicationContext的 bean 配置文件并对其进行初始化。 通过访问 Servlet 上下文, 任何实现ServletConextAware接口的 spring bean 都可以访问ServletContext实例并使用它进行许多操作。例如, 它可以获取上下文初始化参数, 获取上下文根信息, 并在 web 应用程序文件夹中获取资源位置 基于 XML 的 DispatcherServlet 配置 我们来看看一个典型的DispatcherServlet声明和初始化的样子。 web. xml
public class ApplicationInitializer implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/dispatcher-servlet-context.xml");
ServletRegistration.Dynamic registration = servletContext
.addServlet("rootDispatcher", new DispatcherServlet(appContext));
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
完整的基于 Java 的初始化
public class ApplicationInitializer 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);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
在上面的代码中, AppConfig和DispatcherConfig类定义了将在 web 应用程序上下文中使用的 spring 托管 bean。
Upon receiving a web request, DispatcherServlet performs a set of operations for request processing. It used a set of supporting beans for this. This table lists these default configured beans and their responsibilities –
Bean
Responsibilities
HandlerMapping
Maps incoming web requests to handlers and pre- and post-processors
HandlerAdapter
Invokes the handler which resolves arguments and dependencies, such as annotated arguments for URL-mapped controller method endpoints
HandlerExceptionResolver
Allows programmatic handling of exceptions and maps exceptions to views
ViewResolver
Resolves logical view names to view instances
LocaleResolver
Resolves the client’s locale in order to enable internationalization
LocaleContextResolver
A richer extension of LocaleResolver, with timezone information
ThemeResolver
Resolves themes configured in your app for enhanced user experience
MultipartResolver
Handles multipart file uploads as part of HTTP requests
FlashMapManager
Manages FlashMap instances that store temporary Flash attributes between requests redirected from one another
遇到一个奇怪的问题,当SplashActivity跳转到MainActivity之后,按主页键,再去打开程序,程序没法再打开(闪一下),结束任务再开也是这样,只能卸载了再重装。而且每次在Log里都打印了这句话"进入主程序"。后来发现是必须跳转之后再finish掉SplashActivity
本来代码:
// 销毁这个Activity
fin
Kafka is a distributed, partitioned, replicated commit log service.这里的commit log如何理解?
A message is considered "committed" when all in sync replicas for that partition have applied i
安装lua_nginx_module 模块
lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty
Centos和debian的安装就简单了。。
这里说下freebsd的安装:
fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
ma
今天看Netty如何实现一个Http Server
org.jboss.netty.example.http.file.HttpStaticFileServerPipelineFactory:
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast(&quo
环境:Windows XPPHP Version 5.2.9MySQL Server 5.1
第一步、创建一个表date_test(非定长、int时间)
CREATE TABLE `test`.`date_test` (`id` INT NOT NULL AUTO_INCREMENT ,`start_time` INT NOT NULL ,`some_content`
在两个activity直接传递List<xxInfo>时,出现Parcel: unable to marshal value异常。 在MainActivity页面(MainActivity页面向NextActivity页面传递一个List<xxInfo>): Intent intent = new Intent(this, Next
转载:http://www.ibm.com/developerworks/cn/web/wa-jaxrs/
JAX-RS (JSR-311) 【 Java API for RESTful Web Services 】是一种 Java™ API,可使 Java Restful 服务的开发变得迅速而轻松。这个 API 提供了一种基于注释的模型来描述分布式资源。注释被用来提供资源的位
ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long keepAlive