This document takes you through the basics of using NetBeans IDE 5.0 to develop web applications that make use of the Struts framework. The Struts framework enables you to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, resource bundles, and XML.
Struts works with a Model, View, Controller (MVC) framework, but only provides the controller—a servlet, which is included in the Struts libraries that the IDE provides and automatically registers in the web.xml deployment descriptor when you indicate that you want to use Struts. The Struts servlet uses the struts-config.xml file to map incoming requests to a Struts "action" class. An action class receives a Struts "actionform bean" class as input, which serves as a transfer object between the action class and the view, which is typically a JavaServer Pages (JSP) page. Because many web applications use JSP pages for the view, Struts provides custom tag libraries which facilitate interaction with HTML forms.
This document is designed to get you going as quickly as possible. The following topics are covered:
At the end of this tutorial, you will have a very simple semi-functioning login page. You will have learnt several basic features provided by Struts and you will also have learnt how these features are implemented via the IDE. Specifically, you will use Struts tags in a JSP page, validate a field using a Struts actionform bean class, and navigate between pages using a Struts action class. You will also be shown how to add various ancillary Struts functionalities, such as "Cancel" and "Logout".
For specific information on how Struts works, see How does it work? on the Struts website. For general information on working with the IDE, see the Support and Docs page on the NetBeans website.
Before you start writing code, you have to make sure you have all of the necessary software and that your project is set up correctly.
Before you begin, you need to install the following software on your computer:
Optionally, you can download and use the Sun Java System (SJS) Application Server Platform Edition, JBoss, or WebLogic. However, the Tomcat Web Server that is bundled with the IDE provides all the support you need for two-tier web applications such as the one described in this quick start guide. An application server (such as the SJS Application Server, JBoss, or WebLogic) is only required when you want to develop enterprise applications.
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, with the additional step of indicating that you want the Struts libraries and configuration files to be included in your application.

Do not change any of the values in the lower section of this panel. They serve the following purposes:
The IDE creates the $PROJECTHOME/LoginPage project folder in your filesystem. 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, in addition, your web application 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 when you build it later in this quick start guide.
The LoginPage project opens in the IDE. You can view its logical structure in the Projects window and its file structure in the Files window. For example, the Projects window should now look as follows:

In the Configuration Files node, the application includes all the Struts-specific configuration files, of which struts-config.xml is the most important. You will use this configuration file throughout this quick start guide. Also in the Configuration Files node, to handle Struts processing, the Struts servlet (i.e., the controller in the MVC paradigm) is mapped in the web.xml deployment descriptor:
<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>
Above, the Struts servlet (org.apache.struts.action.ActionServlet) is specified as the servlet that will control all requests for the mapping .do. In addition, the web.xml file specifies that the Struts servlet is configured by means of the struts-config.xml file that is found in the WEB-INF folder.
Developing a Struts application is similar to developing any other kind of web application in the IDE. You use components such as JSP pages, servlets, listeners, and filters. However, you complement your web development toolkit by using the facilities provided by Struts via the IDE. For example, you use templates in the IDE to create Struts action classes and Struts actionform bean classes. On top of that, the IDE automatically registers these classes in the struts-config.xml file and lets you extend this file very easily via menu items in the Source Editor's pop-up menu.
Many web applications use JavaServer Pages (JSP) for the view in the MVC paradigm, so Struts provides custom tag libraries which facilitate interaction with HTML forms. These can very easily and smoothly be set up and used in a JSP page in the IDE.
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:form action="login"> <html:submit value="Login" /> </html:form>
Notice that when you type the Struts tags, the IDE helps you by suggesting different ways of completing the code that you're typing, and further helps you by providing Struts Javadoc:

Whenever you finish typing in the Source Editor, you can neaten the code by right-clicking in the Source Editor and choosing Reformat Code:

