5 Steps to Get Spring MVC Web Application on Google App Engine

The article represents steps on what you need to do to get your first Spring MVC Hello World web application project on Google App Engine (GAE). As a pre-requisite, we recommend you to check our earlier article on How to get started with Google App Engine. The article would help you to quickly get your started with GAE based web development using Eclipse IDE.

We shall work  with our existing Non Spring MVC project (check this page) and convert it into Spring MVC based web application.

Step 1: Spring MVC Libraries

Get Spring MVC libraries within folder (war/WEB-INF/lib). Also, do not forget to get following two runtime dependent libraries:

  • commons-logging-1.1.3.jar
  • com.springsource.org.aopalliance-1.0.0.jar

Step 2: Web.XML Configuration

Create following entry in web.xml.

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

Step 3: Spring-servlet.xml Configuration

Create a spring-servlet.xml and place it within war/WEB-INF folder. The content of spring-servlet.xml could be as simple as following:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- Make a NOTE of component scan. Create HelloController in package "com.orgname.hello" -->
<context:component-scan base-package="com.orgname.hello" />

<!-- Configuration defining views files -->

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

Step 4: Creating & Configuring Views & Controllers 

  • Create a “views” folder within war/WEB-INF folder and place all the JSP files within this “views” folder. Create a file index.jsp with “hello world” text. All the request to views shall route through Controller. The file whose content will be displayed will be index.jsp. The way it works is that the request is made to dispatcher servlet which based on the context path, “/” in this case, delegates to the controller (HelloController as shown above) having RequestMapping for “/”. The method then redirects the request to view-resolver with “index” as view name. The view resolver then sends the request appropriately to index.jsp. Thus, we shall later see the RequestMapping entries within Controller class.
  • Create a controller class within your custom package (com.orgname.hello) within src folder. The package name (com.orgname.hello) MUST be same as that mentioned inside sprint-servlet.xml within attribute “context:component-scan”. Look at previous step for sample. Create RequestMapping entries for all the views within the controller such as that shown in the code below.If you shall see the compile-time error, add the Spring-related libraries by right clicking on project, then, Properties and adding external libraries to Java Build Path/Libraries tab.
@Controller
public class HelloController {
 	//
 	// Create a file, index.jsp within folder war/WEB-INF/views. 
 	// This index.jsp would showup at 127.0.0.1:8888.
 	//
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index( ModelMap model ) {
		return new ModelAndView("index");
	}
	//
	// Create a file, java_functional_interface.jsp within folder war/WEB-INF/views. 
	// This file would be accessed when the URL accessed from website is   http://127.0.0.1:8888/java_functional_interface.html
 	//
	@RequestMapping(value = "/java_functional_interface.html", method = RequestMethod.GET)
	public ModelAndView getJavaFuncInterface( ModelMap model ) {
		return new ModelAndView("java_functional_interface");
	}
	//
	// Create a file, aboutus.jsp within folder war/WEB-INF/views. 
	// This file would be accessed when the URL accessed from website is http://127.0.0.1:8888/aboutus.html
 	//	
	@RequestMapping(value = "/aboutus.html", method = RequestMethod.GET)
	public ModelAndView getAboutUs( ModelMap model ) {
		return new ModelAndView("aboutus");
	}
	//
	// Create a file, hello.jsp within folder war/WEB-INF/views. 
	// This file would be accessed when the URL accessed from website is http://127.0.0.1:8888/springmvc/helloworld
 	//		
	@RequestMapping(value = "/springmvc/helloworld", method = RequestMethod.GET)
	public ModelAndView newUser( ModelMap model ) {
		return new ModelAndView("hello");
	} 
}

Step 5: Run & Deploy on GAE

  • Run the Web Application by right-clicking on project and then clicking on Run As…Web Application. Make sure the server starts correctly. Then, check the URL, http://127.0.0.1:8888/. Check the URL http://127.0.0.1:8888/springmvc/helloworld and this should display the content of war/WEB-INF/views/hello.jsp
  • Once all is OK at step 6, you are all set. Right-click on project and go to Google…Deploy to App Engine and that is it. Once deployed, check your web application online at URL http://app-id.appspot.com. Also, check http://app-id.appspot.com/springmvc/helloworld. As a reference, check the URL http://quick-code-samples.appspot.com/springmvc/helloworld.

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Java. Tagged with .

3 Responses