JBoss.org Community Documentation

Struts Tools Tutorial

Version: 1.0.0.GA


1. Introduction
2. Getting Started Guide for Creating a 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
2.3. Generating Stub Coding
2.4. Coding the Various Files
2.4.1. Java Stub Classes
2.4.2. JSP Pages
2.5. Compiling the Classes
2.6. Running the Application
2.7. Other relevant resources on the topic
3. Getting Started Struts Validation Examples
3.1. Starting Point
3.2. Defining the Validation Rule
3.3. Client-Side Validation
3.4. Server Side Validation
3.5. Editing the JSP File
3.6. Editing the Action
3.7. Editing the Form Bean
3.8. Other Resources

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 Developer Studio. 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 Developer Studio. You will design the application, generate stub code for the application, fill in the stub coding, compile the application, and run the application all from inside JBoss Developer Studio.

We assume that you have already launched Eclipse with JBoss Developer Studio 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 dont 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 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 = "";

     private String greetName = "";
this.name = "";

     this.greetName = "";
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 = "";
    private String greetName = "";
    
    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public String getGreetName() 
    {
        return greetName;
    }
    
    public void setGreetName(String greetName) 
    {
        this.greetName = greetName;
    }
    
    public GetNameForm() 
    {
    }
    
    public void reset(ActionMapping actionMapping, HttpServletRequest request) 
    {
        this.name = "";
        this.greetName = "";
    }
    
    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.

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.

You can also read Struts chapter in our "Visual Web Tools" guide for more information on this topic.