Step 1: The directories and files of a Struts application
You'll need to know the directory structure of a Struts application, and
what's put in the directories. The "struts-blank" application is well-suited for
this presentation, since it contains a minimum number of files:

- Figure 3: The Struts directory structure -
|
File or Directory name |
Purpose |
|
META-INF |
Contains meta information. Used by utilities etc. |
|
WEB-INF/classes |
This is where you place you own Java classes. |
|
WEB-INF/classes/ApplicationResources.properties |
Contains the messages (fixed texts) of the application. Error messages
are also put here. |
|
WEB-INF/lib/struts.jar |
Contains the Struts servlet, helper classes, taglib code etc.
|
|
WEB-INF/*.tld |
The Struts tag libraries. |
|
WEB-INF/struts-config.xml |
A Struts configuration file. More on this later. |
|
WEB-INF/web.xml |
The usual configuration file for the servlet container. More on this
later. |
|
index.jsp |
The jsp-files (and html-files) may be placed in the root of the
application directory. "struts-blank" contains this single
jsp-file. |
- Table 1: The files and directories in a Struts application -
Step 2: Starting on a new Struts application
The easiest way to start on a new Struts application is to take an existing
one--for example "struts-blank"--and make a copy of it in the "webapps"
directory. When you get more experienced with Struts you'll probably add your
own standard classes and files to your setup, and you'd then start taking a copy
of this directory structure. For our first application we simply copy all files
and directories in the folder "struts-blank" and rename the folder to
"myproject". The contents of some of the files must now be edited.
Step 3: The web.xml file
The web.xml file is where servlets and other stuff are defined to the servlet
container. We'll remove some unnecessary things from the web.xml file so it
looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app> |
- Figure 4: The web.xml file -
The file contains three sections:
- the definition of the Struts servlet named "ActionServlet"
- the URL mapping for the calls to this servlet
- the definitions of the Struts tag libraries
You'll see that the servlet will be called if our browser requests a file
called <some-name>.do. So when we submit the form in our one-page
application we'll decide to use the action-name "submit.do". How the Struts
servlet knows what to do with this request we'll discover next.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|