There are a couple of points to be made with our simple JSP. First the
shows the use of the
ApplicationResource file that we mentioned earlier. This frees you from hard-coding text into your application.
<html:errors/> is what uses the
ActionErrors collection that we created to return any errors that occurred during validation in the
The form defined within the
<html:form action="login.action" focus="userName">
tag is really what starts the Struts process. The action that is defined here,
login.action, must match an
ActionMapping in the struts-config.xml file. From there, the appropriate actions, forms, and forwards are defined. On the submit of this form, the action is trapped by the Struts
ActionServlet. We'll discuss how that is defined below.
Our Welcome.jsp simply demonstrates how it's possible to use the standard mechanisms available in JSP to pass information from actions to pages:
<title>Welcome to Struts</title>
<p>Welcome <%= (String)request.getSession().getAttribute("USERNAME") %></p>
</p>You have logged in successfully!</p>
USERNAME attribute was set in the
Build the appropriate configuration files
We've spoken a lot about the struts-config.xml file. Usually, the contents of this file are built along the way. However, in this step in the development flow, it's good to take a step back and clearly examine the config file and make sure that everything matches. The final names of all of the
Action classes, JSPs, and forms should be well-known and defined in this file. The one thing that we haven't talked about yet is the web.xml file. This file is used by the JSP container, in this case Tomcat, to give specfic information regarding the application. The web.xml file for the StrutsSample application looks like:
<?xml version="1.0" encoding="ISO-8859-1"?>
This is the web-app configuration that allow the strutsSample to work under
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
<!-- Struts Tag Library Descriptors -->
<servlet> tag is used to define the instance of the
org.apache.struts.action.ActionServlet. In this case, we are calling it
oreilly. Two init parameters are worth calling out here: the application that defines the resource file that holds all of the strings for the applications, and the location of the struts-config.xml file. If you noticed, the url-pattern of the servlet mapping is the same extension we used in our form action attribute. The servlet mapping tells Tomcat to send all requests that end in .action to the
oreilly servlet. You can specify whatever pattern you want here. You might see references to the .do extension in Struts, but I think .action is more explicit. The welcome file list is the default page to display when accessing the application. Finally, we list any of the Struts taglibs we might be using.
Now that we have all of the pieces in place, it is relatively easy to build, test, and deploy your application. Using Ant to build the application is relatively painless. If you haven't used Ant before, I would definitely recommend looking into it. It is easy to learn and provides a clean way to maintain a build environment. I've put together a build.xml file that works with this sample. You can download all of the files used in this article, build and then create a strutsSample.war file by simply typing Ant in the directory where build.xml is located. You need to first download and install the Ant release. It takes about 10 minutes to download and set up.
Our application structure is as follows:
Struts config files (struts-config.xml, web.xml)
Classes directory (strutsSample application package structure)
Lib directory (struts.jar)
Once you have your application in a .war file, you just put it in the webapps directory of Tomcat, and start Tomcat. The .war file will be expanded and a new context will be created in Tomcat for the application. The web.xml file that we created tells Tomcat where to get the rest of the information it needs about the application. You should then be able to access the application by typing http://localhost:8080/strutsSample. Tomcat defaults to use port 8080, so no special setup is required. Login.jsp will display as the welcome page, and you can test away.
In this series of articles, we walked through the entire process of taking an application requirement and building an application from scratch using the Struts framework. While there are more files involved with Struts than with JSP, there are definite benefits in terms of being able to use an MVC model and apply it to a complex environment. The first application takes the most time just getting accustomed to where all the pieces fit together.
Hopefully with the help of this series of Struts articles, you now have a good understanding of exactly what the components are, where they fit, what's necessary, and a good development flow to follow. Struts is just in its infancy and I think will prove to be a valuable tool for doing what we all like to do -- build applications.
Read more JSP and Servlets columns.
Return to ONJava.com.