Wednesday, February 22, 2012

What is Struts

What is Struts

Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open source. Struts Framework is suited for the application of any size. Latest version of struts can be downloaded from We are using jakarta-struts-1.1 and jakarta-tomcat-5.0.4 for this tutorial.

The framework provides three key components:

A “request” handler provided by the application developer that is mapped to a standard URI.
A “response” handler that transfers control to another resource which completes the response.
A tag library that helps developers create interactive form-based applications with server pages.

The framework’s architecture and tags are buzzword compliant. Struts works well with conventional REST applications and with nouveau technologies like SOAP and AJAX.
Downloading and Configuring Struts

Download the Struts zip file.
Start at, or follow the link from I’m using Struts 1.1 in this tutorial.
Unzip into a directory of your choice.
For example, unzip into C:\jakarta-struts-1.1. Throughout the rest of this tutorial, I’ll refer to this location as struts_install_dir.
Update your CLASSPATH.
Add struts_install_dir/lib/struts.jar to the CLASSPATH used by your compiler or IDE (not your server). Here are three possible ways of setting it:

Set it in your autoexec.bat file. E.g., on Windows if you unzipped into C:\jakarta-struts-1.1, you would add the following to C:\autoexec.bat:
set CLASSPATH=C:\jakarta-struts-1.1\lib\struts.jar;%CLASSPATH%
On Unix/Linux, use your .cshrc, .bashrc, or .profile instead.
Set it using the system settings. On WinXP, go to the Start menu and select Control Panel, then System, then the Advanced tab, then the Environment Variables button. On Win2K/WinNT, go to the Start menu and select Settings, then Control Panel, then System, then Environment. Either way, enter the CLASSPATH value from the previous bullet.
Set it in your editor or IDE. Most IDEs have a way of specifying the JAR files needed to compile projects. Or, you could make a small .bat file (Windows) or shell script (Unix/Linux) that supplies the struts.jar file as the argument to -classpath for javac.
Install an XML parser.
If you have JDK 1.4 or Apache Tomcat, this step is not necessary, since they already come with an XML parser. But, if you use JDK 1.2 or 1.3 with another server, you might need to obtain the XML parsing libraries. Here are two good sources:
Testing Struts
Install struts-blank.war.
Install the Web application from struts_install_dir/webapps/struts-blank.war on your server. For example, with Apache Tomcat, copy struts_install_dir/webapps/struts-blank.war to tomcat_install_dir/webapps/.
Start or restart the server.
Most servers only recognize new Web apps when the server is started.
Access http://localhost/struts-blank/.
This URL assumes you are running the server on your desktop and are using port 80. In general, access http://hostname:port/struts-blank/. You should see something like the following.

Setting Up Struts Applications

To make your own Struts application, you need to create a Web application that has the appropriate JAR files, TLD files, and web.xml entries. You almost always do this by starting with the struts-blank application and modifying it. You have three options for doing this:
Accessing Struts Documentation

You probably already have bookmarked the APIs for standard Java, servlets, and JSP. But you will also want to frequently refer to the Apache Struts documentation. You can do this two ways:
Adding Struts to an Existing Web Application

By far the easiest way to develop a Struts application is to start with struts-blank, rename it, and work from there. If that approach is an option for you, do it that way and skip this section. However, if you already have an existing Web application and want to add Struts capabilities to it, starting with struts-blank won’t work. Adding Struts capabilities to existing Web apps is a huge pain in the neck, and will proably require several attempts to get it right. Here is a quick summary:

Copy JAR files from struts-blank/WEB-INF/lib to your_web_app/WEB-INF/lib.
Copy TLD files from struts-blank/WEB-INF to your_web_app/WEB-INF.
Copy struts-config.xml from struts-blank/WEB-INF to your_web_app/WEB-INF.
Copy the application properties file from struts-blank/WEB-INF/classes/resources to your_web_app/WEB-INF/classes/resources.
If you plan on using the automatic validator (see Section 5, Validating User Input), copy validation.xml and validator-rules.xml from struts-blank/WEB-INF to your_web_app/WEB-INF.
If you plan on using Tiles (see Section 6, Composing Pages with Tiles), copy struts-tiles.xml from struts-blank/WEB-INF to your_web_app/WEB-INF.
Copy declarations out of struts-blank/WEB-INF/web.xml into your_web_app/WEB-INF/web.xml. Most importantly, copy the servlet and servlet-mapping entries that map *.do to org.apache.struts.action.ActionServlet. Be sure you keep the entries in the same locations: the order of elements in web.xml matters.