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 http://www.springsource.org/update/e3.4. 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
 ,com.springsource.javax.servlet.jsp
 ,com.springsource.org.apache.taglibs.standard
 ,org.springframework.core
 ,org.springframework.beans
 ,org.springframework.context
 ,org.springframework.context.support
 ,org.springframework.web
 ,org.springframework.web.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.

src/main/resources/META-INF/spring/module-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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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

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

</beans>

src/main/resources/META-INF/spring/osgi-context.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/osgi  
       http://www.springframework.org/schema/osgi/spring-osgi.xsd
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans:beans>

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

src/main/resources/MODULE-INF/index.html

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

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:

src/main/resources/MODULE-INF/WEB-INF/jsp/home.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
 <head><title>Open Toast</title></head>
 
 <body>
  <h2>Welcome to Open Toast Project</h2>
  
  <h3>Current Members:</h3>
  <ul>
  <c:forEach items="${members}" var="member">
  <li><c:out value="${member.name}"/>
  </li>
  </c:forEach>
  </ul>
 </body>
</html>

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

src/main/java/org/simple/web/HomePageController.java

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;

@Controller
@RequestMapping("/home.htm")
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);
    }
}

src/main/java/org/simple/web/Member.java

package org.simple.web;

public class Member {
    private String name;

    public Member(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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.

1 comment:

Unknown said...

Great, first time I see a spring source DM tutorial on eclipse worked !

The only thing missing is the default springsourcedm admin console login, but it was easy to find anyway.

Many thanks again, it was terribly helpful after all the non-working tutorials I've tried.