Marina Sprava, Public Relations manager at Jelastic since October 2011, has technical engineering background in systems of control and automation. Scientific interests: cloud computing, cloud hosting, agile methodologies and medical imaging. Marina is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Web Application Development with Struts Framework and TomEE

09.07.2013
| 3491 views |
  • submit to reddit

Apache TomEE supported in Jelastic PaaS is a Java EE server, which provides you with the best technology stack that can be deployed to a simple Java EE container. It’s lightweight and at the same time is a full-blown application server, which remains simple and avoids architecture overhead. Today you’ll see how easy it is to deploy Java web apps into TomEE. In the following tutorial you’ll learn the basics of web application development with the help of the Apache Struts framework, which is an absolutely free open-source solution for creating Java web apps.

Create environment

1. First of all log into the Jelastic dashboard and click the Create environment button.

2. Pick TomEE as your application server and specify the number of consumed cloudlets for it. Then select the Maven node for further application building. Then enter your application name and clickCreate to confirm the creation of the new environment.

tomee environment

Wait several seconds until your environment is created.

TomEE Environment Created

Create application

As an example we’ll use a TomEE sample application, developed with the Struts framework, which allows you to add users to the list and to find them in there.

1. First of all let’s write our user class:

package org.superbiz.struts;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "USER")
public class User implements Serializable {
private long id;
private String firstName;
private String lastName;
public User(long id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public User() {
}
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

2. The next class has methods that allow you to add user’s info such as first name, last name and ID:

package org.superbiz.struts;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
public class AddUser {
private int id;
private String firstName;
private String lastName;
private String errorMessage;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String execute() {
try {
UserService service = null;
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.core.LocalInitialContextFactory");
Context ctx = new InitialContext(props);
service = (UserService) ctx.lookup("UserServiceImplLocal");
service.add(new User(id, firstName, lastName));
} catch (Exception e) {
this.errorMessage = e.getMessage();
return "failure";
}
return "success";
}
}

3. After that let’s declare the form for adding users:

package org.superbiz.struts;
import com.opensymphony.xwork2.ActionSupport;
public class AddUserForm extends ActionSupport {
}

4. The next class has a list of methods that are designed for user’s identification in our user base:

package org.superbiz.struts;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
public class FindUser {
private int id;
private String errorMessage;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String execute() {
try {
UserService service = null;
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.core.LocalInitialContextFactory");
Context ctx = new InitialContext(props);
service = (UserService) ctx.lookup("UserServiceImplLocal");
this.user = service.find(id);
} catch (Exception e) {
this.errorMessage = e.getMessage();
return "failure";
}
return "success";
}
}

5.  Here’s a declaration of the form for FindUser class:

package org.superbiz.struts;
import com.opensymphony.xwork2.ActionSupport;
public class FindUserForm extends ActionSupport {
}

6. The following class has methods for listing all added users.

package org.superbiz.struts;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.List;
import java.util.Properties;
public class ListAllUsers {
private int id;
private String errorMessage;
private List users;
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String execute() {
try {
UserService service = null;
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.core.LocalInitialContextFactory");
Context ctx = new InitialContext(props);
service = (UserService) ctx.lookup("UserServiceImplLocal");
this.users = service.findAll();
} catch (Exception e) {
this.errorMessage = e.getMessage();
return "failure";
}
return "success";
}
}

7. Now let’s create the interface for our application:

package org.superbiz.struts;
import java.util.List;
public interface UserService {
public void add(User user);
public User find(int id);
public List<User> findAll();
}

8. Here’s the above class realization:

package org.superbiz.struts;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
@Stateless
public class UserServiceImpl implements UserService {
@PersistenceContext(unitName = "user")
private EntityManager manager;
public void add(User user) {
manager.persist(user);
}
public User find(int id) {
return manager.find(User.class, id);
}
public List<User> findAll() {
return manager.createQuery("select u from User u").getResultList();
}
}

9. It’s time to create configuration files for our application. The struts.xml file contains the configuration information that you will be modifying as actions are developed:

<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="addUserForm" class="org.superbiz.struts.AddUserForm">
<result>/addUserForm.jsp</result>
</action>
<action name="addUser" class="org.superbiz.struts.AddUser">
<result name="success">/addedUser.jsp</result>
<result name='failure'>/addUserForm.jsp</result>
</action>
<action name="findUserForm" class="org.superbiz.struts.FindUserForm">
<result>/findUserForm.jsp</result>
</action>
<action name="findUser" class="org.superbiz.struts.FindUser">
<result name='success'>/displayUser.jsp</result>
<result name='failure'>/findUserForm.jsp</result>
</action>
<action name="listAllUsers" class="org.superbiz.struts.ListAllUsers">
<result>/displayUsers.jsp</result>
</action>
</package>
</struts>

10. The Web.xml file provides an entry point for any web application. The entry point of application, developed with Struts framework, will contain the filters defined in deployment descriptor:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Learn EJB3 and Struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.lq</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<jsp-property-group>
<description>JSP configuration of all the JSP's</description>
<url-pattern>*.jsp</url-pattern>
<include-prelude>/prelude.jspf</include-prelude>
</jsp-property-group>
</jsp-config>
</web-app>

11. Finally let’s create decorators.xml for applying our web app header etc.

<decorators defaultdir="/decorators">
<decorator name="main" page="layout.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>

12. Save your code to Git or SVN repository.

Deploy application

1. Return to the Jelastic dashboard and click the Add project button next to the Maven icon.

2. In the appeared dialog select Git or SVN (SVN in our case) tab and enter your project name and the link to your repository. There are also login and password fields in the case of private repository.  After that, select the name of environment and contextfor further project deployment.

Add Struts Project

3. To deploy software updates automatically based on your commits in GIT, tick the Check and Auto-deploy Updates check-box and specify the time period for verification in minutes.

Deploy Software Updates

The Jelastic system will be performing the check if you have made any commits with specified intervals and your project will be built and deployed automatically.

4. Click the Build and deploy button for your project.

Build and Deploy Project

Voila! Now your Struts application is up and running on TomEE right in the Cloud.

Struts Application on TomEE

As you can see it’s very easy to host applications in the Cloud using a great software stack like TomEE. Even very complex web applications can be deployed and run in seconds. Give it a try! Start your free trialtoday!

Published at DZone with permission of Marina Sprava, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)