Start a java web application with struts 2

From the very beginning of this tutorial, the goal is to build a very simple web application in Struts 2. However, the tutorial covers also:

  • where to download Java, Eclipse and Tomcat (this tutorial will not cover how to install Java, Eclipse or Tomcat);
  • where to download Struts 2 framework;
  • how to create a basic web project in Eclipse with Struts;
  • how to run the project in Tomcat.

In what is next I plan to guide you through the steps to create a simple java web application with struts using Eclipse Mars IDE for MAC OS X and Tomcat 7 application server. This tutorial assumes you already have Java, Eclipse and Tomcat installed on your local machine. In case not, you can download them from the following sources:

Once again, the installation of the three falls outside the tutorial’s scope.

Struts 2 Requirements: Struts 2 requires Servlet API 2.4 or higher, JSP 2.0 or higher and JAVA 7 or higher.

Step 1: Create the project in eclipse

Open eclipse and go to File -> New -> Web Project. Near the name field enter the project name (we are going to call our application HelloDemo), choose a simple project template. Leave programming model selected as Java EE and click next button:

Start a simple web project in Eclipse

Choose the target runtime and context root of the HelloDemo application:

Eclipse new web project - deployment step

Eclipse new web project - web module step

Click finish button.

Step 2: Download the struts framework and add the minimum configuration to your project

Forwards, download the struts distribution from apache and copy the minimal necessary jars to WebContent\WEB-INF\lib directory. At the time of this tutorial was written I used the struts-2.5 distribution. Therefore, the jars I copied to HelloDemo projects are:

  • commons-fileupload-1.3.1.jar
  • commons-io-2.4.jar
  • commons-lang-2.4.jar
  • commons-lang3-3.4.jar
  • commons-logging-1.1.3.jar
  • freemarker-2.3.23.jar
  • javassist-3.20.0-GA.jar
  • log4j-api-2.5.jar
  • ognl-3.1.4.jar
  • struts2-core-2.5.jar

Next step is to configure the Struts 2 framework into you project and for this you need to add the struts.xml configuration file (at java source level) and define the entry point of the Struts 2 framework in web.xml file.

Let’s start with the configuration of web.xml, as this is the entry point of any JAVA EE web application. In this file we are configuring the  StrutsPrepareAndExecuteFilter filter (which is the entry point of a struts 2 application). Your web.xml file should look like:

Notice the filter and filter-mapping entries. These two entries are part of struts 2 framework configuration.

Let’s turn our attention to struts.xml file which contains the configurations for the struts actions. This is a mapping file which ties together the URL (the user action), the *Action.java class (the MODEL) and the jsp page (the VIEW) and it must be located under the WEB-INF\classes (hence we are going to create this file under the source folder of the project.

The above minimal configuration tells the Struts 2 framework that if the URL ends in index.action to redirect the browser to index.jsp.

I want to make a couple of points about the previous code:

  • set the constant struts.devMode value=true to enable some useful struts 2 message logs;
  • the package tag allows the separation and the modularization of the configuration;
  • the result tag, determine what gets returned to the browser after an action is executed.

Let’s create an index.jsp page:

Build your project, start your server and go to http://localhost:8080/HelloDemo/ in your preferred browser. Congrats! Your first struts 2 application is up and running. You will see:

Hello Demo Struts Application Up and Running

Let’s not stop here, let’s have some fun and add a simulation login functionality to our HelloDemo application. We are doing this in four steps:

  1. Create a login.jsp page with a simple html form (user, password and submit button)
  2. Create a PerformLoginAction.java class which contains the logic to manage the successful login and failed login
  3. Create a jsp page forsuccessful login (dashboard.jsp) and another jsp page for failed login (wrongCredentials.jsp)
  4. Create an action in struts.xml for PerformLoginAction and tie it’s result to dashboard.jsp and wrongCredentials.jsp

Notice the form action=”performlogin”. This action is defined in struts.xml:

When this action is called, the execute method of PerformLoginAction starts. In this method will return success you will be redirected to dashboard.jsp, otherwise (in case error is returned) you will be redirected to worgCredentials.jsp. Let’s have a look at PerformLoginAction.java class:

You can see, the success definition is username equals John and password equals good. In real life example, the things are different, the password would be encrypted and the values for username and password would be checked into a repository system (usually LDAP or  a database). However, for the purpose of this tutorial the approach used is more adequate.

Following are the dashboard.jsp and wrongCredentials.jsp:

The end !

Summary

In this tutorial I guide you into the creation of a simple web application using Struts 2 framework.

Now that you have a taste of how to use Struts 2 framework in your application, I will extend, in a further post, this application to use database capabilities

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply