Rapid Struts Development in JDeveloper

Please refer http://www.oracle.com/technology/oramag/oracle/04-sep/o54jdev.html

 

 

Use Oracle JDeveloper 10g to build Struts applications.

Would you like to avoid having to create the same Struts structure again and again? The Oracle JDeveloper 10g integrated development environment (IDE) lets you skip some Struts tedium and do what you want to do: build Struts applications quickly.

This article shows how Oracle JDeveloper 10g can help you automate a lot of Struts tasks and develop Struts applications. It details the development of a sample Struts application that demonstrates the usage of various Oracle JDeveloper 10g tools, such as the new Struts flow diagram.

Create a Struts Project

If you don't already have Oracle JDeveloper 10g installed, you can install the free download from OTN (oracle.com/technology ).

Start Oracle JDeveloper 10g . Create a new application workspace for the Struts sample application as follows:

1. Choose File->New.
2. Select Application Workspace from the New Gallery, and click on OK.
3. In the Create Application Workspace dialog box, shown in Figure 1 , enter oraclemag as the Application Name and the Application Package Prefix.
4. For the Application Template, choose Web Application [JSP, Struts, EJB] from the drop-down menu.
5. Click on OK.

figure 1
Figure 1: Create Application Workspace dialog box

Oracle JDeveloper 10g creates a new workspace named oraclemag that contains two projects: Model and ViewController. The ViewController project is where the application's Servlets, JSP, and Struts files are located.

Expand the nodes under the ViewController project, and you will see the following three files:

 

  • struts-config.xml: This is a key Struts configuration file.
  • ApplicationResource.properties: This properties file is normally used in Struts applications and configured as a message resource in the struts-config.xml file.
  • web.xml: This file is also known as the Web deployment descriptor and is the file where configuration details for a Web application are maintained.

In addition to generating these Struts files, Oracle JDeveloper 10g modifies the web.xml file to define a new servlet named action and map all requests in the form *.do to that servlet.

You can build on this structure to develop a Struts application, as illustrated in the following section.

Struts Flow Diagram

One of the most useful features of Oracle JDeveloper 10g is the Struts flow diagram. Struts does a great job of managing flows in the application, but understanding these flows by looking through an XML configuration file can be quite difficult. With the Struts flow diagram, you can now easily depict, in just one diagram, all the flows in a system. The interactive nature of the diagram means that you can also make changes and additions to the application from within the diagram.

The easiest way to create a new Struts flow diagram is to do the following:

1. In the Applications Navigator, double-click on struts-config.xml.

A blank canvas and the Component Palette for Struts Page Flow appear. The palette has components for all the commonly used Struts elements.

2. To add a new Action, Page, or Forward, click on the component in the palette and then click on the canvas. (You can also drag and drop components from the palette to the canvas.)

Now let's look at the sample application and create the various components by using the flow diagram.

Developing the Application

The sample Struts application featured in this article is a simple one, having only two pages. On page 1, the application provides an HTML form where the user enters a flight number. The application processes this number and displays the status of the flight on page 2. You can download the complete application at oracle.com/technology/oramag/oracle/04-sep/jdevstruts.zip .

The sample application has the following components:

 

  • oraclemag.view.FlightStatusAction class: This Action class processes the flight number and sets the flight status message into the request.
  • index.jsp: This is the main page that accepts the flight number as input from the user.
  • result.jsp: This page displays the flight status, using the text set into the request by the class FlightStatusAction.

First add the pages index.jsp and result.jsp to the flow diagram.

With the Struts Flow Diagram canvas and the Struts Page Flow Component Palette open, as described in the preceding section, "Struts Flow Diagram," add the index.jsp and result.jsp pages to the application as follows:

1. Click on the Page component in the Component Palette, and then click anywhere on the flow diagram. A new page named untitled1.jsp appears in the diagram.
2. Rename untitled1.jsp to index.jsp.
3. Add another page, as described in Step 1, and rename this page to result.jsp.
4. Double-click on each of the pages displayed in the flow diagram to generate the empty JSP pages.

Next, add an action named FlightStatusAction , as follows:

1. Click on the Action component on the Component Palette, and then click on the diagram. A new action named action1 is created by default.
2. Rename action1 to flightstatus .
3. Double-click on this action in the flow diagram; the Create Struts Action dialog box appears.
4. Modify the settings to create a new action oraclemag.view.FlightStatusAction , as shown in Figure 2 . (Name: FlightStatusAction, Packages: oraclemag.view , Extends: org.apache.struts.action .Action .)
5. Click on OK.

figure 2
Figure 2: Create Struts Action dialog box

The class is automatically generated for you. When you first create the JSP files and the Action class in the flow diagram, the file icons in the flow diagram display an exclamation mark (!) until you double-click on the icons to generate the files.

Now create a new action form for the new action. You will use this form to pass the flight number the user enters to the code in the Action class.

