Struts framework is a widely used framework for java web development. It has been around for quite a while and has evolved into a powerful development framework.
It's just been a year since I learned Struts. During that time, I was using
Struts In Action as a guide to learn Struts. It did help a lot in terms of learning what the concept is behind the Struts framework, but it didn't teach you how to actually set it up from scratch. I tried looking for other tutorials online but what I did get are usually sample projects that have already been prepared for the user. And so I decided to create this simple tutorial for one to setup and run the simplest struts application.
This is the
simplest and
easiest way that I could think of. I won't be discussing much detail as to the concept behind struts. I would just be providing a startup point for learning and understanding the framework. I firmly believe that once you get this thing going, that's when you actually understand what ActionForwarding is really about, or what tiles actually do (experience + read documentation = understanding). Get into the action and you'll learn faster!
Anyway, you would be needing:
struts-blank.war <-- usually bundled together with the struts release (google it or download struts release from its website: Struts Apache)
eclipse IDE <-- IDE that we will be using
Tomcat <-- 4 or 5 would do.
Steps:1.0 Prepare our project1.1 Unpack struts-blank.war: the topmost folder is named struts-blank, and contents of the folder would include the usual WEB-INF, src, etc. Place this folder under the directory which you would like it to appear.
1.2 rename folder to a project name of your choice. I named mine.... Joytorial-Struts101
Note: you can actually not rename the file and simply use struts-blank folder name1.3 Here's the contents of our Joytorial-Struts101 Folder
2.0 Open our Project in EclipseNow we're ready to open the project into our IDE. I am using Eclipse 3.1.2.
2.1 Click on
FILE ->
NEW ->
PROJECT from menu.
2.2 From the popup screen, please choose
Java Project and press
NEXT button.
2.3 We would like to create a new project based from existing source (since we've already prepared our struts project). Below are the details of my project.
2.4 After setting up the project as seen above, clicking
NEXT would allow me to modify my project build files, sources, etc. And clicking
FINISH will allow me to go back to the main workspace and skip the other requirements of creating a new java project process. (I'd personally prefer modifying my project properties after I've opened the project itself in the workspace).
So here is what my navigator list would look like after I click on
FINISH:
2.5 Now we shall modify our project properties. To do so, please pick your
joytorial-Struts101 project folder, right click, and choose properties, after which, click on
Java Build Path.
There are two crucial actions we have to perform during this process:
2.5.1 Modify our output folder (as seen by the red marks from the figure below).
Note: We would want out compiled classes to go straight to our WEB-INF/classes folders as this is a web application we are creating. 2.5.2 We need a servlet library (
servlet-api.jar)! But where to we get it? We get it from our installed Tomcat Server. In my case, I've installed my tomcat 5 in C: and following its default directory system, I will find my servlet-api.jar on the following directory:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib
Note: It is not advisable that you copy the servlet-api.jar from your tomcat to your WEB-INF/lib directory because your project will now depend on the container version. If this is the case, everytime you change container version (Tomcat 4.0 to Tomcat 5.0 for example), then you'd have incompatability issues because the servlet-api.jar in your WEB-INF/lib is not of the same version as the servlet container you are using. Now, let us add the servlet-api.jar to our java build paths.
HOW? Here's a simple step by step instruction.
1. Go back to Project properties
2. Java Build Path
3. Click tab for Libraries
4. Click on button Add External Jars
5. Look for servlet-api.jar (either from the tomcat common/lib) and press open button
6. Press OK
7. We're Done!
3.0 Setup TomcatNow it's time to setup our conf file. In order for tomcat to know that your project is ready to run, you have to tell tomcat that it can access this project from its directory. How? By creating a simple xml file.
Creating these xml files may be complicated. But for us beginners, we'll use the simplest possible way to create such a file. It's up to you to further add more details to it. For now, our main goal is to simply get our first struts application up and running.
Go to: (or wherever your tomcat is located)
C:\Program Files\Apache Software Foundation\Tomcat 5.0\conf\Catalina\localhost
Get what's inside
Manager.xml, copy and paste it in a notepad, and save as "joytorial-Struts101.xml" (or whatever name you want as long as it's saved as an xml file.

Of course we have to modify what's inside as we've simply copied it off from Manager.xml!

In this context file, there are 2 definitions that we need to modify and take note of:
docBase - actual directory of your struts project
path - this is the pathname that you will be using to run your program in the browser (e.g. http://localhost:8080/path)
Note: In advance topics, this context file can still be modified in so many ways. 4.0 Run Run Run!Run your tomcat server.
Go to any browser you use (IE, Opera, Mozilla, etc) and type in: http://localhost:8080/joytorial-Struts101
The actual format of this address is actually: http://localhost:port/pathname
where port is usually 8080 or 8888 (depends on your tomcat server, or what is defined there), and path is the path name of the project you defined in the .xml file.
If you find this in your web browser:

There you have it! You now have a working struts application!