JBoss.orgCommunity Documentation

Struts Tools Tutorial

Version: 4.0.0


1. Introduction
1.1. Key Features Struts Tools
1.2. Other relevant resources on the topic
2. Creating a Simple Struts Application
2.1. Starting Up
2.2. Creating the Application Components
2.2.1. Creating JSP Page Placeholders
2.2.2. Creating an Action Mappings
2.2.3. Creating a Link
2.2.4. Creating a Forward
2.2.5. Creating a Global Forward
2.2.6. Creating a Form Bean
3. Generating Stub Coding
4. Coding the Various Files
4.1. Java Stub Classes
4.1.1. GetNameForm.java
4.1.2. GreetingAction.java
4.2. JSP Pages
4.2.1. inputname.jsp
4.2.2. greeting.jsp
4.2.3. index.jsp
5. Compiling the Classes and Running the Application
6. Struts Validation Examples
6.1. Starting Point
6.2. Defining the Validation Rule
6.3. Client-Side Validation
6.4. Server Side Validation
6.5. Editing the JSP File
6.6. Editing the Action
6.7. Editing the Form Bean
7. Other Relevant Resources on the topic

The following chapters describe how to deal with classic/old style of Struts development. We recommend users to use JBoss Seam to simplify development, but until then you can read about classical Struts usage here.

We are going to show you how to create a simple Struts application using the JBoss Tools. The completed application will ask a user to enter a name and click a button. The resulting new page will display the familiar message, "Hello <name>!"

This document will show you how to create such an application from the beginning, along the way demonstrating some of the powerful features of JBoss Tools. With the help of our tutorial you will design the application, generate stub code for the application, fill in the stub coding, compile the application, and finally run it all from inside the Eclipse.

Firstly, we assume that you have already launched Eclipse with JBoss Tools installed and also that the Web Development perspective is the current perspective. (If not, make it active by selecting WindowOpen PerspectiveOtherWeb Development from the menu bar.)

Now, we will design the application by creating the individual components as placeholders first. (We don't have to complete all of the details inside the components until afterwards.)

Next, let's create and place two JSP pages. We will not write anything more than basic template code for the files; they will serve only as placeholders so that we can create links to them in the diagram. We will write some custom code a little bit later.

We are done with designing the application through the diagram. Now we need to write code for the action component. We also need to write an action class for the /greeting mapping along with a FormBean. To aid in the coding phase, JBoss Developer Studio can generate Java class stubs for all of the components shown in the diagram.

You should see a screen that says:

Generated classes: 2

Actions: 1

Form beans: 1

The Java files will be generated in a JavaSourcesample folder that you can see in the Package Explorer view under the "StrutsHello" node. One Action stub and one FormBean stub will have been generated.

We will now code both the Java stub classes just generated, the JSP files left in as placeholders from previous steps, and a new start JSP page we will have to create.

As this is the Eclipse environment, no explicit compilation step is required. By default, Eclipse compiles as you go.

Thus at this point everything is ready for running our application without having to leave JBoss Developer Studio by using the JBoss Application Server engine that comes with the JBoss Developer Studio. For controlling JBoss AS within JBoss Developer Studio, there is Servers view.


  • Start up JBoss AS by clicking on the icon in Servers view. (If JBoss AS is already running, stop it by clicking on the red icon and then start it again. Remember, the Struts run-time requires restarting the servlet engine when any changes have been made.)

  • After the messages in the Console tabbed view stop scrolling, JBoss AS is available. At this point, right-click on the getName global forward in the struts-config.xml diagram view and select Run on Server.

The browser should appear with the application started.

Validation of input is an important part of any Web application. All Apache Jakarta frameworks, including Struts, can use a common Jakarta Validation Framework for streamlining this aspect of Web application development. The Validation Framework allows you to define validation rules and then apply these rules on the client-side or the server-side.

JBoss Developer Studio makes using the Validation Framework in Struts even easier with the help of a specialized editor for the XML files that controls validation in a project. In this document, we'll show you how this all works by creating some simple client-side validation and server-side validation examples.

In these steps you will set up the validation that can be used for either client-side or server side validation. You need to enable validation as a part of the project, define an error message, and tie it into an appropriate part of the application.


  • Expand the "form-beans" node under the StrutsHelloConfigurationdefaultstruts-config.xml node. Then, drag the form bean "GetNameForm" and drop it onto a formset in the XML Editor

  • In the Validation Editor, expand the formset node, right-click GetNameForm, and select Create Field... from the context menu

  • Enter a name for Property in the dialog box. A new property will be created:


  • In the Properties view for the name field to the right of the "tree" for the validation.xml file, click on the Change...button next to the Depends entry field

  • In the displayed double list, select required from the left list and then click Add

  • Click Ok

  • Right-click name and select Add Arg... from the context menu

  • In the Add Arg dialog box, click on the Change...button next to the Key field

  • In the Key dialog box that appears now, click on the Add button

  • Enter "name.required" in the Name field, and enter a person's name in the Value field

  • Click Finish, then Ok, and then Ok again

  • Select FileSave All from the menu bar

Client-side validation uses a scripting language (like JavaScript) running in the client browser to actually do the validation. In a Struts application using the Validation Framework, however, you don't actually have to do any of the script coding. The Validation Framework handles this.

To see how this works in our application, you'll just need to make a couple of modifications to one of the JSP files.

onsubmit="return validateGetNameForm(this)"

    

The file should now look like this:


<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<html:html >
    <head>
        <html:javascript formName="GetNameForm"/>
	    <title></title>
    </head>
    <body>
        <html:form action="/greeting.do" onsubmit="return validateGetNameForm(this)">
            Input name:<html:text property="name"/><html:submit value="Say Hello!"/>
         </html:form>
    </body>
</html:html>

    

A JavaScript error message should be displayed in an alert box.

Our reference: Struts Tools Reference Guide

Apache Struts: Struts Technology

Struts 2: Apache Struts 2

Get Started: Struts Getting Started

Struts on IBM: Struts - An open-source MVC implementation

FAQ: Struts FAQ

Download: Release of Apache Struts

Thus, this tutorial should help you to execute the whole development circle for building a sample Struts-based Web application using JBoss Tools bundle of Eclipse plugins starting from organizing a new Struts project and ending with the running and deploying it onto the JBoss Server.

To find out all the features of JBoss Tools for working with Struts refer to our Struts Tools Reference Guide. If you still have questions you are always welcome on JBoss Tools Forum.