3. GreenPages Highlights
Prev |  | Next

## 3. GreenPages Highlights

This chapter picks out some notable features of the GreenPages sample code from the `solution` folder.

## 3.1 Web Application Bundle Highlights

The GreenPages Web Application Bundle (WAB) is built using Spring MVC configured with Spring annotations and component scanning. The Bundlor tool is used to generate the bundle manifest of the WAB and a service is injected into the code using Spring DM in combination with Spring autowiring.

For more information on Spring, Spring MVC, Bundlor and Spring DM, please see Projects..

### web.xml

The web deployment descriptor file `web.xml` is in the `src/main/webapp/WEB_INF` folder of the `greenpages.web` project. It defines a servlet, a servlet context parameter, and a servlet context listener.

Spring's dispatcher servlet is used to dispatch web requests to handlers.

      <servlet>
      <servlet-name>greenpages</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>

The `contextClass` servlet parameter declares the implementation of `WebApplicationContext` that Spring instantiates. The application context acts as a root application context and each servlet in the web application, which in the case of GreenPages is just the dispatcher servlet, has its own application context which is a child of the root application context. `ServerOsgiBundleXmlWebApplicationContext` is provided by Virgo and will hold beans created by Spring DM, which are then available in child application contexts.

      <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
      </context-param>

A servlet context listener is defined which will start up the root application context for the web application when the servlet context is initialised.

      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

### Controller Class

In the `src/main/java` source folder of the `greenpages.web` project the package `greenpages.web` contains the controller class `GreenPagesController`.

Spring annotations are used to add web behaviour to the class. The `@Controller` annotation tells Spring that the class serves the role of a controller and that the class should be scanned for request mappings. Request mappings are defined using the `@RequestMapping` annotation. For instance, the URL `/home.htm` is mapped to the handler method `home`.

    @Controller
    public class GreenPagesController {
    	…
        @RequestMapping("/home.htm")
        public void home() {
        }
        …

Note that request mappings can also be specified at the class level.

### Component Scanning

Spring will detect the `@Controller` annotation and create a bean of type controller, provided that it scans the classpath for these. Spring’s component scanning is enabled by the presence of a `context` tag in one of the Spring bean definition files.

The `WEB-INF/greenpages-servlet.xml` file in the `src/main/webapp` folder contains the following lines:

    <!-- enable classpath scanning -->
    <context:component-scan base-package="greenpages.web" />

Notice the convention embodied in the filename `WEB-INF/greenpages-servlet.xml`. During dispatcher servlet initialisation, Spring looks for a file named `[servlet-name]-servlet.xml` in the `WEB-INF` directory of the web application and creates the beans defined there.

### Bundle Manifest

The Virgo Tomcat Server has special support for WABs. To take advantage of this support, the `greenpages.web` bundle must be declared to be a WAB and a context path must be defined.

The Bundlor template (the file `template.mf` at the top level under the `greenpages.web` project) is input to the Bundlor tool which generates the manifest of the bundle.

The Bundlor template defines the context path as follows (and this is what declares the bundle to be a WAB):

    Web-ContextPath: greenpages

The Bundlor template also ensures Spring packages and greenpages packages from other bundles are imported with suitable version ranges:

    Import-Template:
     org.springframework.*;version="[3.0, 3.1)",
     greenpages.*;version="[2.3, 2.4)"

### Service Injection

The file `webapp/WEB-INF/applicationContext.xml` declares a reference to a `greenpages.Directory` service in the service registry using Spring DM as follows:

    <osgi:reference id="directory" interface="greenpages.Directory"/>

The resultant bean resides in the root web application context.

The `GreenPagesController` class uses Spring autowiring to inject the service:

    @Autowired
    private Directory directory;

The controller's bean resides in the web application context associated with the Spring dispatcher servlet and so has access to the directory service bean in the root web application context.

Prev |  | Next
2.5 Running GreenPages from Eclipse | Home | 3.2 Middle Tier Highlights