WASKB003 - Building Struts Based Applications in WSAD V5.1.2 Tutorial

Popular Courses

Browse Our Free Resources

  • whitepapers
  • whitepapers
  • webinars
  • blogs

Our Locations

Training Centres

Vancouver, BC
Calgary, AB
Edmonton, AB
Toronto, ON
Ottawa, ON
Montreal, QC
Hunt Valley

locations map


550 6th Av SW
Suite 475
Calgary, AB
T2P 0S2


821A Bloor Street West
Toronto, ON
M6G 1M1


409 Granville St
Suite 902
Vancouver, BC
V6C 1T2

U.S. Office

436 York Road
Suite 1
Jenkintown, PA

Other Locations

Dallas, TX
Miami, FL

In this tutorial, you will develop a simple online registration application. The tutorial was tested on WSAD v5.1.2.

Create the Projects

Start WSAD. Switch to the J2EE perspective (from menu select Windows->Open Perspective->Other then select J2EE). Activate the J2EE Hierarchy view.

Right click on Enterprise Applications and select New->Enterprise Application Project. Select the J2EE 1.3 option and click on Next. Set the project name to StrutsApp and click on Finish.

Right click on Web Modules and select New->Dynamic Web Project. Set the project name to StrutsWeb. Select the Configure advanced options check box. Click on Next.

In the EAR project drop down, select StrutsApp. Click on Next.

Under the Web Project features, select Add Struts support.

Click on Next.

Uncheck Create a Web diagram for the project. Check Override default settings. Enter the following values.

Default Java package prefix: com.webagesolutions.struts
Resource bundle->Java package: com.webagesolutions.struts.resources

Click on Finish.

Click on Yes to switch to the Web perspective.

Note the following about a Web project with Struts support enabled:

  1. The action servlet is automatically registered in web.xml.
  2. The URL mapping of the action servlet is *.do.
  3. The Struts custom tag library files are added to the WEB-INF folder.
  4. The Struts configuration file struts-config.xml is added to the WEB-INF folder.

Build the View

Make sure that you are in the Web perspective. In the Project Navigator view, right click on StrutsWeb and select New->JSP File. Set the file name to register.jsp. Notice, that the model for the new JSP is automatically set to Struts JSP. Click on Finish.

System will open register.jsp in the editor. At the bottom of the editor, click on the Source tab. Notice that the struts-html and struts-bean tag libraries are already loaded.

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

Within the <BODY> and </BODY> tag, create a form as follows.

<html:form action="/">
    <html:text property="name"></html:text>
    <html:text property="address"></html:text>
    <html:text property="city"></html:text>
    <html:text property="state"></html:text>
    <html:text property="country"></html:text>
    <html:text property="zip"></html:text>
    <html:submit>Register Online</html:submit>

Save register.jsp and ignore the warning about the missing action.

Struts Refresher: When the html:form tag is executed, the tag's code looks up the Struts action by the name specified in the action attribute of the tag. The code then locates the form bean name associated with the action and creates a new instance of the bean if one can not be located in the request or session scope. Form element tags such as html:text uses this bean instance to display the bean's properties.

Create another JSP file called thankyou.jsp. Enter a basic thank you message in that JSP file.

Build the Model

First, we will create the form bean class that will hold user input data. Right click on the StrutsWeb/Java Resources folder and select New->Other and then Web->Struts->ActionForm Class. Click on Next.

Set the ActionForm class name to RegistrationFormBean. By default, the package name will be com.webagesolutions.struts.forms as set at the project level. Click on Next.

The tool can automatically add attributes to the form bean based on a Struts form created in a JSP file. Follow the screenshot below to select all the form elements in the register.jsp page.

Click on Finish. This will generate the file and register the form bean in struts-config.xml with the name registrationFormBean.

Open the file and change the reset method as follows.

