14.2 Developing Struts Web applications

 < Day Day Up > 

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 :

  1. Create a Struts Web application:

    1. Switch to the Web perspective.

    2. Select File -> New -> Web -> Web Project .

    3. Enter the project name of WebBookStruts .

    4. Select the Web Project features Add Struts support . This adds the necessary support for Struts into your Web project.

    5. Click Next > .

    6. In the J2EE Settings Page, click Next > again.

    7. 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

      graphics/14fig02.jpg

    8. Click Finish .

  2. Next, graphically lay out the Web application using the Web Diagram Editor:

    1. Select File -> New -> Other -> Web -> Struts -> Web Diagram to create a new Web diagram.

    2. Select the WebBookStruts Web project and enter the name BookWeb.gph . Click Finish . The Web diagram editor opens.

    3. 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

      graphics/14fig03.jpg

    4. Add two Web page nodes. Change their names to addABookResponse.jsp and error.jsp .

    5. Select New -> Action Mapping Node to add a new action mapping node. Change its name to addABook .

    6. Select New -> Form Bean Node . Name it newBook and set the scope to request .

    7. Select addABook.jsp and choose Connection from the context menu. Draw a connection to addABook .

    8. Connect addABook to addABookResponse.jsp . Label the connection success .

    9. Connect addABook to error.jsp . Label the connection failure .

    10. Connect addABookResponse.jsp to addABook.jsp .

    11. Connect error.jsp to addABook.jsp .

    12. Connect newBook to addABook .

    13. 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.

      graphics/14fig04.jpg

    14. Press Ctrl-S to save the Web diagram.

  3. From the Web diagram, we create first the newBook action form class as follows:

    1. Double-click newBook to launch the New Action Form Class file wizard.

    2. On the first page, click Next > .

    3. On the Choose new accessors page, click Next > again.

    4. On the Create new accessors page, add four new accessors of type String: title, isbn, author and category .

    5. Add one accessor price of type java.mathBigDecimal.

    6. Click Next > , then Finish . Close the Java editor.

  4. From the Web diagram, we can now create the JSP and complete its content as follows:

    1. Double-click addABook.jsp to launch the New JSP file wizard.

    2. Select XHTML as the Markup Language. Click Next > and on the second page Next > again.

    3. On the Page Directive Information Page, disable Generate a Page Directive Information .

    4. Click Next > three times.

    5. On the Form Field Selection page, select newBookForm as the form bean.

    6. By pressing the shift key, select all four fields of the bean.

      Figure 14-5. JSP File wizard to create an html form

      graphics/14fig05.jpg

    7. Click Next > and Finish .Close the Web designer.

  5. From the Web diagram, double-click the error.jsp . Select XHTML as the Markup language and disable the page directive generation. Click Finish .

  6. In the Web page designer, enter an error message and close the Web designer.

  7. From the Web diagram, double-click the addABookResponse.jsp . Select XHTML as the Markup language and disable the page directive generation. Click Finish .

  8. In the Web page designer, enter a success message and close the Web designer.

  9. Next, create the Struts Action class. The action class receives control from the input page using the input data stored in the form bean.

    1. Double-click the addABook action mapping to launch the New Action Mapping dialog.

    2. In the Form Bean Name field, select the newBookForm bean (see Figure 14-6 on page 455).

      Figure 14-6. Create the action mapping

      graphics/14fig06.jpg

    3. Click Finish . This creates the com.ibm.webbookstruts.actions.AddABookAction class.

    4. Save the file and close the Java editor. Close the Web diagram editor.

  10. To test the Struts Web application, we will create a server project and run the application on it.

    1. 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.

    2. From the Servers control panel, stop the server: select the server and click the Stop button.

 < Day Day Up > 


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net