The loginForm.jsp now looks as follows:
<html:form action="login">
<table border="1">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<html:submit value="Login" />
</html:form>
<bean:message key="login.name" />
In between the <TD> tags, type the following:
<html:text property="name" />
The body of loginForm.jsp is now as follows:
login.name=Name
A Struts "actionform bean" class represents data shared between the view (in this case, a JSP page) and the Struts action class. An actionform bean class is available both for populating the view and for providing input to an action class. An actioform bean class also has a validate method to allow input mapped from the view to be verified.
The actionform bean class opens in the Source Editor. By default, the IDE provides it with a String called name and an int called number. Both fields also have getters and setters defined for them.
<form-beans>
<form-bean name="NewStrutsActionForm" type="com.myapp.struts.NewStrutsActionForm"/>
</form-beans>
Hold down the Ctrl key and move your mouse over the actionform bean class's fully qualified class name:
A hyperlink appears. Click it to navigate to the actionform bean class.
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
return errors;
}
Notice that the field called name is validated by default. If validation fails, which happens when no name is entered in the JSP page, a message that is identified by error.name.required is returned.
error.name.required=Enter a name!
At the top of the file, to customize the formatting of your error message, change the first four keys to the following:
errors.header= errors.prefix=<span style="color: red"> errors.suffix=</span> errors.footer=
<html:errors />
A Struts "action" class is executed in response to a user request and commonly interacts with the model through a business delegate. The responsibility of an action class is to provide navigation and forward control to the appropriate view.
<action-mappings>
<action input="/loginForm.jsp"
name="NewStrutsActionForm"
path="/login"
scope="session"
type="com.myapp.struts.NewStrutsAction"/>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
</action-mappings>
If you want the action class to function per request, instead of per session, put the cursor in the scope attribute and press Ctrl-Space:
Choose Request.
A hyperlink appears. Click it to navigate to the action class.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(SUCCESS);
}
Notice the definition of SUCCESS, at the top of the NewStrutsAction:
private final static String SUCCESS = "success";
The SUCCESS String declaration specifies that this action class forwards to the output view called success.
The Add Forward dialog box opens.
Type success in Forward Name. Browse to loginSuccessful.jsp in Resource File. The dialog box should now look as follows:
Click Add.
Notice that struts-config.xml now shows the following (the new code is in bold):
<action-mappings>
<action input="/loginForm.jsp"
name="NewStrutsActionForm"
path="/login"
scope="session"
type="com.myapp.struts.NewStrutsAction">
<forward name="success"
path="/loginSuccessful.jsp"/>
</action>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
</action-mappings>The IDE uses an Ant build script to build and run your web application. The IDE generated the build script when you created the application, basing it on the options you entered in the New Project wizard and the project's Project Properties dialog box.
Note: Remember that at the start of this quick start guide, you mapped the .do mapping to the Struts controller servlet. Now, when you run the application and the .do mapping is used, the Struts controller servlet knows that it has to handle the request.
The IDE builds the web application and deploys it, using the server you specified when creating the project.
The browser opens and displays the loginForm.jsp page:
Only if field-level validation succeeds, so that the action class's execute method returns the SUCCESS output view, does Struts call the loginsuccesful.jsp page. To pass validation, all that you need to do is add any value to the Name row in the loginForm.jsp page. Then, loginSuccessful.jsp is displayed:
Of course, as pointed out at the start of this quick start guide, this is not a complete login form; it merely shows you what the basis of such a form could look like in Struts. The following section shows you how quickly and easily the login form can be extended with a variety of standard functionality.
Struts simplifies and organizes an application in many more ways than can be listed here. However, here are some simple extensions to your existing login page, using Struts.
<html:cancel />
if (isCancelled(request)){
return mapping.findForward(CANCEL);
}
Press Ctrl-Space within the isCancelled method and then read the Javadoc to understand the method:
Declare the definition of CANCEL at the top of the NewStrutsAction class, right below the definition of SUCCESS:
private final static String CANCEL = "cancel";
Type cancel in Forward Name. Browse to loginCancel.jsp in Resource File. The dialog box should now look as follows:
Click Add.
Notice that struts-config.xml now shows the following (the new code is in bold):
<action-mappings>
<action input="/loginForm.jsp"
name="NewStrutsActionForm"
path="/login"
scope="session"
type="com.myapp.struts.NewStrutsAction">
<forward name="success"
path="/loginSuccessful.jsp"/>
<forward name="cancel"
path="/loginCancel.jsp"/>
</action>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
</action-mappings>
Click it and notice that the new loginCancel.jsp page is opened in the browser.
<html:link action="/logout">Logout</html:link>
Type logout in Action Path. Browse to loginOut.jsp in Resource File. The dialog box should now look as follows:
Click Add.
Notice that struts-config.xml now shows the following (the new code is in bold):
<action-mappings>
<action input="/loginForm.jsp"
name="NewStrutsActionForm"
path="/login"
scope="session"
type="com.myapp.struts.NewStrutsAction">
<forward name="success"
path="/loginSuccessful.jsp"/>
<forward name="cancel"
path="/loginCancel.jsp"/>
</action>
<action forward="/loginOut.jsp"
path="/logout"/>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
</action-mappings>
Click it and notice that the new loginOut.jsp page is opened in the browser.
<html:reset />
Type something in the Name row, click Reset, and notice that Struts empties the table.
When using Struts, you are not limited to the functionality provided by the IDE. You can enhance the IDE by adding features that the IDE does not support. Here are some examples:
The IDE can be enhanced to support Struts in various other ways. For details, see Tutorials for NetBeans Module (Plug-in) and Rich Client Application Development.
For more information about using NetBeans IDE 5.0, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE J2EE development features, join the nbj2ee@netbeans.org mailing list . For more information about upcoming J2EE development features in NetBeans IDE, see j2ee.netbeans.org .