WLSKB-002 PROCESSING USER INPUT FROM A PAGE FLOW IN WEBLOGIC WORKSHOP TUTORIAL

PROCESSING USER INPUT FROM A PAGE FLOW IN WEBLOGIC WORKSHOP TUTORIAL

INTRODUCTION

One of the main responsibilities of a controller in the Model View Controller (MVC) pattern is to convert String based user input data available from a HTTP request to more complex data types used by the model. The process of converting HTTP request data to Java objects can be tedious. The process was automated in the Struts framework. The Page Flow technology is based on Struts. As in Struts, a JavaBean – called a form bean – is associated with an action. An action is associated with a form. When the form is submitted, system instantiates the form bean object and populates its properties by matching the property name with the form element name. Struts is capable of performing basic data type conversion – such as String to double or int.

In this tutorial, we will develop a login form, a form bean and an action that processes the form submission. If the login succeeds, the flow will display the home page. Otherwise, an error page will be shown.

BEFORE YOU GET STARTED

Launch WebLogic Workshop. Create an application called BasicApp unless it is already created from a previous tutorial. See the Build a Project Structure section of the WLSKB001 article to find out how to do this. If you are new to WebLogic Workshop, it is highly recommended that you complete that tutorial before starting this one.

CREATE THE PAGEFLOW

Right click on BasicWebApp and select New->Page Flow.

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

CREATE THE FORM BEAN

On the right hand side of the screen, locate the Data Palette. Next to the Form Beans section, click on Add and select Form Bean.

Enter LoginFormBean as the name of the form bean.

Add two property fields to the bean as follows.

  1. userId (String)
  2. password (String)

Click on OK.

Workshop creates the form bean class as an inner class of the Page Flow class. At the bottom of the editor, click on the Source View tab. Locate the LoginFormBean class as shown below.

Save changes to the Page Flow class (Control+S).

ADD THE ACTION METHOD

At the bottom of the editor, click on the Flow View tab. Right click on anywhere in the editor and select New Action.

Set the name of the action to Login. Associate the LoginFormBean with the action. Click on OK.

Right click anywhere on the editor and select New Page. Change the name of the page to home.jsp. Similarly, create another page called login_failed.jsp.

Draw an arrow from the Login action to home.jsp. Change the forward name to success. Draw an arrow from the Login action to login_failed.jsp. Change the forward name to failed.

Now we are ready to implement the login logic. Double click on the Login action’s icon. Workshop will take you to the Login method in the Java editor. Notice, that a LoginFormBean object is passed to the method.

Change the implementation of the method as follows.

protected Forward Login(LoginFormBean form) {
   String forward_name = "";
   String message = "";
 
   if (form.getUserId().equals("bob") && form.getPassword().equals("bobcat")) {
      forward_name = "success";
      //Store the user ID in the session
      getRequest().getSession().setAttribute("userId", form.getUserId());
   } else if (form.getUserId().equals("bill")) {
      forward_name = "failed";
      message = "Your account has expired."; 
   } else {
      forward_name = "failed";
      message = "Invalid user ID and password."; 
   }
 
   //Store the message in the request context
   getRequest().setAttribute("message", message);

   return new Forward(forward_name);
}

Save changes (Control+S). The business logic is as follows. Only the user ID of bob with password bobcat is valid. The user ID bill is rejected. For all other cases, the user ID and password are invalid.

DEVELOP THE VIEWS

Open the index.jsp file in the editor. This file will contain the login form. Click on the Source View tab at the bottom of the editor. Change the contents of the <body> tag to as shown below.

<body>
 <netui:form action="Login">
  User ID:<br>
  <netui:textBox dataSource="{actionForm.userId}"/> <br>
  Password:<br>
  <netui:textBox dataSource="{actionForm.password}" password="true"/> <br>
  <netui:button type="submit" value="Login"/>
 </netui:form>
</body>

Note: The form is associated with the Login action. The dataSource value of {actionForm.userId} indicates that when the form is submitted, system will transfer the contents of the text box to the userId attribute of the form bean.

Save and close index.jsp.

Open home.jsp. Set the contents of the <body> tag to as shown below.

<body>
 <h2>Home Page</h2>
 <p>
 Welcome <netui:label value="{session.userId}"/>.
 </p>
</body>

Here, we show the user ID from the session. Save and close the file.

Open login_failed.jsp. Set the contents of the <body> to as shown below.

<body>
 <h2>Login Failed</h2>
 <p><netui:label value="{request.message}"/></p>
</body>

Here, we show the error message by fetching it from the request context. Save and close the file.

TEST

From the menubar, select Tools->WebLogic Server->Start WebLogic Server. Wait for the server to start.

Open a browser window and enter the URL: http://localhost:7001/BasicAppWeb/LoginFlow/begin.do

Enter bob as the user id and bobcat as the password. Make sure that you can login.

Try entering bill as the user ID.

CONCLUSION

In this tutorial we learned how to process user input using a form bean. The framework makes it easy to initialize a JavaBean (the form bean) with input data from the HTTP request. The JavaBean can then be passed to the model layer as an input to a business transaction.

In a later tutorial, we will add form validation.