Friday, January 18, 2013

Running Spring 3.2 on Google App Engine

We will walk through the minimum to set up Spring MVC on Google App Engine.

Below are the specs that we are using:
  • Java 1.6
  • Spring MVC 3.2
  • Google App Engine SDK 1.7
  • Spring Tool Suite 3.1

Launch Spring Tool Suite.

In the Dashboard (one of the tabs), click Extensions. Find and install Google Plugin.

Create a Google App Engine Project. Do not check GWT.

First put the following Spring dependencies to /war/WEB-INF/lib
  • spring-aop-3.2.0.RELEASE.jar
  • spring-beans-3.2.0.RELEASE.jar
  • spring-context-3.2.0.RELEASE.jar
  • spring-context-support-3.2.0.RELEASE.jar
  • spring-core-3.2.0.RELEASE.jar
  • spring-expression-3.2.0.RELEASE.jar
  • spring-web-3.2.0.RELEASE.jar
  • spring-webmvc-3.2.0.RELEASE.jar

You will also need:
Right click on the root folder of the project. Click Properties. Click Java Build Path. Add all the jars above.

In /war/WEB-INF/web.xml, add the following:

<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
view raw gistfile1.xml hosted with ❤ by GitHub


Create /war/WEB-INF/spring-mvc-servlet.xml.

Replace with:

<bean id="VideoController" class="com.yourcompanyname.controller.HelloController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
view raw gistfile1.xml hosted with ❤ by GitHub

Create the folder /war/views.

Create the file /war/views/hello_world.jsp.

Create the file /src/com.yournamespace.controller.HelloWorldController.java

Put the following:

package com.yournamespace.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping(value="/hello")
public class HelloContoller {
@RequestMapping(value="", method = RequestMethod.GET)
public String sayHello() {
return "hello_world";
}
}
view raw gistfile1.java hosted with ❤ by GitHub

The above returns the hello_world.jsp.

Right click on the root project folder. Click Run as -> Web Application.

No comments:

Post a Comment