148 Self-Service Applications using IBM WebSphere V5.0 and IBM MQSeries Integrator
7.1 MVC development using the Struts framework
This section describes the development cycles for the Model-View-Controller
(MVC) design pattern using the Apache Struts framework.
7.1.1 Creating a Web diagram
WebSphere Studio Application Developer V5 has a built-in plug-in that supports
the development of Web applications using the Struts framework. It has two
major features:
򐂰 Visual Web diagram editor for Struts elements.
You can develop interaction diagrams and Struts applications based on these
diagrams. The diagrams look like the one in Figure 7-1.
򐂰 Special editor view for the Struts configuration .xml file.
This special editor view simplifies development. Instead of editing the XML
source, the developer can create and configure the Struts elements
interactively in the editor view.
Figure 7-1 Struts Web diagram - OrderPickup process
7.1.2 Coding Struts elements
The following steps are an outline of how a sample Struts flow is created.
1. Create a Web application. In WebSphere Studio, this is done by creating a
Web project (
File -> New -> Web Project). The wizard will allow you to select
Web project features. Be sure to include “Add Struts Support”. This will create
a Web application with the necessary framework for Struts, including the
Struts tag libraries, Struts runtime libraries, Struts configuration file and
entries to support Struts in web.xml, and the application resources properties
Chapter 7. Application development 149
2. Create an input page with an HTML form, for example, input.jsp. Make it a
JSP so it can provide dynamic information, for example, using JSP taglibs, or
provide error messages when validation fails.
The WebSphere Studio wizard that creates JSPs (
File -> New -> JSP File)
gives you the option of selecting a model on which to base the JSP. Be sure
to select
Struts JSP for this JSP and the next two JSPs. This will include the
taglib directives to the Struts tag libraries.
Example 7-1 input.jsp
<%@ page language="java" %>
<!-- include all the JSP Taglibs needed -->
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-form.tld" prefix="form" %>
<!-- print out the errors, if any -->
<!-- create a form -->
<html:form action="/sampleaction.do" focus="userid">
<!-- get the value for prompt.name from the applicationresources.properties -->
<bean:message key="prompt.name"/>:
<!-- create a text input field -->
<html:text property="name" size="20">
<!-- create a submit button -->
3. Create a Results page with dynamic content, for example, results.jsp.
Example 7-2 results.jsp
This is the result page.
<!-- Check out the Struts documentation on how to use the form-bean here to
dynamically retrieve information from the bean. Also check the Struts taglibs
4. Create an error page to indicate errors, for example, error.jsp.
150 Self-Service Applications using IBM WebSphere V5.0 and IBM MQSeries Integrator
Example 7-3 error.jsp
This is the error page.
<!-- Check out the Struts documentation for Struts taglibs -->
5. Create a form bean to store the form data for the action, for example,
com.ibm.itso.msgtrx.SampleForm. The FormBean provides access to the
fields within a form submitted on the input page; it also provides validation if
WebSphere Studio provides wizards to create Struts elements, including form
beans (
File -> New -> Other -> Web -> Struts -> ActionForm Class). The
wizard to create ActionForm classes allows you to select fields from JSPs for
which you want field, setter and getter methods. For example, you would
choose the input fields on input.jsp. The wizard also automatically registers
the form bean in the Struts-config.xml file.
Example 7-4 com.ibm.itso.msgtrx.forms.SampleForm
package com.ibm.itso.msgtrx.forms;
// You will need the following imports at least
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class RegistrationForm extends ActionForm {
private String name=null;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request) {
ActionErrors errors = new ActionErrors();
// If the name is empty then add a new error to the error list
// Get the text for the error from the resource file
if ((name == null) || (name.length() < 1)) errors.add("username", new
return errors;

Get Self-Service Applications using IBM WebSphere V5.0 and IBM MQSeries Integrator now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.