Struts Tools Tutorial
Struts Tools Tutorial
Version: 3.3.0.M5
1. Introduction ................................................................................................................. 1 1.1. Key Features Struts Tools ................................................................................... 1 1.2. Other relevant resources on the topic .................................................................. 2 2. Creating a Simple Struts Application .......................................................................... 3 2.1. Starting Up ......................................................................................................... 3 2.2. Creating the Application Components .................................................................. 3 2.2.1. Creating JSP Page Placeholders .............................................................. 3 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 3. Generating Stub Coding .............................................................................................. 9 4. Coding the Various Files ........................................................................................... 11 4.1. Java Stub Classes ............................................................................................ 11 4.1.1. GetNameForm.java ................................................................................ 11 4.1.2. GreetingAction.java ................................................................................ 13 4.2. JSP Pages ....................................................................................................... 4.2.1. inputname.jsp ......................................................................................... 4.2.2. greeting.jsp ............................................................................................ 4.2.3. index.jsp ................................................................................................ 5. Compiling the Classes and Running the Application ................................................ 6. Struts Validation Examples ....................................................................................... 6.1. Starting Point .................................................................................................... 6.2. Defining the Validation Rule .............................................................................. 6.3. Client-Side Validation ........................................................................................ 6.4. Server Side Validation ....................................................................................... 6.5. Editing the JSP File .......................................................................................... 6.6. Editing the Action .............................................................................................. 6.7. Editing the Form Bean ...................................................................................... 7. Other Relevant Resources on the topic ..................................................................... 14 14 16 17 19 21 21 21 23 25 25 26 26 29
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 to simplify development, but until then you can read about classical Struts usage here. We are going to show you how to create a simple Struts application using the JBoss Tools. The completed application will ask a user to enter a name and click a button. The resulting new page will display the familiar message, "Hello <name>!" 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 Tools. With the help of our tutorial you will design the application, generate stub code for the application, fill in the stub coding, compile the application, and finally run it all from inside the Eclipse.
Chapter 1. Introduction
Chapter 2.
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 Struts Project Next enter "StrutsHello" as the project name Leave everything else as it is, and click Next If you have server runtime already defined, just pass to next point. Otherwise in the Runtime section click the New button and target at needed server runtime environment. Click Finish. 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 hit Finish A "StrutsHello" node should appear in the Package Explorer view. Click the plus sign next to StrutsHello to reveal the child nodes Click the plus sign next to WebContent under StrutsHello Click the plus sign next to WEB-INF under WebContent Then, double-click on the struts-config.xml node to display a diagram of the Struts application configuration file in the editing area At this point, its empty except for the background grid lines.
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %> <html:html> <head> <title></title> </head> <body> <html:form action=""> </html:form> </body> </html:html>
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 You should now have two JSP pages in the diagram.
("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.
). 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
), 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) Expand the struts-config.xml/action-mappings/ /greeting node and then select the greeting forward 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
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 Right-click struts-config.xml form-beans and select Create Form Bean Enter GetNameForm in the name field and sample.GetNameForm for type Click Finish To save your changes to struts-config.xml, select File Save from the menu bar Note the disappearance of the asterisk next to the name, struts-config.xml.
Chapter 3.
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 Leave everything as is in the dialog box and click Generate You should see a screen that says: 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.
10
Chapter 4.
4.1.1. GetNameForm.java
Double-click GetNameForm.java for editing You are looking at a Java stub class that was generated by JBoss Tools. Now we are going to edit the file Add the following attributes at the beginning of the class:
Inside the reset method, delete the TO DO and throw lines and add:
this.name = "";
Inside the validate method, delete the TO DO and throw lines and add:
11
Right-click and select Source Generate Getters and Settersfrom the context menu In the dialog box, check the check box for name, select First method for Insertion point, and click on the OK button The final GetNameForm.java file should look like this:
package sample; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; public class GetNameForm extends org.apache.struts.action.ActionForm { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } public GetNameForm() { } public void reset(ActionMapping actionMapping, HttpServletRequest request) { this.name = ""; } public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); return errors; } }
12
GreetingAction.java
4.1.2. GreetingAction.java
Open GreetingAction.java for editing Inside the execute method, delete the TO DO lines and add the following:
String name = ((GetNameForm)form).getName(); String greeting = "Hello, "+name+"!"; ((GetNameForm)form).setName(greeting); return mapping.findForward(FORWARD_sayHello);
package sample; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class GreetingAction extends org.apache.struts.action.Action { // 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); } }
13
Save the file Close the editors for the two Java files The last thing left to do is to code the JSP files whose editors should still be open from having been created as placeholders.
Click on the inputname.jsp tab in the Editing area to bring its editor forward In the Web Projects view, expand StrutsHello Configuration default strutsconfig.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:
Input name:
Select the Visual pane of the editor 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.
14
inputname.jsp
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:
15
4.2.2. greeting.jsp
Next, we will fill in the result page. Click on the greeting.jsp tab in the Editing area to bring its editor forward Type in the following code:
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
16
index.jsp
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 Position the cursor inside the <p> element Click on write in the JBoss Tools Palette Type in "GetNameForm" for the name attribute and add a property attribute with "name" as its value The editor should now look like this:
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.
17
In the Web Projects view, right-click on StrutsHello WEB-ROOT(WebContent) node and select New File JSP Type index for Name and click on the Finish button On the JBoss Tools Palette, select the Struts Common folder of macros by clicking on it in the palette Click on the logic taglib icon Press the Enter key in the editor to go to the next line Back on the palette, select the Struts Logic folder of macros 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 The finished code for the page is shown below:
To save all the edits to files, select File Save All from the menu bar
18
Chapter 5.
19
20
Chapter 6.
Right-click on a "plug-ins" node under the StrutsHello Configuration default strutsconfig.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 New 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 Select File Save All from the menu bar
21
Select validation.xml under the StrutsHello Validation node and double-click it to open it with the JBoss Tools XML Editor Here you must create a Formset. 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 strutsconfig.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:
22
Client-Side Validation
23
To see how this works in our application, you'll just need to make a couple of modifications to one of the JSP files.
Double-click inputname.jsp under StrutsHello WEB-ROOT(WebContent) pages to open it for editing 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 Modify the <html:form> tag by inserting this attribute:
onsubmit="return validateGetNameForm(this)"
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %> <html:html > <head> <html:javascript formName="GetNameForm"/> <title></title> </head> <body> <html:form action="/greeting.do" onsubmit="return validateGetNameForm(this)"> Input name:<html:text property="name"/><html:submit value="Say Hello!"/> </html:form> </body> </html:html>
24
Select File Save from the menu bar 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 A JavaScript error message should be displayed in an alert box.
<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %> <html:html > <head> <html:javascript formName="GetNameForm"/> <title></title> </head> <body> <html:form action="/greeting.do" >Input name:<html:text property="name"/> <html:submit value="Say Hello!"/>
25
package sample; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; public class GetNameForm extends org.apache.struts.validator.ValidatorForm { private String name = "";
26
/** * @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 () { } public void reset(ActionMapping actionMapping, HttpServletRequest request) { this.name = ""; } // public ActionErrors validate(ActionMapping actionMapping, // HttpServletRequest request) //{ // ActionErrors errors = new ActionErrors(); // return errors; // } }
Select File Save All from the menu bar Reload the application into JBoss AS by clicking on the "Change Time Stamp" icon (a finger pointing with a little star) in the toolbar Run the application In the browser window, click on the "Say Hello!" button without having entered any name in the form The error message should appear in a refreshed version of the form.
27
28
Chapter 7.
29
30