Run Struts 2 Hello World Application
In this section, you will learn how to run "Hello World" Application. You follows the following instructions:
First of all compile your application. If you want to compile this application you need ANT building tools. ANT is used to deploy the hole application into specified directory. Ant works according to given instruction in build.xml file. In build.xml file contains all the information regarding deploy the application. Here, we specify all jars to help set class path.
build.xml:
<project name="struts2helloworldapplication"
basedir="../" default="all">
<!-- Project settings -->
<property name="project.title" value="Welcome
to Struts 2 Hello World Application"/>
<property name="project.jar.file"
value="struts2helloworldapplication.jar"/>
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- 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 ="lib/servlet-api.jar"/>
<pathelement path ="classes"/>
<pathelement path ="${classpath}"/>
</path>
<!-- Check timestamp on files -->
<target name="prepare">
<tstamp/>
<copy
file="src/struts.xml"
todir="src/classes"/>
</target>
<!-- Copy any resource or configuration files -->
<target name="resources">
<copy todir="src/classes" includeEmptyDirs="no">
<fileset dir="src/java">
<patternset>
<include name="**/*.conf"/>
<include name="**/*.properties"/>
<include name="**/*.xml"/>
</patternset>
</fileset>
</copy>
</target>
<!-- 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"/>
</javac>
<jar
jarfile="lib/${project.jar.file}"
basedir="src/classes"/>
</target>
<!-- Remove classes directory for clean build -->
<target name="clean"
description="Prepare for clean build">
<delete dir="classes"/>
<mkdir dir="classes"/>
</target>
<!-- Build Javadoc documentation -->
<target name="javadoc"
description="Generate JavaDoc API docs">
<delete dir="./doc/api"/>
<mkdir dir="./doc/api"/>
<javadoc sourcepath="./src/java"
destdir="./doc/api"
classpath="${servlet.jar}:${jdbc20ext.jar}"
packagenames="*"
author="true"
private="true"
version="true"
windowtitle="${project.title} API Documentation"
doctitle="<h1>${project.title} Documentation
(Version ${project.version})</h1>"
bottom="Copyright © 2002">
<classpath refid="compile.classpath"/>
</javadoc>
</target>
<!-- Build entire project -->
<target name="project" depends="clean,prepare,compile"/>
<!-- Create binary distribution -->
<target name="dist"
description="Create binary distribution">
<mkdir
dir="${distpath.project}"/>
<jar
jarfile="${distpath.project}/${project.distname}.jar"
basedir="./classes"/>
<copy
file="${distpath.project}/${project.distname}.jar"
todir="${distpath.project}"/>
<war
basedir="../"
warfile="${distpath.project}/${project.distname}.war"
webxml="web.xml">
<exclude name="${distpath.project}
/${project.distname}.war"/>
</war>
</target>
<!-- Build project and create distribution-->
<target name="all" depends="project"/>
</project> |
To deploy the application:
Go into your application where build.xml
file. Open Dos prompt and type cd C:\apache-tomcat-6.0.14\webapps\struts2HelloWorldApplication\WEB-INF\src
And type "ant" then you get the following output:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\biru>cd C:\apache-tomcat-6.0.14\webapps\struts2HelloWo
rldApplication\WEB-INF\src
C:\apache-tomcat-6.0.14\webapps\struts2HelloWorldApplication\WEB-INF\src>ant
Buildfile: build.xml
clean:
[delete] Deleting directory C:\apache-tomcat-6.0.14\webapps\struts2HelloWorld
Application\WEB-INF\classes
[mkdir] Created dir: C:\apache-tomcat-6.0.14\webapps\struts2HelloWorldApplic
ation\WEB-INF\classes
prepare:
resources:
compile:
[javac] Compiling 1 source file to C:\apache-tomcat-6.0.14\webapps\struts2He
lloWorldApplication\WEB-INF\src\classes
[jar] Building jar: C:\apache-tomcat-6.0.14\webapps\struts2HelloWorldAppli
cation\WEB-INF\lib\struts2helloworldapplication.jar
project:
all:
BUILD SUCCESSFUL
Total time: 2 seconds
C:\apache-tomcat-6.0.14\webapps\struts2HelloWorldApplication\WEB-INF\src>
|
Your application is deployed successfully. Then run your tomcat and get the following:

Open your web browser and type: http://localhost:8080/struts2HelloWorldApplication/ . Press enter then you will get:

Click on "Hello World Application" Then you will get:
