JBoss.orgCommunity Documentation

Struts Tools Tutorial

Version: 3.0.0.GA

April 2008


1. Introduction
1.1. 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 Window > Open Perspective > Other > Web 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 any code for the files, but only create them as placeholders so that we can create links to them in the diagram. We will write the 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 JavaSource > sample 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.

private String name = "";
this.name = "";
ActionErrors errors = new ActionErrors();

     return errors;

The final GetNameForm.java file should look like this:

package sample;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class GetNameForm extends org.apache.struts.action.ActionForm 
{
    
    private String name = "";
    
    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public GetNameForm() 
    {
    }
    
    public void reset(ActionMapping actionMapping, HttpServletRequest request) 
    {
        this.name = "";
    }
    
    public ActionErrors validate(ActionMapping actionMapping, 
            HttpServletRequest request) 
    {
        ActionErrors errors = new ActionErrors();
        return errors;
    }
}
String name = ((GetNameForm)form).getName();

String greeting = "Hello, "+name+"!";
((GetNameForm)form).setName(greeting);
return mapping.findForward(FORWARD_sayHello);

The final version of GreetingAction.java should look like this:

package sample;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class GreetingAction extends org.apache.struts.action.Action 
{
    
    // Global Forwards
    public static final String GLOBAL_FORWARD_getName = "getName";
    
    // Local Forwards
    public static final String FORWARD_sayHello = "sayHello";
    
    public GreetingAction() 
    {
    }
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        String name = ((GetNameForm)form).getName();
        String greeting = "Hello, "+name+"!";
        ((GetNameForm)form).setName(greeting);
        return mapping.findForward(FORWARD_sayHello);
    }
}

The last thing left to do is to code the JSP files whose editors should still be open from having been created as placeholders.

In this page, the user will enter any name and click the submit button. Then, the greeting action will be called through the form.


  • In the Insert Tag dialog box, type in name for property and select Finish

  • In the StrutsForm library in the JBoss Tools Palette, select submit , and drag it to right after the text box in the Visual pane of the editor

  • Right-click the submit button and select <html:submit> Attributes from the context menu

  • In the Attributes dialog box, select the value field and type in "Say Hello!" for its value

After tidying the page source, the Editor window for the file should look something like this:


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 JBoss Server view.


  • Start up JBoss AS by clicking on the icon in JBoss Server 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 StrutsHello > Configuration > default > struts-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 File > Save 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.tld" prefix="html" %>
<html:html>
<head>
    <title>Input name</title>
    <html:javascript formName="GetNameForm"/>
</head>
<body>
    <html:form action="/greeting.do" onsubmit="return
        <para>validateGetNameForm(this)"></para>
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><b>Input name:</b></td>
            </tr>
            <tr>
                <td>
                    <html:text property="name" />
                    <html:submit value=" Say Hello! " />
                </td>
            </tr>
        </table>
    </html:form>
</body>
</html:html>

  • In the browser window, click on the "Say Hello!" button without having entered any name in the form

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

The file should now look like this:

package sample;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class GetNameForm extends
org.apache.struts.validator.ValidatorForm 
{
    private String name = "";
    
    /**
    * @return Returns the name.
    */
    public String getName() 
    {
        return name;
    }
    
    /**
    * @param name The name to set.
    */
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public GetNameForm () 
    {
    }
    
    public void reset(ActionMapping actionMapping, 
        HttpServletRequest request) 
    {
        this.name = "";
    }
    
    // public ActionErrors validate(ActionMapping actionMapping,
        HttpServletRequest request) 
    {
        // ActionErrors errors = new ActionErrors();
        // return errors;
        // }
    }

The error message should appear in a refreshed version of the form.

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 hole 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.