3. Message Service Web Module

Manifest Configuration

The web module's manifest is very similar to the other bundles, but it imports the taglib and servlet bundles. Also instead of exporting a package, it imports the Message Service package exported by the Message Service bundles and specifies that is can use version 1.0.0 through 1.1.0.

src/main/resources/META-INF/MANIFEST.MF
                    
Manifest-Version: 1.0
Bundle-Name: Simple Service Web Module
Bundle-SymbolicName: org.springbyexample.sdms.message.webModule  1
Bundle-Version: 1.0.0  2
Bundle-Vendor: Spring by Example
Bundle-ManifestVersion: 2
Import-Bundle: com.springsource.org.apache.taglibs.standard,
 com.springsource.javax.servlet
Import-Library: org.aspectj;version="[1.6.0,1.7.0)",
 org.springframework.spring;version="[2.5.5,3.0.0)"
Module-Type: Web  3
Web-ContextPath: message  4
Web-DispatcherServletUrlPatterns: *.html  5
Import-Package: org.springbyexample.sdms.message.service;version="[1.0.0,1.1.0]"  6
                    
                
1 The symbolic name the bundle is deployed under and another bundle could use to reference it.
2 The version the bundle is deployed under.
3 Declares the module type indicating this is a web module. This is used by the Spring dm Server.
4 The Web-ContextPath property configures the web application's context path.
5 The Web-DispatcherServletUrlPatterns property configures the dispatch servlet to route any matches for the pattern '*.html' to controllers.
6 The Import-Package property imports the package org.springbyexample.sdms.message.service which was exported by the message service bundle.

Spring Configuration

The bundle-context.xml isn't used in this example, but bundle-context-osgi.xml makes a reference to the message bundle service and exposes it as a bean. The Spring web configuration is in webmvc-context.xml.

The reference to the OSGi service MessageService is exposed as a bean named messageService.

src/main/resources/META-INF/spring/bundle-context-osgi.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/osgi 
                           http://www.springframework.org/schema/osgi/spring-osgi.xsd">

    <reference id="messageService" interface="org.springbyexample.sdms.message.service.MessageService"/>
  
</beans:beans>
                    
                

This is just a standard Spring MVC configuration and the controller is register using context:component-scan.

src/main/resources/META-INF/spring/webmvc-context.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springbyexample.sdms.web.message.mvc" />
    
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors" ref="localeChangeInterceptor"/>
    </bean>

    <!-- Enables annotated POJO @Controllers -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- Enables plain Controllers -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <value>messages</value>
        </property>
    </bean>
    
    <!-- Declare the Interceptor -->
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="locale"/>
    </bean>
    
    <!-- Declare the Resolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

</beans>
                    
                

JSP Example

To reduce the complexity of the example the controller just puts the message into the Model and is rendered on the page using JSTL.

src/main/resources/MODULE-INF/WEB-INF/jsp/display/index.jsp
                    
<html>
<head>
<title>Message</title>
</head>
<body>
<h1>Message</h1>

<p>${message}</p>
</body>
</html>
                    
                

Code Example

The controller has the message service defined in bundle-context-osgi.xml injected using @Autowired. The display method maps to /display/index.html" and puts the results of the message service into the Model under the key 'message'.

Example 5. Mesage Service MessageController

sdms/message-service/service-web-module/src/main/java/org/springbyexample/sdms/web/message/mvc/MessageController.java
                    
@Controller
public class MessageController {

    @Autowired
    protected MessageService messageService = null;
    
    /**
     * Displays RMI info.
     */
    @RequestMapping(value="/display/index.html")
    public void display(Model model) {
    	model.addAttribute("message", messageService.getMessage());
    }

}