0 to struts in 60 minutes

Steps to build a struts application

Struts has become a standard for web applications. It lets you fit your web application in a MVC framework - which earlier I had wrongly thought - MVC is only for desktop GUI clients.

What is MVC?


Model deals with the data - JavaBeans containing the data.
View deals with how you want to display the model
Controller - How the model and view are to be controlled

Lets take the example of a login process.

Here is the scenario : User enters login name, password hits submit. The form data are sent to a servlet that talks to the database to see if the information is valid. If it is valid, it puts the name, role information in a javabean and sends the browser to a welcome page. If not it sends the browser an error page.

Now with MVC scenario. When the user hits submit, the control does not directly go to the servlet but to a controller servlet. The form values are put in a javabean ( model ). The controller's main job is to see what the request is and finds the correct servlet to work on it - in this case the login servlet. The login servlet then authenticates and sends either of these two words : success or failure. Also if the authentication succeeds it puts the name,role information into a javabean ( the model ). The controller if it sees the return value as "success" sends the browser the welcome page ( view ) . The welcome page takes the value from the javabean ( model ) and displays it. You might be thinking what do we call the login servlet - the model or the controller.You might also think it lies somewhere between the model and the controller - but actually its the model. It just tells the controller where the logic flow should be going next - but it cannot change the logic flow by itself. Only the almighty controller has the power to change the logic flow.

What are the advantages of MVC?

* The control logic is in one place. Otherwise the servlets, jsps have fun throwing the logic between one another - and when it comes to maintenance mode its a headache when one has to follow the trial. Now if we have all the logic flows in one place its enough to go to that one place and see how things flow.
In Struts this logic flow, controller, is held in an xml file - struts-config.xml. Now you can play with the logic flow - lets say for some reason you do not want to show the welcome page but want to directly take the users to another page. You need not touch the servlets ( means compiling, testing etc. ) - vs - you touch the struts-config.xml file - make the change you want to do - and restart the server. ( no recompiling, testing is necessary yes - but need not worry about the servlets throwing any exception etc.. as we never touched the servlets ).

* Lets say you want to toss out the current JSP view and have a new design. All you have to make sure is you display the javabeans in the right places. The Model and controller need not be touched at all. Isnt it beautiful !!

* Lets say you change the model - for instance your application goes the EJB way. Authentication is not done with a simple servlet but by a complex session bean. As long as the session bean creates the same javabean with name,role - the View(JSP page) need not be touched at all.

* Lets say tomorrow a new version of Struts comes - or another alternative to Struts comes along - it has a new controller - instead of struts-config.xml file they change a couple of things. No problem your model, view never get affected.

What you need?

A web browser

Where to start?

Go to the wonderful Apache site.

Download the binaries. Unzip it.

There will be a directory called webapps. There is a nice little template file called struts-blank.war. Rename it as antiPC.war ( anti ProCrastinator - thats the name of our struts app - you can name it whatever you want )

Start tomcat.
Go to tomcat manager ( http://localhost:8080 ) and deploy this war file. Tomcat will neatly unzip the war file and deploy the application too. Click on the link for antiPC


You should be able to see the welcome message. If not try to fix the problem. Look at catalina.out to see if there is any error messages, missing class library anything.

And then?

Time to start working on our struts application.
Fire up eclipse.
If you havent installed sysdeo eclipse plugin its a good idea to install it. Google for sysdeo plugin and load it in. Its useful.
Start a new Java Tomcat project (antiPC) and make it point to webapps/antiPC directory.

PS: If its not windows OS - eclipse might start throwing tantrums saying file not found. In your terminal editor navigate to the directory where webapps is. See the ownership of antiPC directory - it might be root. So do a
sudo chown -R youruserid antiPC

build.xml - the oxygen for your struts app !! It cannot live without it.

In eclipse navigate to antiPC/WEB-INF/src/build.xml

The build.xml cleans,compiles,generates javadoc and creates war file for deploying. We can remove the last 2 steps for now.

So make these changes

from :

<!-- Build entire project -->

<target name="project" depends="clean,prepare,compile,javadoc"/>


<!-- Build entire project -->

<target name="project" depends="clean,prepare,compile"/>

and line 1 :

from :

<project name="blank" basedir="../" default="all">


<project name="blank" basedir="../" default="project">

Click on the WEB-INF/src directory and click on Run as ant build. It should say something like this :

Buildfile: /usr/local/tomcat/webapps/antiPC/WEB-INF/src/build.xml
[delete] Deleting directory /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
[mkdir] Created dir: /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
[copy] Copying 1 file to /usr/local/tomcat/webapps/antiPC/WEB-INF/classes
Total time: 4 seconds

And thennnnn?

You are all set to play with your struts application. Next I will explain how to set up a login page, connect to database, etc.


  1. Anonymous2:15 AM

    for someone who knows zero about struts % cia, it was quite enjoyable


  2. Hi,

    I just came to your page while I was looking for a solution to a struts problem. Quite a lot of effort has been put here....

    Keep going..

  3. Anonymous11:25 PM

    Awesome, thanks for this wonderful things for struts. Very looking forward for the Part 3.

  4. Anonymous11:25 PM

    Awesome, thanks for this wonderful things for struts. Very looking forward for the Part 3.


Post a Comment

Popular posts from this blog

Sufi you are an illegal immigrant as of today

Shahul Hameed still lives...

Avalum Naanum Lyrics