Introduction to the Struts Web Framework 1

 

 

 

 

Note: The original document can be found at: An Intro to Struts web framework & NetBeans. This is a refined version with details steps.

 

This document takes you through the basics of using NetBeans IDE to develop web applications using the Struts web framework. Struts is an open source framework that extends the Java Servlet API and employs a Model, View, Controller (MVC) architecture. It enables you to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, JavaBeans, resource bundles, and XML.

This tutorial teaches you how to build a simple MVC application that displays a login page and returns a success page upon submitting data that passes validation. You learn several basic features provided by Struts, as well as how these features are implemented using the IDE. Specifically, you use Struts tags in JSP pages, maintain user data with a Struts ActionForm bean, and implement forwarding logic using a Struts Action object. You are also shown how to implement simple validation to your application, including setting up warning message for a failed login attempt.

For a more fine-grained introduction to Struts, see How does Struts work? On the official Struts website. Also, make use of the IDE's Javadoc Index Search (Help > Javadoc Index Search) to view the Struts Framework API, which is packaged with the Struts libraries.

 

Contents

  1. Overview of the Application

  2. Setting Up a Struts Application

  3. Creating JSP Pages

  1. Creating a Login Page

  2. Creating a Success Page

  1. Creating an ActionForm Bean

  2. Creating an Action Class

  3. Implementing Validation

  1. Accessing Bean Data and Preparing a Forwarding Condition

  2. Setting Up an Error Message

  1. Adding forward Entries to struts-config.xml

  2. Configuring and Running the Application

  1. Setting the Welcome Page

  2. Attaching a Stylesheet

  3. Running the Application

 

To complete this tutorial, you need the following software and resources.

 

Software or Resource

Version Required

NetBeans IDE

Java, version 6.8

Java Development Kit (JDK)

version 5 or 6

GlassFish application server or Tomcat Servlet container

v2 or v3

version 6.x

 

Notes:

  1. The Web and Java EE installation enables you to optionally install the GlassFish (v2 or v3) application server and the Apache Tomcat Servlet container 6.0.18. You must install one of these (or register a different server in the IDE) to work through this tutorial.

  2. If you need to compare your project with a working solution download it here.

 

Overview of the Application

 

When you use Struts, the framework provides you with a controller servlet, ActionServlet, which is defined in the Struts libraries that are included in the IDE, and which is automatically registered in the web.xml deployment descriptor as shown below. The controller servlet uses a struts-config.xml file to map incoming requests to Struts Action objects, and instantiate any ActionForm objects associated with the action to temporarily store form data. The Action object processes requests using its execute method, while making use of any data stored in the form bean. Once the Action object processes a request, it stores any new data (i.e., in the form bean, or in a separate result bean), and forwards the results to the appropriate view.

 

Struts workflow diagram

 

Developing a Struts application is similar to developing any other kind of web application in NetBeans IDE. However, you complement your web development toolkit by taking advantage of the Struts support provided by the IDE. For example, you use templates in the IDE to create Struts Action objects and ActionForm beans. Upon creation, the IDE automatically registers these classes in the struts-config.xml file and lets you extend this file very easily using menu items in the Source Editor's right-click menu. Because many web applications use JSP pages for the view, Struts also provides custom tag libraries which facilitate interaction with HTML forms. Within the IDE's Source Editor, you can invoke code completion and Javadoc support that helps you to work efficiently with these libraries.

The following steps demonstrate how to create a simple form that collects user data, performs simple validation, and outputs the data on a success page.

 

Setting Up a Struts Application

 

In the IDE, a Struts application is nothing more than a normal web application accompanied by the Struts libraries and configuration files. You create a Struts application in the same way as you create any other web application in the IDE - using the New Web Application wizard, with the additional step of indicating that you want the Struts libraries and configuration files to be included in your application.

 

1.      Choose File > New Project. Under Categories, select Java Web. Under Projects, select Web Application and click Next.

 

NetBeans - New Project menu

 

New Java web application NetBeans project template

 

2.      In the Name and Location panel, enter MyStrutsApp for Project Name and click Next. You may want to change the project location and folder if needed.

 

NetBeans New Web Application project template

 

3.      In the Server and Settings panel, select the server to which you want to deploy your application. Only servers that are registered with the IDE are listed. (To register a server, click Add next to the Server: drop-down list.) Also, note that the Context Path to your deployed application becomes /MyStrutsApp. Click Next.

 

NetBeans new web application web server selection and Java EE version

 

4.      In the Frameworks panel, select Struts (Struts 1.3.8 in this case).

 

 

 

 

 

 

----------------------------------------------------------

 

NetBeans new web application - Struts framework selection

 

For purposes of this tutorial, do not change any of the configuration values in the lower region of this panel. These are the following:

 

 

5.      Click Finish. The IDE creates the project folder in your file system. As with any web application in the IDE, the project folder contains all of your sources and the IDE's project metadata, such as the Ant build script. However, your web application in addition has all of the Struts libraries on its classpath. Not only are they on the application's classpath, but they are included in the project and will be packaged with it later when you build the project.

 

NetBeans new web application struts framework project template creation in progress

 

The project opens in the IDE. The Projects window is the main entry point to your project sources. It shows a logical view of important project contents. For example, if you expand several nodes within the new project, it may appear as follows:

 

NetBeans with struts framework project - struts-config.xml file

 

Note: Use the Files window (Window > Files) to see all of your project contents in a directory-based view.

 

NetBeans with struts framework project - generated files and folders template

 

The Struts-specific configuration files, as well as the application's deployment descriptor, are conveniently placed within the WEB-INF folder. Open the deployment descriptor (double-click the web.xml file node to have it display in the Source Editor). In order to handle Struts processing, a mapping is provided for the Struts controller servlet:

 

<servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

        <param-name>config</param-name>

        <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

        <param-name>debug</param-name>

        <param-value>2</param-value>

    </init-param>

    <init-param>

       <param-name>detail</param-name>

       <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>

 

NetBeans with struts framework project - the web.xml file content

 

Above, the Struts controller servlet is named action and is defined in the Struts library (org.apache.struts.action.ActionServlet). It is set to handle all requests that satisfy the *.do mapping. In addition, initialization parameters for the servlet are specified by means of the struts-config.xml file, also contained in the WEB-INF folder.

 

 


Back to Main | Strut & Web 2