Struts 2 Guide

This is first example of Struts 2 in this Complete application development tutorial series.

Struts 2 Guide


This is first example of Struts 2 in this Complete application development tutorial series. You must finish this Struts 2 Guide in day 1 itself. Struts is vast topic and requires hard work to master it. I am going to explain you Struts 2 with "Hello World" example. So, work hard to learn the process of development and deployment of "Hello World" quick Struts 2 guided tutorial.

Example presented here will help you learn Struts 2 easily. Good luck for this lesson!

Step by Step Struts 2 Guide:

In this section you will learn how to develop "Hello World" application in Struts 2. This is your first step towards learning Struts 2. We will follow the following steps to develop the application.

Step 1:  

Download the Struts 2.0.11 and extract the download file and copy struts2-blank-2.0.6.war to your tomcats webapps directory. Rename struts2-blank-2.0.6.war to struts2helloworldand unzip it in the tomcats webapps directory. Now start the tomcat and type http://localhost:8080/struts2HelloWorldApplication/ into your browser. You browser should show look like:


Congratulations you have successfully installed struts 2 blank application to start with.

Step 2:  

Now delete the content of struts2helloworld\WEB-INF\src and struts2helloworld\WEB-INF\classes directories, as we don't need these files that comes with struts 2 blank application.

Step 3:  

Create build.xml file in the struts2helloworld\WEB-INF\src and paste the following content in the build.xml file.

<project name="Struts 2 Tutorial" basedir="../" default="all">

  <!-- Project settings -->
  <property name="project.title" value="RoseIndia Struts 2 Tutorials"/>
  <property name="project.jar.file" value="struts2tutorial.jar"/>

  <path id="class.path">
  <fileset dir="lib">
  <include name="**/*.jar"/>
  <fileset dir="libext">
  <include name="**/*.jar"/>

  <!-- Classpath for Project -->
  <path id="compile.classpath">
  <pathelement path ="lib/commons-beanutils.jar"/>
  <pathelement path ="lib/commons-digester.jar"/>
  <pathelement path ="lib/struts.jar"/>
  <pathelement path ="libext/servlet-api.jar"/>
  <pathelement path ="libext/catalina-ant.jar"/>
  <pathelement path ="classes"/>
  <pathelement path ="${classpath}"/>

  <!-- Check timestamp on files -->
  <target name="prepare">

  <!-- Copy any resource or configuration files -->
  <target name="resources">
  <copy todir="src/classes" includeEmptyDirs="no">
  <fileset dir="src/java">
  <include name="**/*.conf"/>
  <include name="**/*.properties"/>
  <include name="**/*.xml"/>

  <!-- Normal build of application -->
  <target name="compile" depends="prepare,resources">
  <javac srcdir="src" destdir="src/classes" debug="true" debuglevel="lines,vars,source">
  <classpath refid="class.path"/>

  <!-- Remove classes directory for clean build -->
  <target name="clean"
  description="Prepare for clean build">
  <delete dir="classes"/>
  <mkdir  dir="classes"/>

  <!-- Build Javadoc documentation -->
  <target name="javadoc"
 description="Generate JavaDoc API docs">
  <delete dir="./doc/api"/>
  <mkdir dir="./doc/api"/>
  <javadoc sourcepath="./src/java"
  windowtitle="${project.title} API Documentation"
  doctitle="&lt;h1&gt;${project.title} Documentation (Version ${project.version})&lt;/h1&gt;"
  bottom="Copyright &#169; 2002">
  <classpath refid="compile.classpath"/>

  <!-- Build entire project -->
  <target name="project" depends="clean,prepare,compile"/>

  <!-- Create binary distribution -->
  <target name="dist"
  description="Create binary distribution">


  <exclude name="${distpath.project}/${project.distname}.war"/>


  <!-- Build project and create distribution-->
  <target name="all" depends="project"/>


Step 4:

Create directory libext under the struts2helloworld\WEB-INF\ then you copy latest Servlets api jar (in our case servlet-api.jar) file over there. This library file will be used to compile Servlets in our application.

Step 5:

Now create directories java and classes under struts2helloworld\WEB-INF\src. The directory struts2helloworld\WEB-INF\src\java will be used to put all the java sources file and directory struts2helloworld\WEB-INF\src\classes will be used by ant build utility to store compiled java files.

Now we have successfully created the directory structure and ant build file for our Struts 2 Hello World Application.

Download "Hello World" Application


Share on Google+Share on Google+

Struts 2 Guide

Posted on: June 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Struts 2 Guide  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image