Testing Struts Application
It will be noticed that there are a lot of 'system.out.println's
in the action class code.Intentionally so! The step by step progress gets
printed in the Tomcat console and is very instructive. When trouble occurs, it
aids in diagnosis greatly.It also gives us a clear understanding.
------------------------------------------
But, understanding the code is one thing ( and the easier thing at that) and
actually executing the program ,is quite a different thing altogether! We must
do each step with maximum care, to avoid a lot of problems.
------------------------------------------
We begin by creating a folder( say)
g:\examplestruts>
Our proposed package name is demo1;
So create a subfolder named 'demo1'.
cd to g:\examplestruts\demo1
---------------------------
Create the following six files in demo1 folder.
- query.jsp
- QueryForm.java
- QueryAction.java
- sqlbean.java
- resultbean.java
- result.jsp
-----------------------------------
We cannot compile QueryForm.java & QueryAction.java , unless we first install Struts in our system.
---------------------------------
How do we instal Struts in our system?
-----------------
steps.
-------------
- transfer jakarta-struts1.1 zip file from DeveloperIQ CD to g:drive.
- unzip it into c:\unzipped.
- It got unzipped as jakarta-struts1.1 folder.
----------------------------------------------
We get three folders and 3 files
4. The 3 folders are
a) contrib
b) lib
c) webapps
============
The 3 files are:
5. a) readme.txt
b) license.txt
c)
instal.txt
-----------------------------------------------
6. open webapps folder.
7. we will find the following war files there.(
web-application-archives)
a )struts-blank
b)struts-documentation
c) struts-example
d) struts-exercise-taglib
e) struts-template
f) struts-upload
g) struts-validator
----------------------------------------------
8. copy struts-blank.war into:
g:\tomcat41\webapps folder.
----------------------------------------------
9. In a DOS window, cd to g:\tomcat41\bin
10. >set JAVA_HOME=D:\JDK1.4
11. startup
--------------------------------------------
12. tomcat server gets started .
13. Now in another dos window , check up that struts-blank.war has automatically
expanded by the server and we get a folder named
struts-blank.
14. If you expand struts-blank folder, there are 3 subfolders there.
i) META-INF
ii) WEB-INF
iii) pages
There is also an index.jsp
-----------------------------------------------
15. start the browser and type the url as :
http://localhost:8080/struts-blank/index.jsp
----------------------------------------------
16. You will get the corresponding display as follows:
Welcome!
To get started on your own application, copy the struts-blank.war to a new WAR
file using the name for your application. Place it in your container's "webapp"
folder (or equivalent), and let your container auto-deploy the application. Edit
the skeleton configuration files as needed, restart your container, and you are
on your way! (You can find the application.properties file with this message in
the /WEB-INF/src/java/resources folder.
-----------------------------------------------
Thus, we have correctly installed struts in our tomcat server.
==========================================
Inside the WEB-INF folder, we have the following subfolders and
files.
sub-folders
i)classes
ii) lib
iii)
src
-----------------------
files
===============
tld files ( 6 files)
===============
1) struts-bean.tld
2) struts-html.tld
3) struts-logic.tld
4) struts-template.tld
5) struts-tiles.tld
6) struts-nested.tld
-------------------
xml files ( 5 files).
==========================
i) web.xml
ii) struts-config.xml
iii) tiles-defs.xml
iv) validation.xml
v) validator-rules.xml
=============================
There are a number of jar files available in lib folder.
i) commons-beanutils.jar
ii) commons-collections.jar
iii) commons-digester.jar
iv) commons-fileupload.jar
v) commons-lang.jar
vi) commons-logging.jar
vii) commons-validator.jar
viii) jakarta-oro.jar
ix) struts.jar
-------------------------------------
All this is just for information.
===========================================
When we want to build our own application, we first copy struts-blank folder in
the webapps folder of tomcat & Rename the folder .(say) sam. And restart the
Tomcat server.
struts.jar was copied to c:\quick folder for easily remembering
the classpath.
==============================================
Now, we are ready to begin compiling.
We are now in g:\examplestruts\demo1
>set path=c:\windows\command;d:\jdk1.4\bin
>set classpath=g:\examplestruts;c:\quick\struts.jar;d:\tomcat41\common\lib\servlet.jar
..demo1>javac QueryForm.java
..demo1>javac sqlbean.java
..demo1>javac resultbean.java
..demo1>javac QueryAction.java
( if we compile the files one after the other, in this sequence,
we will be able to compile easily. After compiling, copy all the class files in
this folder to:
g:\tomcat41\webapps\sam\WEB-INF\classes\demo1
copy query.jsp & result.jsp to:
'g:\tomcat41\webapps\sam'
now in another dos
window, cd to:
g:\tomcat41\webapps\
sam\WEB-INF\struts-config.xml
edit the struts-config.xml as
follows:
==========================================
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation// DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/ dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="query name="que <action path path="/Query" ty type="demo1.QueryAction name="queryForm" scope=&q scope="session" input="/query.jsp" <forward name="success" path="/result.jsp" /> <forward name="failure" path="/query.jsp" />sp;</struts-config> </struts-config>
After all these steps, restart the Tomcat server, so that the settings become effective.
Type the URL in browser as:
'http://localhost:8080/sam/query.jsp'
You should get a form with orange background, etc. If we type the password as
'ADMINISTRATOR' and the sql as 'select * from table1', we will get the query
result. ( Ofcourse, the example presumes the usual creation of a simple Access
database named 'dbdemo' with just table1 having two fields ( name, place) and
registering it in ODBC. If the password is wrong, the page appears to be not
responding just showing whatever we typed. So, our adventure with Struts has had
a happy ending.
If we get, this far, we can and must build up further
by in-depth study of books.& articles.
In the forthcoming installments, we will see advanced Struts topics like :
DynaForm
Validator Framework
Tiles
RequestProcessor
Scaffold
Struts-Tags
------------------------
This tutorial cannot be a substitute for hundreds of articles and dozens of books on Struts .It is but a simple introduction .
Respectful Acknowledgements to:
- 'Struts your Applications'
by
Piyush SriVasthava
( DeveloperIQ---November-2002) - 'Understanding Struts Framework'
by
Manoj Kothale
( DeveloperIQ.....August-2003)
==================================
Suggested Reading:
- Struts in Action .
by
Ted Husted
( Manning press/ DreamTech publication) - Mastering Jakarata Struts
by
James Goodwill
(Wiley/DreamTexh) - Professional Struts Applications
by
John Carnell
( Wrox )