Navigation Bar

Friday, September 26, 2025

Building your first Struts application - putting it all together

This is a basic struts application where the user will input a stock quote and on submit he will get the corresponding quote for that stock.

Windows OS used : Windows 10

We first download the necessary softwares for this project as per the links provided below
Building your first Struts application - downloads

install java
sett up tomcat
extract the blank struts application to a local folder.

In <CATALINA_HOME>/webapps folder create the project structure for the getquote project as below


DirectoryContains
/getquoteThe root directory of the web application. All JSP and HTML files are stored here
/getquote/WEB-INFThis directory contains all resources related to the application that are not in the document root of the application. It is where the web application deployment descriptor web.xml is located. For the struts project, this folder will include the struts .tld. .dtd and .xml files.
/getquote/WEB-INF/classesThis is where the servlet and other classes are located.
/getquote/WEB-INF/libThis directory contains Java Archive (JAR) files that the web application is dependent on. The jars from the blank struts project are copied here.
/getquote/srcThe directory having the java source code.
/getquote/imagesImage files are kept in this folder

Once this folder structure is created copy the respective jars, tlds, dtd and xml files from the blank struts project to the <CATALINA_HOME>/webapps/getquote/WEB-INF folder. The tiles-defs.xml is not present in the blank struts project and a blank tiles-defs.xml file will need to be copied to this folder.

The Struts jar files
Copy the struts jar files from the blanks struts project struts-1.2.7/lib folder to the 
<CATALINA_HOME>/webapps/getquote/WEB-INF/lib folder.

The Java code components
The java files for the controller and the java bean data objects for the view components are copied to the
<CATALINA_HOME>/webapps/getquote/src folder.

Java bean data object components 

The controller components

The View components
Copy the view components to the <CATALINA_HOME>/webapps/getquote/

The config files
Copy the config files namely the struts-config.xml file and the web.xml file to the
<CATALINA_HOME>/webapps/getquote/WEB-INF folder.


Once all components are ready and placed in the respective folders the final folder structure should look as below.
<CATALINA_HOME>\webapps\getquote
quote.jsp
index.jsp

<CATALINA_HOME>\webapps\getquote\src
LookupAction.java
LookupForm.java

<CATALINA_HOME>\webapps\getquote\images
stockquote.jpg

<CATALINA_HOME>\webapps\getquote\WEB-INF
struts-config.xml
web.xml

struts-config_1_2.dtd
tiles-config_1_1.dtd
web-app_2_3.dtd

validator-rules.xml

struts-bean.tld
struts-html.tld
struts-logic.tld
struts-nested.tld
struts-tiles.tld

tiles-defs.xml

<CATALINA_HOME>\webapps\getquote\WEB-INF\lib
antlr.jar
commons-beanutils.jar
commons-digester.jar
commons-fileupload.jar
commons-logging.jar
commons-validator.jar
jakarta-oro.jar
struts.jar

--The compiled class files will get copied to the following directory path
<CATALINA_HOME>\webapps\getquote\WEB-INF\classes\src
LookupForm.class
LookupAction.class
Set the path variables for building the application.
Run set_path.bat on the command prompt
set COMMON_JAR=E:\winnith\java\project\getquote
set CLASSPATH=E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\lib\servlet-api.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\commons-beanutils.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\commons-digester.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\commons-fileupload.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\commons-logging.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\commons-validator.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\jakarta-oro.jar;E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\lib\struts.jar;
set ANT_HOME=E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1
set JAVA_HOME=E:\install\jdk\jdk
set PATH=E:\install\21cXE\dbhomeXE\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\PuTTY\;E:\install\jdk\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 6.0\bin;E:\install\jdk\jdk\bin;C:\Users\winni\AppData\Local\Microsoft\WindowsApps;E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
Run the ant build script to compile the java code components Build.xml
<project name="getquote" default="all" basedir=".">
<property name="struts.home"		value="E:\winnith\java\struts\home"/>
<property name="miscjar.home"	    value="E:\winnith\java\project\getquote"/>
<property name="wardir.dir"		value="${basedir}/war"/>

<property name="CATALINA_HOME" value="E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0"/>
<property name="tomcat.home" value="${CATALINA_HOME}"/>

<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/WEB-INF/classes"/>

<target name="all" depends="clean,compile"/>
	<target name="compile" depends="clean">
    <mkdir dir="${build.dir}"/>
    <javac srcdir="${src.dir}" destdir="${build.dir}"/>
</target>

<target name="clean">
	<delete dir="${build.dir}"/>
</target>

</project>
Output of the ant build should be as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote>ant
Buildfile: build.xml

clean:
   [delete] Deleting directory E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\classes

compile:
    [mkdir] Created dir: E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\classes
    [javac] Compiling 2 source files to E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\getquote\WEB-INF\classes

all:

BUILD SUCCESSFUL
Total time: 0 seconds

When running the application you will get an error tiles-defs.xml does not exist. Need to create a sample tiles-defs.xml file and put it it the WEB-INF folder of the corresponding project.
This is to avoid the error that you will get when starting the tomcat server : Can't find file '/WEB-INF/tiles-defs.xml'

Sample stack trace of the error from the <CATALINA_HOME>/logs folder is below

INFO: SessionListener: contextInitialized()
Sep 27, 2025 12:25:41 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /getquote threw load() exception
org.apache.struts.tiles.FactoryNotFoundException: null : Can't find file '/WEB-INF/tiles-defs.xml'
	at org.apache.struts.tiles.xmlDefinition.I18nFactorySet.initFactory(I18nFactorySet.java:224)
	at org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper.init(ComponentDefinitionsFactoryWrapper.java:139)
	at org.apache.struts.tiles.TilesUtilImpl.createDefinitionsFactory(TilesUtilImpl.java:168)
To avoid this error copy a sample tiles-defs.xml file to
<CATALINA_HOME>/webapps/getquote/WEB-INF/ folder
Sample tiles-defs.xml can be taken from link below

Some common errors you may encounter when starting the web server or in your application code are detailed below

We are now ready the run our first Struts application On any browzer like chrome or edge as follows
http://localhost:8080/getquote/index.jsp


Enter the quote SUNW and click on Get Quote
You will get the stock quote on the response page as below



God's Word for the day
Care in choosing friends 
From a spark many coals are kindled,
  And a sinner lies in wait to shed blood.
Beware of scoundres for they devise evil,
  And they may ruin your reputation forever.
Receive strangers in your home and they will stir up trouble for you,
  And will make you a stranger to your own family.
Sirach 11:32-34

Concerning Anger 
You have heard that it was said to those of ancient times
  'You shall not murder' and 'Whoever murders shall be liable to judgement'
But I say to you that if you are angry with a brother or sister,
   you will be liable to judgement;
And if you insult a brother or a sister, 
   you will be liable to the council.
And if you say 'You fool', 
    you will be liable to the hell of fire.
So when you are offering your gift at the altar,
    if you remember that your brother or sister has something against you,
Leave your gift there before the altar and go;
    First be reconciled to your brother or sister,
And then come and offer your gift.
    Come to terms quickly with your accuser
while you are on the way to court with him,
    Or your accuser may hand you over to the judge,
And the judge to the guard, and you will be thrown into prison.
Truly I tell you, you will never get out until you have paid the last penny.
Mathew 5:21-26

No comments:

Post a Comment