Creating a simple Struts application
This documentation describes in detail how to create a simple Struts application that has two Web pages, an action mapping, and a form bean.
Before you perform this task, make sure that you are in the Web perspective. If you are not in the Web perspective, click Window > Open Perspective > Other > Web.
Suppose you want to create an application that has a Web page that accepts a year, month, and date as input and some code that manipulates the data to compute the day of the week. If the input is valid, the computed value is displayed on an output Web page; if the input is not valid, an error message is displayed on the input page. Figure 1 shows a Web diagram for such an application.
Figure 1. Web diagram of simple application that is to be created
In this figure, index.jsp represents the input JSP file, output.jsp represents the output JSP file, dateData represents the form bean that stores input and output data, and /computeDay represents a computeDay action mapping of the action code that will be run when an input is submitted and its output is directed to output.jsp. The action code computes the day of the week for a specified date. Table 1 shows the input and output fields.
Table 1. Input and output fields Field name File name Description year index.jsp Four-digit year month index.jsp Two-digit month day index.jsp Two-digit day dayOfWeek output.jsp String that represents day of week In this application, the view of the model-view-controller design pattern consists of the two JSP files. The controller comprises the computeDay action mapping and the dateData form bean. Because no database or data source other than user input is involved, the model is absent.
To create the Struts application described above, complete the following steps:
- Create a dynamic Web project enabled for Struts.
A Struts-enabled Web project is created in your Dynamic Web Projects folder, and a Web diagram named diagram.gph is created in the DayOfWeek project folder.
- From the main menu, click File > New > Dynamic Web Project.
- On the Dynamic Web Project page, type the name of the project, for example, DayOfWeek, and click Next.
- On the Features page, make sure that the Struts box is checked, and click Finish.
- In the Project Explorer, click Dynamic Web Projects > DayOfWeek, click + to expand the entry, and double-click Web Diagram. The Web diagram editor opens diagram.gph.
- In the Web diagram editor, right-click and select Snap > Show grid to display an alignment grid. (See Editing Web application diagrams - overview.)
- Right-click and select Snap > Snap to grid on.
- Edit the Web diagram to look like Figure 1, except that at this point the nodes will appear grayish, indicating that they are not yet realized).
- Create Web page nodes named index.jsp and output.jsp, an action mapping node named computeDay, and a form bean node named dateData. For details on how to do this, see Drawing a node in a Web diagram. This adds node information to the diagram.gph file but does not realize the nodes.
- Create the following connections (see Drawing a connection in a Web diagram):
- From index.jsp to /computeDay
- From /computeDay to output.jsp (with a connection type of Local Forward and with the label set to success)
- From /computeDay to dateData
- From /computeDay to index.jsp (with a connection type of Global Forward and with the label set to failure)
This adds more information to the diagram.gph file. Your diagram should now look like Figure 2.
Figure 2. Web diagram that contains unrealized nodes
- Click File > Save to save your file.
- Create a Struts form bean.
- In the Web diagram editor, double-click the dateData form-bean icon.
- On the New Form-Bean page, click Next.
- On the "Choose new fields for your ActionForm class" page, check the DayOfWeek box and click Next.
- On the "Create new fields for your ActionForm class" page, click Add and specify each of the fields in the Table 2. Then click Finish.
A file named DateData.java is created in the DayOfWeek\JavaSource directory (DayOfWeek > JavaSource > (default package) in Project Explorer), ApplicationResources.properties is created in DayOfWeek\JavaSource\dayofweek\resources (DayOfWeek > JavaSource > dayofweek.resources in Project Explorer), and the Java editor opens DateData.java.
Table 2. Fields to add to dateData form bean Name Type year int month int day int dayOfWeek String Tip: Creating your form beans before the JSP pages that use them eliminates the need to retype the field names when you create the JSP pages.- Edit the form bean source file and the Java resources file specifically for the simple application.
- Edit the DateData.java file, inserting the following code near the bottom of the file after ActionErrors errors = new ActionErrors();:
if (year < 1582) { errors.add("year",new org.apache.struts.action.ActionError("pre_gregorian")); }- Save and close the file.
- In the Project Navigator, click JavaSource > com.ibm.dayofweek.resources, and then double-click ApplicationResources.properties. Edit the file, uncommenting the errors.header and errors.footer lines and adding the following line anywhere in the file:
pre_gregorian=<li>Date is before 1582, the year the Gregorian calendar began</li>- Save and close the file.
- From the Web diagram, create a JSP file named index.jsp.
- Double-click the index.jsp icon.
- On the JSP File page, make sure that the Model field says Struts JSP and that the Configure advanced options box is checked. Then click Next.
- On the following pages, click Next:
- Tag Libraries
- JSP File
- JSP File
- On the Form Field Selection page in the Form bean entry drop-down menu, click dateData. Then check the year, month, and day boxes, make sure that the Generate fields in a form box is checked, and click Finish. An index.jsp file is created in the Web content folder.
- If the sequence of labels is not year - month - day, click Source, edit the JSP file to create that sequence, and save the file.
- Click File > Close to close the file.
- Create a JSP file named output.jsp.
- Double-click the output.jsp icon.
- On the JSP File page, make sure that the Model field says Struts JSP and that the Configure advanced options box is checked. Then click Next.
- On the following pages, click Next:
- Tag Libraries
- JSP File
- JSP File
- On the Form Field Selection page in the Form bean entry drop-down menu, click dateData. Then check the dayOfWeek box, uncheck the Generate fields in a form box, and click Finish. An output.jsp file is created in the Web content folder.
- Save and close the file.
- Create a Struts action and an action mapping.
- In the Web diagram editor, double-click the /computeDay icon.
- On the New Action Mapping page, click Finish. An action mapping is added to the Struts configuration file (struts-config.xml), and a file named ComputeDayAction.java is created in DayOfWeek\JavaSource\com\ibm\dayofweek\actions, and the file opens in the Java editor.
- Edit the file to match the code in Source code for action in simple Struts application.
- Save the file and close it. The file ComputeDayAction.java is compiled to produce a ComputeDayAction.class file as shown WEB-INF > classes > dayofweek > actions in the Web content folder of the Project Navigator. Figure 3 shows what your Web diagram should look like. Note that the nodes are now in color, indicating that they are realized.
Figure 3. Web diagram that contains realized nodes
- Save and close the diagram.gph file.
- Test your Struts application.
- In the Project Navigator, right-click index.jsp and select Run on Server. A "Server selection" window appears.
- In the Server type field, click WebSphere version 5.1 > Test Environment > Finish. This brings up the index.jsp file for input.
- Fill out the year, month, and day fields, for example, 2001, 9, and 11.
- Click Submit. Figure 4 shows the output.
Figure 4. Output from simple Struts application
Related concepts
Struts and model-view-controller design pattern
Struts-based Web applications
The Web perspective
Related tasks
Creating well-architected Web applications using Struts
Creating a dynamic Web project
Creating a Web diagram
Editing Web application diagrams - overview
Creating a Struts form bean
Creating JavaServer Pages (JSP) files
Creating a Struts action
Testing your Struts application
Related reference
Feedback
Struts preferences for Web diagram editor
Source code for action in simple Struts application