public void reset(ActionMapping mapping, HttpServletRequest request) {
    name = "";
    address = "";
    city = "";
    state = "";
    country = "USA";
    zip = "";

The reset method is automatically called by the framework just before it transfers the input data from a HTTP request to the form bean properties.

We will keep this tutorial simple and not develop a business logic layer. If you wish, you can develop a class called com.webagesolutions.struts.UserManager and add a method called registerUser() as follows.

public void registerUser(RegistrationFormBean u) throws Exception {

Build the Controller

We will develop a Struts action class called Register that will act as the controller. The controller will invoke the model logic available from the UserManager class. In case of success it will redirect the browser to thankyou.jsp. In case of failure, it will forward to register.jsp and display an error message.

Right click on the Java Source folder and select New->Other and then Web->Struts->Action Class. Set the Action class name to Register. The package name should be com.webagesolutions.struts.actions by default. Click Next.

Struts Refresher: Since the mapped name of the action is register and the action servlet's URL map is *.do, the actual URL for the action is This is what we had specified in the action attribute of the form in register.jsp.

Set the Form Bean Name to registerFormBean and set the scope to request.

Click on Finish. System will create the file and add the action in the struts-config.xml.

The new action class wizard allows basic configuration of the action class. In our case, we will need to open the struts-config.xml to set the input and forwards for the action.

Double click on WEB-INF/struts-config.xml. Select the /register action. Set the Input: field to register.jsp.

Click on the Local Fowards tab on the top. Select the /register action from the Action Mappings list. Under the Local Forwards list, click on Add. Set the forward name to success and hit Enter. Set the Path field to /thankyou.jsp. Check the Redirect checkbox.

Similarly, add another forward as follows:

Name: failure
Path: /register.jsp
Redirect: Not checked.

Save and close struts-config.xml.

Struts Refresher: If the form bean validation finds errors, the framework forwards to the JSP page specified in the input attribute of the action. If, on the other hand, the action class encounters errors, it can forward to a JSP page appropriate for the error. In either case, we need to be able to show error messages to the user from the forwarded JSP page. Error handling from an action is more pwoerful in the sense, we can forward to different error pages based on the error condition.

Open the action class and set the perform method as follows.

public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    ActionErrors errors = new ActionErrors();
    ActionForward forward = new ActionForward();
    RegistrationFormBean reg = (RegistrationFormBean) form;

    if (!errors.empty()) {
        saveErrors(request, errors);
        forward = mapping.findForward("failure");
    } else {
        forward = mapping.findForward("success");
    return (forward);


Right click on register.jsp and select run on Server. Select a WebSphere V5 server. Try submitting the form. We do not have error handling built in yet. In all cases, you should see the thank you page.

Error Handling

We will perform basic validation from the form bean class and more advanced validation from the action class.

In the form bean class ( add a new method called checkEmpty as follows.

private void checkEmpty(String param, ActionErrors errs, String msg) {
    if (param == null || param.trim().length() == 0) {
        errs.add(msg, new ActionError(msg));

Change the validate method as follows.

public ActionErrors validate(
    ActionMapping mapping,
    HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();

    checkEmpty(getName(), errors, "name_missing");
    checkEmpty(getAddress(), errors, "address_missing");
    checkEmpty(getCity(), errors, "city_missing");

    return errors;

Struts Refresher: Each ActionErrors object contains zero or more ActionError object. Each ActionError object contains a message key String (For example, name_missing and address_missing). Actual messages are stored in the application's resource bundle.

Open and enter the error messages as follows.

# Optional header and footer for <errors/> tag.
name_missing=<li>Name missing
city_missing=<li>City missing
address_missing=<li>Address missing
invalid_address=<li>Invalid address

Next, we will show how to perform error handling from an action class. Usually, the model layer performs complex error checking (such as a product is out of inventory). It needs to communicate the error condition to the controller through exceptions or return codes. On error, the controller needs to forward to an error handler JSP.

Open the action class ( and add validation code as follows.

RegistrationFormBean reg = (RegistrationFormBean) form;

if (reg.getAddress().length() < 5) {
    errors.add("invalid_address", new ActionError("invalid_address"));

Save changes. Open register.jsp and before the html:form tag display the error by adding this line.



Restart the StrutsApp enterprise application. Test for the error conditions.


Email Address: *

Very useful Somewhat useful Not bad Needs many corrections


( * ) = mandatory field