Google App Engine + Struts 1 example

Long live classic Struts 1 framework, in this tutorial, we will show you how to develop a Struts 1.x web application in Google App Engine (GAE) environment.

Tools and technologies used :

  1. JDK 1.6
  2. Eclipse 3.7 + Google Plugin for Eclipse
  3. Google App Engine Java SDK 1.6.3.1
  4. Struts 1.3.10
Note
You may also interest at this Google App Engine + Struts 2 example.

This example is going to merge Struts 1.x hello world example with this GAE + Java example.

1. New Web Application Project

In Eclipse, create a new Web Application project, named as “StrutsGoogleAppEngine”.

struts1 on gae example

Google Plugin for Eclipse will generate a sample GAE project structure. later will integrate Struts 1 into this GAE structure.

2. Integrate Struts 1.x Libraries

Visit this link to download Struts 1.x. Following jars are required :

  • antlr-2.7.2.jar
  • commons-beanutils-1.8.0.jar
  • commons-chain-1.2.jar
  • commons-digester-1.8.jar
  • commons-logging-1.0.4.jar
  • commons-validator-1.3.1.jar
  • oro-2.0.8.jar
  • struts-core-1.3.10.jar
  • struts-taglib-1.3.10.jar

Copy and put it in “war/WEB-INF/lib” folder.

struts1 on gae example library

Right click on the project folder, select “Properties“. Select “Java Build Path” -> “Libraries” tab, click “Add Jars” button and select above 9 jars from “war/WEB-INF/lib” folder into the build path.

struts1 on gae example java build

3. Integrate Struts 1.x Action and Form

3.1 Delete StrutsGoogleAppEngineServlet.java , you don’t need this.

3.2 Create a new Action class.

File : src/com/mkyong/action/HelloWorldAction.java

package com.mkyong.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
import com.mkyong.form.HelloWorldForm;
 
public class HelloWorldAction extends Action {
 
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
 
		HelloWorldForm helloWorldForm = (HelloWorldForm) form;
		helloWorldForm.setMessage("Hello World!");
 
		return mapping.findForward("success");
	}
 
}

3.3 Create a new form class.

File : src/com/mkyong/form/HelloWorldForm.java

package com.mkyong.form;
 
import org.apache.struts.action.ActionForm;
 
public class HelloWorldForm extends ActionForm {
 
	String message;
 
	public String getMessage() {
		return message;
	}
 
	public void setMessage(String message) {
		this.message = message;
	}
 
}

4. Integrate Struts 1.x JSP Pages

4.1 Create HelloWorld.jsp page and put in “war/User/pages/HelloWorld.jsp“.

File : HelloWorld.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
</head>
<body>
	<h1>
	   Google App Engine + Struts 1.x example
	</h1>
	<h2><bean:write name="helloWorldForm" property="message" /></h2>
</body>
</html>

5. Struts XML configuration

Create a struts-config.xml file, and put it in “war/WEB-INF/struts-config.xml“.

File : struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
	   <form-bean name="helloWorldForm" type="com.mkyong.form.HelloWorldForm" />
	</form-beans>
 
	<action-mappings>
	   <action path="/helloWorld" type="com.mkyong.action.HelloWorldAction"
		name="helloWorldForm">
		<forward name="success" path="/HelloWorld.jsp" />
 
	   </action>
	</action-mappings>
 
</struts-config>

6. web.xml

Update web.xml, integrate Struts.

File : web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
 
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
		    <param-name>config</param-name>
		    <param-value>
         		/WEB-INF/struts-config.xml
        	    </param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
 
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

7. Enable Session in GAE

Update appengine-web.xml, enable session support, Struts need this.

File : appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application></application>
  <version>1</version>
 
	<sessions-enabled>true</sessions-enabled>
 
</appengine-web-app>
Note
If you don’t enable session in GAE, you will hit error “java.lang.RuntimeException: Session support is not enabled in appengine-web.xml“.

8. Directory Structure

Review the final directory structure.

struts1 on gae example final directory structure

9. Run on Local

Right click on the project, run as “Web Application“.

URL : http://localhost:8888/helloWorld.do

struts1 on gae example run on local

10. Deploy on GAE

Update appengine-web.xml file, add your App Engine application ID.

File : appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>mkyong-strutsgae</application>
  <version>1</version>
 
  <sessions-enabled>true</sessions-enabled>
 
</appengine-web-app>

Select project, click on Google icon, “Deploy to App Engine“.

URL : http://mkyong-strutsgae.appspot.com/helloWorld.do

struts1 on gae example deploy on gae
Note
Not much problem, just follow GAE directory structure, at least integrate Struts 1 is more easily than Struts 2.

Download Source Code

Due to large file size, all Struts1 jars are excluded, you need to download it manually.

Download – StrutsGoogleAppEngine (13 KB)

References

  1. Struts hello world example
  2. Google App Engine + Java hello world example, using Eclipse
  3. Download Struts 1.x
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments