Learn Java for Web Development

(Tina Meador) #1
CHAPTER 5: Building Java Web Applications with Spring Web MVC 231

Listing 5-35. Programmatic Equivalent of Listing 5-43


public class ExampleWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("dispatcher",
new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/bookstore/*");
}
}


By default, DispatcherServlet looks for a file named WEB-INF/-servlet.xml,
where is replaced with the value declared in web.xml in the tag.
DispatcherServlet uses this -servlet.xml file to create WebApplicationContext.


Spring MVC Components

As mentioned earlier, DispatcherServlet searches the SpringMVC components from the
WebApplicationContext it created, and if not found, it uses the default. These Spring MVC
components are expressed as interfaces. Table 5-6 gives an overview of all the main component
types involved in the request-processing workflow.


Table 5-6. Spring MVC Components


Bean type Explanation

HandlerMapping Maps incoming requests to handlers and interceptors
HandlerAdapter For extending DispatcherServlet to customize the web workflow
HandlerExceptionResolver Maps exceptions to views
ViewResolver Resolves logical view names to actual views
LocaleResolver Resolves the locale a client is using for internationalized views
ThemeResolver Resolves themes for personalizing layouts
MultipartResolver Parses multipart for file uploads
FlashMapManager Supports FlashMap to pass attributes from one request to another

The Spring DispatcherServlet uses Spring MVC components that need to be configured in
WebApplicationContext to process requests. However, if you don’t configure these components,
Spring Web MVC uses the default. Table 5-7 lists the default implementation of the components.

Free download pdf