How do I set up a Struts 1 project?
Author: Deron Eriksson
Description: This Java tutorial walks through setting up a Struts 1 project in Eclipse.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 3 >

ApacheSW StrutsW is a framework for creating Model-View-Controller-based JavaSW web applications. Struts exists in two versions, Struts 1 and Struts 2. This tutorial will cover Struts 1. To begin with, you can download the Struts libraries, documentation, source code, and sample applications from http://struts.apache.org/download.cgi.

For this tutorial, I downloaded the struts-1.3.5-lib.zip file. We find the following jarW libraries within this zip file.

Struts jar libraries

Not all of the above libraries are necessary to use the basic functionality of Struts. One way to figure out which jar files are needed is to set up a project, start the project, and include required jar files until all the errors (many of which are runtime) disappear when you run the application. If we follow this technique, we find that for this tutorial, commons-beanutils-1.7.0.jar, commons-chain-1.1.jar, commons-digester-1.6.jar, struts-core-1.3.5.jar, and struts-taglib-1.3.5.jar are the required jar files.

Our struts-demo project is shown below, with the required jar files added to the /lib directory. These jar files are added to the build path of the project.

jars added to lib directory

Next, I'll describe the various files in the project and how they all fit together. The best place to start is the web.xmlW file. First off, notice that we utilize the Struts ActionServlet. This servletW serves as our controller, which means that this servlet decides where to send the browser when it makes a request of our application. Notice that the 'config' init parameter points to '/WEB-INF/struts-config.xml', which contains the various Struts mappings and things, which we'll describe soon. The ActionServlet is configured to start up when our application starts up (rather than the first time the servlet is hit).

Notice that requests made to our application ending with '.do' map to the ActionServlet. In Struts, a particular '.do' maps to a particular Action to perform. So, if a particular URL is hit (which may or may not include parameters), the ActionServlet decides where to send the request to the application.

Finally, notice that index.jsp is a welcome file. So, if we hit our application, the default page that is hit is index.jsp.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts-demo" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Struts Demo</display-name>
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Next let's examine index.jsp. The top of the JSPW has 4 Struts taglibs referenced, although only one is actually needed. The logic:redirect tag instructs our application to be forwarded to 'howdy'. Where does this 'howdy' go?

index.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<logic:redirect forward="howdy"/>

The last question is answered by examining our struts-config.xml file. This file contains a global-forwards section, which contains 'forward' mappings that can be used throughout our application. In this section, we can see that 'howdy' maps to the 'myStart.do' Action. Notice that this file contains an action-mappings section, and one of the actions in this section is 'myStart', which forwards a request to the 'start.jsp'. Although this may seem a little confusing at first, this means that if we make a request to index.jsp, the request will get forwarded to 'howdy', which maps to 'myStart.do', which maps to the 'start.jsp'.

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans></form-beans>
	<global-exceptions></global-exceptions>
	<global-forwards>
		<forward name="howdy" path="/myStart.do" />
	</global-forwards>
	<action-mappings>
		<action path="/myStart" forward="/start.jsp" />
		<action path="/myTest" type="test.TestAction">
			<forward name="success" path="/success.jsp"/>
			<forward name="failure" path="/failure.jsp"/>
		</action>
	</action-mappings>
	<message-resources parameter="StrutsDemoResources" />
</struts-config>

(Continued on page 2)

Page:    1 2 3 >