Wednesday, March 25, 2009

Simple Web Application with SpringSource dm Server (Updated)

I posted my experience of developing web application on SpringSource dm Server, then struggled with Maven, OSGi for last several weeks, didn't know some people asked me questions about the Simple Web Application with SpringSource dm Server. I apologize for not setting comment notification.

So here is the updated tutorial:

1. Download and install Eclipse Ganymede SR2. Install Spring IDE 2.2.2 and dm Server Tools 1.1.2 from Download and unzip SpringSource dm Server 1.0.2.RELEASE, and set up a server runtime in Eclipse at localhost.

2. Create a new SpringSource dm Server Bundle Project in Eclipse. Project name is "org.simple.web". Set Java source folder to src/main/java. Next, set symbolic name same as project name, the module type is "Web", and set target runtime to SpringSource dm Server. Next, web context path is "simple", and dispatcher servlet url pattern is "*.htm".

3. After project creation, move the META-INF folder from src/main/java to src/main/resources folder and add the resources folder to Java build source path in Eclipse.

Update the MANIFEST.MF :

Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: Simple Web Bundle
Bundle-ManifestVersion: 2
Bundle-Description: A very simple Spring DM Web Bundle
Bundle-SymbolicName: org.simple.web
Import-Bundle: com.springsource.javax.servlet
Module-Type: Web
Web-ContextPath: simple
Web-DispatcherServletUrlPatterns: *.htm

The SpringSource dm Server Tool automatically sets up Bundle Dependencies.

4. Add spring config files. Create folder src/main/resources/META-INF/spring, and add module-context.xml, osgi-context.xml.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""
  xmlns:p="" xmlns:aop=""
  xmlns:context="" xmlns:jee=""

 <context:component-scan base-package="org.simple.web"/>

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



<?xml version="1.0" encoding="UTF-8"?>
   xsi:schemaLocation=" ">


5. Create folder "src/main/resources/MODULE-INF", this is the folder for all the web content. Add index.html to MODULE-INF folder:


  <meta http-equiv="Refresh" content="0; URL=home.htm">

The index.html redirects to home page "home.htm", and invoke Spring MVC code.

6. Add a very simple jsp file for the home page, just display welcome messag and list all members from backend:


<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="" %>
 <head><title>Open Toast</title></head>
  <h2>Welcome to Open Toast Project</h2>
  <h3>Current Members:</h3>
  <c:forEach items="${members}" var="member">
  <li><c:out value="${}"/>

7. Add Spring MVC controller class and support class for home page:


package org.simple.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class HomePageController {
    public HomePageController() {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        List<Member> members = new ArrayList<Member>();
        members.add(new Member("Yuan Ji"));
        return new ModelAndView("home", "members", members);


package org.simple.web;

public class Member {
    private String name;

    public Member(String name) { = name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

8. Add org.simple.web project to SpringSource dm Server, and start the server. Open browser to http://localhost:8080 and click admin console. click /simple, you can see the home page of Open Toast Project.