WebSphere Studio provides numerous tools for developing Struts Web applications. For example, a visual assembly tool, the Web diagram editor, helps developers to design a Struts-based Web application by creating Struts artifacts and connecting these artifacts visually. In this section you will develop a Struts Web application. This application adds a new book to a fictitious bookshop. Please note this is only a sample application which does not execute any real business logic. To develop a Struts application proceed as follows : -
Create a Struts Web application: -
Switch to the Web perspective. -
Select File -> New -> Web -> Web Project . -
Enter the project name of WebBookStruts . -
Select the Web Project features Add Struts support . This adds the necessary support for Struts into your Web project. -
Click Next > . -
In the J2EE Settings Page, click Next > again. -
In the Struts settings page, select Override default settings and select the Struts version 1.1 . Figure 14-2. For Struts portlets you need Struts version 1.1 -
Click Finish . -
Next, graphically lay out the Web application using the Web Diagram Editor: -
Select File -> New -> Other -> Web -> Struts -> Web Diagram to create a new Web diagram. -
Select the WebBookStruts Web project and enter the name BookWeb.gph . Click Finish . The Web diagram editor opens. -
Right-click anywhere in the editor and select New -> Web Page Node . Name the Web page node addABook.jsp . Figure 14-3. In the Web diagram editor, create a new Web page -
Add two Web page nodes. Change their names to addABookResponse.jsp and error.jsp . -
Select New -> Action Mapping Node to add a new action mapping node. Change its name to addABook . -
Select New -> Form Bean Node . Name it newBook and set the scope to request . -
Select addABook.jsp and choose Connection from the context menu. Draw a connection to addABook . -
Connect addABook to addABookResponse.jsp . Label the connection success . -
Connect addABook to error.jsp . Label the connection failure . -
Connect addABookResponse.jsp to addABook.jsp . -
Connect error.jsp to addABook.jsp . -
Connect newBook to addABook . -
Connect addABook.jsp to newBook . The final Web diagram should look as in Figure 14-4 on page 453. Figure 14-4. The final Web diagram of the book application. -
Press Ctrl-S to save the Web diagram. -
From the Web diagram, we create first the newBook action form class as follows: -
Double-click newBook to launch the New Action Form Class file wizard. -
On the first page, click Next > . -
On the Choose new accessors page, click Next > again. -
On the Create new accessors page, add four new accessors of type String: title, isbn, author and category . -
Add one accessor price of type java.mathBigDecimal. -
Click Next > , then Finish . Close the Java editor. -
From the Web diagram, we can now create the JSP and complete its content as follows: -
Double-click addABook.jsp to launch the New JSP file wizard. -
Select XHTML as the Markup Language. Click Next > and on the second page Next > again. -
On the Page Directive Information Page, disable Generate a Page Directive Information . -
Click Next > three times. -
On the Form Field Selection page, select newBookForm as the form bean. -
By pressing the shift key, select all four fields of the bean. Figure 14-5. JSP File wizard to create an html form -
Click Next > and Finish .Close the Web designer. -
From the Web diagram, double-click the error.jsp . Select XHTML as the Markup language and disable the page directive generation. Click Finish . -
In the Web page designer, enter an error message and close the Web designer. -
From the Web diagram, double-click the addABookResponse.jsp . Select XHTML as the Markup language and disable the page directive generation. Click Finish . -
In the Web page designer, enter a success message and close the Web designer. -
Next, create the Struts Action class. The action class receives control from the input page using the input data stored in the form bean. -
Double-click the addABook action mapping to launch the New Action Mapping dialog. -
In the Form Bean Name field, select the newBookForm bean (see Figure 14-6 on page 455). Figure 14-6. Create the action mapping -
Click Finish . This creates the com.ibm.webbookstruts.actions.AddABookAction class. -
Save the file and close the Java editor. Close the Web diagram editor. -
To test the Struts Web application, we will create a server project and run the application on it. -
In the J2EE Navigator view, select the addABook.jsp file and select Run On Server from the context menu. In the server selection dialog, select the WebSphere Express 5.0 Test Environment . Click Finish . The following page is displayed. Enter some values to add a new book. -
From the Servers control panel, stop the server: select the server and click the Stop button. |