Struts Tools Tutorial
Struts Tools Tutorial
Version: 1.0.0.GA
1. Introduction ................................................................................................................. 1
2. Getting Started Guide for Creating a Struts Application .............................................. 3
2.1. Starting Up ......................................................................................................... 3
2.2. Creating the Application Components .................................................................. 4
2.2.1. Creating JSP Page Placeholders .............................................................. 4
2.2.2. Creating an Action Mappings .................................................................... 5
2.2.3. Creating a Link ........................................................................................ 5
2.2.4. Creating a Forward .................................................................................. 6
2.2.5. Creating a Global Forward ........................................................................ 6
2.2.6. Creating a Form Bean .............................................................................. 7
2.3. Generating Stub Coding ...................................................................................... 7
2.4. Coding the Various Files ..................................................................................... 8
2.4.1. Java Stub Classes ................................................................................... 8
2.4.2. JSP Pages ............................................................................................ 11
2.5. Compiling the Classes ...................................................................................... 16
2.6. Running the Application .................................................................................... 16
2.7. Other relevant resources on the topic ................................................................ 16
3. Getting Started Struts Validation Examples ............................................................... 19
3.1. Starting Point .................................................................................................... 19
3.2. Defining the Validation Rule .............................................................................. 19
3.3. Client-Side Validation ........................................................................................ 21
3.4. Server Side Validation ....................................................................................... 23
3.5. Editing the JSP File .......................................................................................... 23
3.6. Editing the Action .............................................................................................. 24
3.7. Editing the Form Bean ...................................................................................... 24
3.8. Other Resources ............................................................................................... 26
iii
iv
Chapter 1.
Introduction
The following chapters describe how to deal with classic/old style of Struts development. We
recommend users to use JBoss Seam [../../../seam/en/html/create_new_seam.html] to simplify
development, but until then you can read about classical Struts usage here.
1
2
Chapter 2.
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.)
2.1. Starting Up
We are first going to create a new project for the application.
• Go to the menu bar and select File > New > Project....
• Select JBoss Tools Web > Struts > Struts Project in the New Project dialog box
• Click Next
• Make sure that struts-bean.tld, struts-html.tld, and struts-logic.tld are checked in the list of
included tag libraries and then click Finish
• Click the plus sign next to StrutsHello to reveal the child nodes
• Then, double-click on the struts-config.xml node to display a diagram of the Struts application
configuration file in the editing area
3
Chapter 2. Getting Started Gu...
At this point, its empty except for the background grid lines.
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.
• Bring the Web Projects view to the front of the Package Explorer view by selecting the Web
Projects tab next to that tab.
• Right-click the StrutsHello > WEB-ROOT (WebContent) folder in the Web Projects view and
select New > Folder...
• Right-click the pages folder and select New > Fila > JSP...
• For Name type in inputname (the JSP extension will be automatically added to the file), for
Template select StrutsForm and then click on the Finish button
• Right-click the pages folder again and select New > File > JSP...
• For Name type in greeting, for Template leave as Blank, and then click on the Finish button
Lets now place the two pages just created on the diagram.
• Click on the struts-config.xml tab in the Editing area to bring the diagram to the front
• Click on the inputname.jsp page in the Web Projects view, drag it onto the diagram, and drop it
• Click on the greeting.jsp page in the Web Projects view, drag it onto the diagram, and drop it to
the right of the /pages/inputname.jsp icon with some extra space
4
Creating an Action Mappings
Using a context menu on the diagram, we are next going to create an Action mapping.
• Right-click between the two icons and select Add > Action
Table 2.1.
path /greeting
name GetNameForm
scope request
type sample.GreetingAction
validate <leave blank>
("GetNameForm" is the name for a form bean that we will create later.)
• Click Finish
The /greeting action should appear in four places, in the diagram, under the action-mappings
node, under the struts-config.xml node in Tree view, in Web Projects view and in the Outline view.
Also, note the asterisk to the right of the name, struts-config.xml, in the Outline view showing that
the file has been changed, but not saved to disk.
Let's now create a link from the inputname.jsp page to the action.
• On the left-hand side of the diagram in the column of icons, click on this icon:
• In the connect-the-components mode you are in now, click on the /pages/inputname.jsp icon in
the diagram and then click on the /greeting action
5
Chapter 2. Getting Started Gu...
• On the left-hand side of the diagram in the column of icons, click on this icon, again:
• Click on the /greeting action icon in the diagram and then click on the pages/greeting.jsp icon
• That's it. A link will be drawn from the actions new greeting forward to the greeting.jsp JSP
page. Note that the forwards name will be set based on the name of the target JSP file name.
If you don't like it, you can easily change it
• Select the Tree tab at the bottom of the editor window (between Diagram and Source)
• In the Properties Editor to the right, change the text to "sayHello" in the Name field
• Select the Diagram tab at the bottom of the editor window and see how the diagram is also
updated to reflect the change
One last component that we need to create in the diagram is a global forward.
• Somewhere in the top-left corner of diagram, right-click and select Add > Global Forward...
• Expand the StrutsHello > WEB-ROOT (WebContent) > pages node and then select the
inputname.jsp page
• Click Ok.
6
Creating a Form Bean
A forward object now appears on the diagram and also in the global-forwards folder in the Outline
view.
• Tidy up the diagram, by clicking and dragging around each icon, so that the diagram looks
something like this:
• Switch to the Tree viewer in the editor for the struts-config.xml file, by selecting the Tree tab
at the bottom of the editor window
• Click Finish
• To save your changes to struts-config.xml, select File > Save from the menu bar
7
Chapter 2. Getting Started Gu...
• Switch back to the diagram, by selecting the Diagram tab at the bottom of the editor window
• Right-click a blank space in the diagram and select Generate Java Code
Generated classes: 2
Actions: 1
Form beans: 1
• Click Finish
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.
• To finish the two Java classes, switch to the Package Explorer view and expand the JavaSource
> sample folder
2.4.1.1. GetNameForm.java
• You are looking at a Java stub class that was generated by JBoss Developer Studio. Now we
are going to edit the file
• Inside the reset method, delete the TO DO and throw lines and add:
8
Java Stub Classes
this.name = "";
this.greetName = "";
• Inside the validate method, delete the TO DO and throw lines and add:
• Right-click and select Source > Generate Getters and Setters...from the context menu
• In the dialog box, check the check boxes for name and greetName, select First method for
Insertion point, and click on the OK button
package sample;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
9
Chapter 2. Getting Started Gu...
{
this.greetName = greetName;
}
public GetNameForm()
{
}
2.4.1.2. GreetingAction.java
• Inside the execute method, delete the TO DO lines and add the following:
package sample;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
10
JSP Pages
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
// 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.
2.4.2.1. inputname.jsp
In this page, the user will enter any name and click the submit button. Then, the greeting action
will be called through the form.
Input name:
• Click on the inputname.jsp tab in the Editing area to bring its editor forward
11
Chapter 2. Getting Started Gu...
• In the Web Projects view, expand StrutsHello > Configuration > default > struts-config.xml >
action-mappings and select /greeting
• Drag it and drop it between the quotes for the "action" attribute to the <html:form> element in
the Source pane of the editor
• Then type this text on a new line just below this line:
• Then, in the JBoss Tools Palette, expand the Struts Form library, select text, and drag it onto
the box
Note:
By default there are only four groups on the JBoss Tools Palette. If you wish to
make some group visible click the Show/Hide button on the top of palette and in
the prompted dialog check the group (or groups) you want to be shown.
12
JSP Pages
• In the Insert Tag dialog box, type in name for property and select Finish
• In the StrutsForm library in the JBoss Tools Palette, select submit, and drag it to right after the
text box in the Visual pane of the editor
• Right-click the submit button and select <html:submit> Attributes from the context menu
• In the Attributes dialog box, select the value field and type in "Say Hello!" for its value
After tidying the page source, the Editor window for the file should look something like this:
2.4.2.2. greeting.jsp
• Click on the greeting.jsp tab in the Editing area to bring its editor forward
13
Chapter 2. Getting Started Gu...
<html>
<head>
<title>Greeting</title>
</head>
<body>
<p>
</p>
</body>
</html>
To complete editing of this file, we will use macros from the JBoss Tools Palette. This palette is
a view that should be available to the right of the editing area.
• Click on the Struts Common folder in the JBoss Tools Palette to open it
• Position the cursor at the beginning of the greeting.jsp file in the Source pane and then click
on bean taglib in the JBoss Tools Palette
This will insert the following line at the top of the file:
• Click on the Struts Bean folder in the JBoss Tools Palette to open it
• Type in "GetNameForm" for the "name" attribute and add a "property" attribute with "greetName"
as its value
14
JSP Pages
2.4.2.3. index.jsp
Finally, we will need to create and edit an index.jsp page. This page will use a Struts forward to
simply redirect us to the getName global forward.
• In the Web Projects view, right-click on StrutsHello > WEB-ROOT(WebContent) node and select
New > File > JSP..
• On the JBoss Tools Palette, select the Struts Common folder of macros by clicking on it in the
palette
15
Chapter 2. Getting Started Gu...
• Click on redirect
• Delete the ending tag, put a forward slash in front of the closing angle bracket, and type
"forward=getName" in front of the slash
• To save all the edits to files, select File>Save All from the menu bar
• 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.
16
Other relevant resources on the topic
17
18
Chapter 3.
JBoss Developer Studio makes using the Validation Framework in Struts even easier with the
help of a specialized editor for the XML files that controls validation in a project. In this document,
we'll show you how this all works by creating some simple client-side validation and server-side
validation examples.
• Right-click on a "plug-ins" node under the StrutsHello > Configuration > default > struts-
config.xml node in the Web Projects view and select Create Special Plugin > Validators from
the context menu
• Further down in the Web Projects view, right-click on the StrutsHello > ResourceBundles node
and select New > Properties File...from the context menu
• In the dialog box, click on the Browse...button next to the Folder field, expand the JavaSource
folder in this next dialog box, select the sample subfolder, and click on the OK button
• Back in the first dialog box, type in "applResources" for the Name field and click on the Finish
button
• Right-click on a newly created file and select Add > Default Error Messages from the context
menu
• Drag up the sample.applResources icon until you can drop it on the resources folder under
struts-config.xml
19
Chapter 3. Getting Started St...
• Select validation.xml under the StrutsHello > Validation node and double-click it to open it with
the JBoss Tools XML Editor
• In the validation.xml file editor click the button Create Formset on the panel Formsets
• In the dialog Add Formset fill the fields Language and Country or just leave them empty to create
a default formset. Click OK
• Expand the "form-beans" node under the StrutsHello > Configuration > default > struts-
config.xml node. Then, drag the form bean "GetNameForm" and drop it onto a formset in the
XML Editor
• In the Validation Editor, expand the formset node, right-click GetNameForm, and select Create
Field... from the context menu
• Enter a name for Property in the dialog box. A new property will be created:
20
Client-Side Validation
• In the Properties view for the name field to the right of the "tree" for the validation.xml file, click
on the Change...button next to the Depends entry field
• In the displayed double list, select required from the left list and then click Add
• Click Ok
• Right-click name and select Add Arg... from the context menu
• In the Add Arg dialog box, click on the Change...button next to the Key field
• In the Key dialog box that appears now, click on the Add button
• Enter "name.required" in the Name field, and enter a person's name in the Value field
21
Chapter 3. Getting Started St...
To see how this works in our application, you'll just need to make a couple of modifications to
one of the JSP files.
• Find the tag near the top and hit Return to make a new line under it
• In the JBoss Tools Palette view to the right, open the Struts HTML folder and click on the
javascript tag
• Back in the editor, just in front of the closing slash for this inserted tag, hit Ctrl+Space and select
"formName" from the prompting menu
• Over in the Web Projects view, select GetNameForm under the StrutsHello > Configuration >
default > struts-config.xml > form-beans node, drag it, and drop it between the quotes in the
editor
onsubmit="return validateGetNameForm(this)"
22
Server Side Validation
</table>
</html:form>
</body>
</html:html>
• Start JBoss Application Server by clicking on its icon (a right-pointing arrow) in the toolbar
• Click the Run icon or right click your project folder and select Run As > Run on Server
• In the browser window, click on the "Say Hello!" button without having entered any name in
the form
• Delete the "onsubmit" attribute in the <html:form> element that you put in for client-side
validation
23
Chapter 3. Getting Started St...
<body>
<html:form action="/greeting.do">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><b>Input name:</b></td>
</tr>
<tr>
<td>
<html:text property="name" />
<html:submit value=" Say Hello! " />
</td>
</tr>
</table>
</html:form>
<html:errors />
</body>
</html:html>
• In the Edit Properties window, insert the cursor into the value column for the input property and
click on the ... button
• In the dialog box, make sure the Pages tab is selected, select StrutsHello > WEB-
ROOT(WebContent) > pages > inputname.jsp, click the Ok button, and then click on the Close
button
package sample;
import javax.servlet.http.HttpServletRequest;
24
Editing the Form Bean
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class GetNameForm extends
org.apache.struts.validator.ValidatorForm
{
/**
* @return Returns the name.
*/
public String getName()
{
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name)
{
this.name = name;
}
public GetNameForm ()
{
}
25
Chapter 3. Getting Started St...
• Reload the application into JBoss AS by clicking on the "Change Time Stamp" icon (a finger
pointing with a little star) in the toolbar
• In the browser window, click on the "Say Hello!" button without having entered any name in
the form
26