appContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml");
ServletRegistration.Dynamic dispatcher =
container.addServlet("appServlet", new DispatcherServlet(appContext));
MultipartConfigElement multipartConfigElement =
new MultipartConfigElement(null, 5000000, 5000000, 0);
dispatcher.setMultipartConfig(multipartConfigElement);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
As shown in Listing 17-66, the WebApplicationInitializer.onStartup() method is overridden, with
the logic to load the WebApplicationContext for the DispatcherServlet implemented. By calling the
method ServletContext.addServlet(), we can add the servlet to the underlying web container. The
method will return an instance of the javax.servlet.ServletRegistration.Dynamic interface, and via
this interface, we can configure various attributes for the servlet, such as the loadOnStartup servlet-to-
URL mapping, as well as the multipart support for file uploading, and so on. Rebuild and deploy the
project, and the DispatcherServlet's WebApplicationContext will be bootstrapped like the one defined in
the web.xml file.
Using this approach, when combined with the Java code-based configuration of Spring, it's possible
to implement a pure Java code-based configuration of a Spring-based web application, without the need
to declare any Spring configuration in web.xml or other Spring XML configuration files. For a more
detailed description of this feature, please refer to Spring Framework's online documentation for the
WebApplicationInitializer interface (http://static.springsource.org/spring/docs/3.1.x/javadoc-
api/org/springframework/web/WebApplicationInitializer.html).
Spring MVC in the Sample Application
Many topics discussed in this chapter will be adopted in the sample application for implementing the
presentation layer, and their relationships will be covered in this section.
MVC Implementation for SpringBlog
For the presentation layer of the SpringBlog application, Spring MVC will be used for implementing the
MVC pattern, and JSPX will be used as the view technology. The usage of Spring MVC and related
libraries are as follows:
Internationalization: Spring MVC's built-in support for i18n will be used to
·
support various languages in the SpringBlog application. In SpringBlog, support of
English (US) and Chinese (HK) will be provided.
Theming: We will use the theming support in Spring MVC. For example, the
·
<spring:theme> tag will be used in a page template to load the active theme being used.
Page templating: In the SpringBlog application, Apache Tiles will be used for page
·
templating. The page layout will be much like the one we presented in this
chapter, with standard components including a header, menu, footer, body, and
so on. Spring MVC's built-in support for using Tiles as the view resolver will be
used also.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home