Tuesday, June 30, 2009

Using Spring 2.0 MVC in Google App Engine

In this post we will deploy sample spring 2.0 mvc application on google app engine we will download the spring related jar files from URL http://www.springsource.org/download.

Add the spring related jar files to the project by right click on projects node in the project explorer, choosing Build Path->Configure Build Path (see the image below).

Then select the Library tab and click add jars and select all the jars related to spring as shown below .

We will develop an example where we read from the datastore using spring JDO ,We will use earlier post model class i.e Brand for this example .

We will have an Manager class to obtain the list of Brands and Controller class to list method for the manager . The Manager class is as shown below .

import java.util.List;
import javax.jdo.PersistenceManager;
import org.springframework.orm.jdo.support.JdoDaoSupport;
public class BrandManager extends JdoDaoSupport {
  public List getBrandList() {
    PersistenceManager pm = getPersistenceManager();
    String query = "select from " + Brand.class.getName() ;
    List brandList = (List) pm.newQuery(query).execute();
    return brandList;
  }
}

Controller class is as follows

package com.tutorials;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BrandController implements Controller{
 private BrandManager brandManager;

 public BrandManager getBrandManager() {
  return brandManager;
 }

 public void setBrandManager(BrandManager brandManager) {
  this.brandManager = brandManager;
 }

 public ModelAndView handleRequest(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
   ModelAndView modelAndView = new ModelAndView("brandList");
  modelAndView.addObject("brandList", this.brandManager.getBrandList());
   return modelAndView;
 }

}

In Spring MVC DispatcherServlet is the first place a request meets the application, Front Controller pattern uses a HandlerMapping implementation to figure out which Controller class should process the request .

Configure DispatcherServlet in web.xml as shown below


      
  dispatcher     
  org.springframework.web.servlet.DispatcherServlet     
  1
  
      
  dispatcher     
  *.do
  

Default, DispatcherServlet looks for a configuration file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application

The servlet name in our application is dispatcher so we will create dispatcher-servlet.xml file in /WEB-INF/ folder

In dispatcher-servlet.xml we need configure JDO for appengine using Spring ORM as shown below

 <bean id="pmf"
        class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
    <property name="jdoProperties">
      <props>
        <prop key="javax.jdo.PersistenceManagerFactoryClass">
          org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory
        </prop>
        <prop key="javax.jdo.option.ConnectionURL">
          appengine
        </prop>
        <prop key="javax.jdo.option.NontransactionalRead">
          true
        </prop>
        <prop key="javax.jdo.option.NontransactionalWrite">
          true
        </prop>
        <prop key="javax.jdo.option.RetainValues">
          true
        </prop>
        <prop key="datanucleus.appengine.autoCreateDatastoreTxns">
          true
        </prop>
      </props>
    </property>

  </bean>

Code Can be downloaded from this Link

No comments:

Post a Comment