Struts and Eclipse

We'll start by taking a look at the Struts example we're going to create using Eclipse, using the latest version of Tomcat available (Version 4.1.29 as of this writing), and the latest version of Struts (Version 1.1). When you navigate to the opening JSP, http://localhost:8080/Ch10_01/Ch10_01.jsp, you'll see a menu with a number of HTML controls, as appears in Screenshot-1.

Screenshot-1. The opening view of the Struts app
Java figs/ecps_1001.gif

The user can order a pizza or sandwich using the controls here and can also include his email address. Clicking the "Place your order" button sends the data in the form to an underlying bean and to the app's controller servlet, which displays a summary of the order, as you see in Screenshot-2. Although these results look pretty simple, that's not to say that the implementation of this app is easy. Struts is not a lightweight framework; to create this example, you use these files, arranged in the Tomcat webapps/Ch10_01 directory:

webapps
|
|_ _Ch10_01
 | Ch10_01.jsp [View: Starting form]
 | Ch10_05.jsp [View: Summary form]
 |
 |_ _WEB-INF
 | struts-config.xml [Struts Configuration File]
 | Ch10.tld [Custom Tag Definitions]
 | Struts TLD files [Struts Tag Definitions]
 | web.xml [app Descriptor File]
 | |_ _lib
 | struts.jar [Java Archive of Struts Classes]
 | |_ _classes
 | appResources.properties [Contains property data]
 |
 |_ _org
 |
 |_ _eclipsebook
 |
 |_ _ch10
 |_ _Ch10_02.class [Custom Tag 1 Implementation]
 Ch10_03.class [Custom Tag 2 Implementation]
 Ch10_04.class [Controller: Action servlet]
 Ch10_06.class [Model: Form bean]


Screenshot-2. The Struts app's summary
Java figs/ecps_1002.gif

The next step is to create this app using Eclipse. To follow along, create a simple Java project named Ch10_01, and create a new folder in the Tomcat webapps directory, Ch10_01. To get access to this folder in Eclipse, create a new folder by right-clicking the project and selecting NewScreenshot Folder; name this folder deployment, and link it to the webapps\Ch10_01 folder. Now, in Eclipse, create the folders you need for Java web apps—WEB-INF, lib, and classes—which will also create these folders in the Tomcat webapps\Ch10_01 folder:

deployment
 | |_ _WEB-INF
 | |_ _lib
 | |_ _classes


To handle the compiled output of this project, select the ProjectScreenshot Properties menu item, click the Browse button next to the Default output folder box, create a new folder named output linked to the classes directory, make sure that folder is selected in the Folder Selection dialog, and click OK. Doing so ensures that the compiled code will go into this directory (actually into classes\org\eclipsebook\ch10, following the name of the package we'll create here). In this case, we purposely didn't create a src directory to hold the source code when creating this project as we have in the past, in order to demonstrate that it's easy enough to create a source code folder after a project has been created. To create a new source code folder, right-click the project and select NewScreenshot Source Folder. Name the new source folder src. Now add servlet.jar to the build path (it's in the Tomcat common\lib directory), as well as the Struts support JAR file, struts.jar. You can get struts.jar free at http://jakarta.apache.org/struts/ in compressed format (the current version is 1.1). Unzip or untar the download to get struts.jar, and place that file in the app's lib directory. Besides struts.jar, we'll also need these JAR files from the download in this example, so place them in the lib directory as well:

  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar

You'll also need these Struts .tld (Tag Library Definition files) from the download in the app's WEB-INF directory; these files support the custom tags that Struts uses:

  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-template.tld

To get started with the code for this example, right-click the new folder and select NewScreenshot Package to add a new package, org.eclipsebook.ch10, to the src folder. That sets up the build environment in Eclipse. Now it's time to write some code.

      
Comments