WLSKB-001 DEVELOPING A PAGE FLOW IN WEBLOGIC WORKSHOP TUTORIAL

DEVELOPING A PAGE FLOW IN WEBLOGIC WORKSHOP TUTORIAL

INTRODUCTION

WebLogic Workshop introduces a new framework that implements the Model View Controller (MVC) pattern. The framework, as it exists within Workshop, has no single official name. It is generally known as the Page Flow and Control framework. BEA has donated the framework to Apache as the Beehive project.

The View and the Controller portion of the framework – called Page Flow – is built as an extension of the Struts framework.

This is how the MVC pattern is implemented.

Model

The model layer can be built using Plain Old Java Objects (POJO) or EJB. Workshop introduces an alternative technology – called Control – that can also be used to implement the model layer.

View

The view layer is built using JSP and a set of custom tags supplied by the Workshop framework. These tags are known as the NetUI tags. Struts custom tags have been completely abandoned by the framework and replaced by the NetUI tags.

Controller

The controller layer is built using Page Flow. If you are new to Page Flow, think of it as a replacement for Servlet or Struts Actions. A Page Flow is a Java class with .jpf file name extension. You develop public methods in that class. The method’s name can be used as the action attribute of a form. When the form is submitted, system executes the method. These methods are called actions. Behind the scene, system actually registers a Struts action for each of these methods. One advantage of Page Flow over tradtional Struts action is that, a Page Flow can have many methods. Essentially, multiple related Struts actions can be implemented using a single Page Flow class.

Struts and Page Flow

It is almost an academic discussion that the Page Flow framework uses Struts under the covers. On the surface the two are significantly different. If you know Struts, you will still need to learn about Page Flow. The motivation behind Page Flow was to simplify web development beyond the level of simplicity already achieved by Struts.

The focus of this article is the Controller layer, or, Page Flow. Subsequent articles will get into the other two layers of MVC.

BUILD A PROJECT STRUCTURE

Launch WebLogic Workshop. From the menubar, select File->New->Application.

Enter BasicApp as the name of the application. Click on Create.

You can think of an application as a J2EE Enterprise Application. You can add mutiple web and EJB modules within the application. An application is exported as a EAR file that can be deployed in a production server.

An application is configured to be deployed to a WebLogic server for testing and debugging. A test server is already configured in the samples/domains/workshop folder as a part of the Workshop installation. Applications are deployed to that server by default. (See the Server dropdown list in the screenshot above).

When an application is created, system automatically adds a web module project. In our case, this project is called BasicAppWeb.

You can add additional web or EJB module projects to this application. We are not going do that for this article.

CREATE A SIMPLE PAGE FLOW

A controller has two primary responsibilities:

  1. Convert data from HTTP request to a data structure the model layer can work with. In HTTP, all request data is of String data type. The controller needs to build input objects and convert String to other data types if needed (such as int or java.sql.Date).
  2. Determine the view that needs to be displayed under a given circumstance. For example, after the login action is performed, a controller needs to display the home page. This act of view selection is essentially what determines the page navigation flow of a web site.

In this step, we will learn about the second responsibility of a controller – page navigation flow. We will build a controller (Page Flow class), that will display odd.jsp or even.jsp depending on whether the number URL parameter is odd or even. It is a made up business problem but illustrates how a controller employs some kind of logic to decide which page should be displayed to the user.

Right click on the BasicAppWeb project and select New->Page Flow.

Enter OddEvenFlow as the name of the Page Flow. Click on Next. Click on Create.

This will create the Page Flow class OddEvenFlowController.jpf under the OddEvenFlow folder as shown below.

IMPLEMENT THE PAGE NAVIGATION LOGIC

Every Page Flow must have an action called begin. When the flow begins execution, this action method is executed. When a new Page Flow is created, Workshop automatically sets it up as follows.

How to read this diagram? It essentially means, that under success condition, the begin action shows the index.jsp page. In other words, if the outcome of the begin action is a success, index.jsp is shown. The condition name (success) is also known as a forward name.

In our case, the begin action will have two possible outcomes – the number URL parameter is either odd or even.

Right click on the index.jsp icon and delete it. Right click anywhere on the editor and select New Page. This will create a new JSP page called newPage1.jsp. Click on the filename and rename it to odd.jsp.

Similarly create another JSP page called even.jsp.

Move the mouse below the begin action. System should show three little rectangles. Click on one of them and draw an arrow to even.jsp. By default, system calls the condition or forward name success. Click on success and change the name to even. Similarly, draw an arrow from begin to odd.jsp and change the condition name to odd. The diagram should look like this.

Save changes to the OddEvenFlowController.jpf file (Control+S).

WRITE THE ACTION CODE

Double click on the begin action’s icon in the diagram. You should see the following code.

 /**
 * This method represents the point of entry into the Page Flow
 * @jpf:action
 * @jpf:forward name="even" path="even.jsp"
 * @jpf:forward name="odd" path="odd.jsp"
 */
 protected Forward begin()
 {
     return new Forward("success");
 }

Let’s study the action. First of all, the existance of the @jpf:action XDoclet tag identifies this otherwise regular member function as an action. Once again, action methods are special in the way that they can be associated with a form. The @jpf:forward XDoclet tag is used to decribe the JSP file names for each forward name. An action method must return a Forward object indicating the JSP file that should be shown.

Change the action’s code as follows.

 protected Forward begin()
 {
     String strNumber = getRequest().getParameter("number");
     int number = Integer.parseInt(strNumber);
 
     if (number % 2 == 0) {
         return new Forward("even");
     }
 
     return new Forward("odd");
 }

Save changes. That’s it. This utterly simple example shows you how to control the page flow.

DEVELOP THE VIEW

In the Application pane, double click on odd.jsp to open it in the editor. At the bottom of the editor, click on the Source View tab. Change the contents of the <body> tag as shown below.

 <body>
 <h2>Odd Number</h2>
 <p>You have entered <netui:label value="{url.number}"/>.
 </body>

Save changes (Control+S). The JSP page essentially uses the <netui:label> custom tag to show the number URL parameter.

Similarly, change the contents of the <body> tag of the even.jsp file as shown below.

 <body>
 <h2>Even Number</h2>
 <p>You have entered <netui:label value="{url.number}"/>.
 </body>

Save changes.

TEST

From the menubar select Tools->WebLogic Server->Start WebLogic Server.

Wait for the server to start.

Open a web browser. Enter the URL: http://localhost:7001/BasicAppWeb/OddEvenFlow/begin.do?number=14.

Try entering other numbers.

A few things to note of the URL:

  1. The context root of the web module is BasicAppWeb.
  2. The existence of begin.do in the URL indicates that the begin action should be executed.
  3. Since, begin is a special action (default starting point of the flow), we can also execute it by using the flow’s class name. For example: http://localhost:7001/BasicAppWeb/OddEvenFlow/OddEvenFlowController.jpf?number=14

CONCLUSIONS

This tutorial should help you understand the following points.

  1. A Page Flow is a regular Java class with the .jpf file name extension.
  2. Certain public methods of this class can be designated as actions using the @jpf:actionXDoclet tag. Action methods can be executed by sending a HTTP request. The URL must contain the action method’s name with a .do extension.
  3. An action method must decide what page should be displayed following the execution of the action. This is done by returing an appropriate Forward object. All applicable forward names and corresponding JSP file names must be delcared using the @jpf:forward XDoclet tag adorning the action method.