Editing struts-config.xml

Oracle JDeveloper 10g provides an editor for working with the struts-config.xml file, making Struts application management a lot easier. To launch the struts-config editor, right-click on the struts-config.xml file for the sample application in the Applications Navigator and choose Edit Struts-Config to bring up the Struts Configuration Editor, as shown in Figure 4 .

Using the Struts Configuration Editor allows you to edit files with dialog box controls rather than typing XML code and, therefore, maintain the integrity of the XML syntax.

Another useful struts-config.xml editor is the Struts Console, downloadable from www.jamesholmes.com/struts/console . As shown in Figure 5 , this freeware tool is quite accomplished and can be easily integrated with Oracle JDeveloper 10g as well as many other IDEs.

Add a new action form as follows:

1. Right-click on the /flightstatus Action icon displayed in the Struts flow diagram, and choose the Go to Form Bean option.
2. In the dialog box that pops up, change Name to FlightForm and FormBean to flightformbean.
3. Click on OK. The FlightForm class opens in the editor.

In the FlightForm class, add a new private String named flightNumber and public getter and setter methods for the field. To add the private String and flightNumber field, do the following:

With the FlightForm class open in the editor, click on the Class tab at the bottom of the editor and then click on the Fields tab at the top of the editor.
2. Click on the Add button.
3. In the Field Settings dialog box, Add Field appears. For Field Name, enter flightNumber ; for Field Type, leave the default java.lang.String.
4. For Scope (under Modifiers), select private from the drop-down menu.
5. Under Accessors, check the Create get() method and Create set() method boxes, and for Binding Status, choose Unbound from the drop-down menu.
6. Click on OK.

Next, create the flow of the application as follows:

1. In the Struts Page Flow Component Palette, click on the Page Link component.
2. In the Struts Page Flow diagram, click on /index.jsp and then on /flightstatus.

A dashed line and an arrow point from /index.jsp to /flightstatus in the diagram.
3. In the Struts Page Flow Component Palette, click on the Forward component.
4. In the Struts Page Flow diagram, click on /flightstatus and then on /result.jsp.

A solid line and an arrow point from /flightstatus to /result.jsp in the diagram.

You now have the diagram shown in Figure 3 . If you look at the struts-config.xml file contents, you will see that the necessary Struts configuration has been automatically generated by Oracle JDeveloper 10g . The struts-config.xml file now defines a new form and action and also associates the form with the action.

figure 3
Figure 3: Sample Application Page Flow diagram

 

figure 4
Figure 4: Struts Configuration Editor

figure 5
Figure 5: Struts Console

The basic application structure is in place; all you need to do to get the application working is to put a few lines of code into the JSPs and the Java class.

The JSPs

The user will enter the flight number on the index.jsp page. Add an HTML form and a Submit button to this page as follows in Step 1 and 2 here:

1. In the Struts Page Flow diagram, double-click on index.jsp. Note that index.jsp is linked to the flightstatus action in the Struts flow diagram.
2. To add the HTML form to index.jsp, you can use the Design or Source view. In the Design view, you can use the HTML, JSTL, or Struts HTML palette to add the form, the text field, and the Submit button. For this article, use the Source view and replace the code in the index.jsp file with the code in Listing 1 .

Note that the action name in Listing 1 , flightstatus , is the same as that defined in the struts-config file. By default, Oracle JDeveloper 10g sets up the web.xml file so that all requests in the form *.do are sent to the Struts action servlet, so the request for the flightstatus action will be in the form http://.../flightstatus.do.

Because the Java class will set the flight status message into a request attribute, the task of the result.jsp page is to pick and display this message. Listing 2 shows the necessary result.jsp page code. Replace the source code in result.jsp with the code in Listing 2 .

Action Class

In an earlier step, you created a new action class named FlightStatusAction . You will now add the flight status logic to that Java code.


Because the autogenerated Action class extends org.apache.struts.action.Action and has a basic implementation for the execute method, the task is simple. Nothing in the class, except the execute method, needs editing. Replace the execute method with the code shown in Listing 3 .

This code compares the flight number entered by the user with the values in the HashMap and accordingly sets a status message.

Run the Application

To run the sample application, right-click on index.jsp in the Applications Navigator and choose Run to start the input form. (You can also run the application from the Struts Page Flow diagram by right-clicking index.jsp and choosing Run.)

In this form, the values 1, 2, 3, and 4 will return a proper status message, whereas any other number will result in an "Invalid Flight Number" error message.

Conclusion

This article looked at Oracle JDeveloper 10g 's Struts capabilities and showed how easy it is to develop and manage Struts applications by using various Oracle JDeveloper 10g features. If you are new to Oracle JDeveloper, I think it's a good idea to download a copy of Oracle JDeveloper 10g and try developing new Struts applications or migrating existing Struts development to Oracle JDeveloper 10g .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值