Building Systems Management as a Service

Dmitry Sotnikov

Subscribe to Dmitry Sotnikov: eMailAlertsEmail Alerts
Get Dmitry Sotnikov: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Java EE Journal, Java Developer Magazine

Blog Feed Post

Web Application Development with Struts Framework and TomEE

In this tutorial you'll learn the basics of web application development with the help of the Apache Struts framework

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>

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 context for 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 trial today!

The post Web Application Development with Struts Framework and TomEE appeared first on Jelastic Blog.

Read the original blog entry...

More Stories By Dmitry Sotnikov

Dmitry Sotnikov is VP of Cloud at WSO2, building the cloud business for this leading middleware provider. Check out the WSO2 Cloud platform at http://CloudPreview.WSO2.com