JBoss.org Community Documentation

Chapter 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

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

We are first going to create a new project for the application.

A "StrutsHello" node should appear in the upper-left Package Explorer view.

At this point, its empty except for the background grid lines.

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 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 = "";

     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.

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

Everything is now 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.

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