Home Struts Struts Struts2.2.1 Running and testing the example

Ask Questions?

View Latest Questions


 
 

Running and testing the example
Posted on: November 23, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how to run and test the struts application

Running And Testing The Example

Before running the application, you will have to build it  to build the application using ant at first download the ant from http://ant.apache.org/bindownload.cgi and set the the environment variable path of the ant like this.

then open your command prompt ant type ant. In a message appears like

Then your ant is configured.

now write your struts application and make an xml file known as build.xml  as

build.xml

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

<!-- Project settings -->
<property name="project.jar.file" value="struts2tutorial.jar"/>

<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="libext">
<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 ="libext/servlet-api.jar"/>
<pathelement path ="libext/catalina-ant.jar"/>
<pathelement path ="classes"/>
<pathelement path ="${classpath}"/>
</path>

<!-- Check timestamp on files -->
<target name="prepare">
<tstamp/>
<!--
<copy
file="src/java/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="src/classes"/>
<mkdir dir="src/classes"/>
</target>

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


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

</project>
Save the build.xml file into the src directory, now on command prompt go to the src directory where build.xml file is saved. ant type ant for bulding application. This make a struts2tutorial.jar file into the lib directory of your application.  To run the application download the code and paste into the tomcat webapps directory. Now start your tomcat, open the web browser type http://localhost:8080/struts221 and press the enter then you will see the following output

 

To run the hello world example press on the link HelloWorld in English. then it will call the HelloWorld.action class. and ecute() method of the called and the output displays like this

To test whether action is called or not you can add any Console message like System.out.println("Hello World Action Called");  into to your action class so that you may confirm that whether action is called or not.you will see an output as

When you click on the link Login Form it will call the Login Action and executed the  logic written within this class. If you type wrong password then it calls the message.properties files and display the messages written in the properties file on the form. If you login successful then then it calls to the loginsuccess.jsp file. The properties files is also known as message resources. It stores the messages in the form of key and values.

Related Tags for Running and testing the example:


DMCA.com