WLSKB-003 VALIDATE USER INPUT IN WEBLOGIC WORKSHOP TUTORIAL

VALIDATE USER INPUT IN WEBLOGIC WORKSHOP TUTORIAL

INTRODUCTION

The Page Flow technology builds on top of Struts and uses Struts techniques to validate user input. Error messages are shown using the NetUI tags instead of Struts tags. In this tutorial, you learn about how to enable server side input validation.

BEFORE YOU GET STARTED

This tutorial is a continuation of WLSKB002. You must successfully complete that tutorial before starting this one.

ADD VALIDATION LOGIC

Validation logic is entered in the form bean class. In our case, the name of the form bean class is LoginFormBean. It is a inner class of the Page Flow class LoginFlowController.

Double click on the LoginFlowController.jpf file to open it in the Page Flow editor. Click on the Source View tab at the bottom of the editor.

Scroll to the top of the class. Right below the line:

package LoginFlow;

Add a few import statements.

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

Scroll down and locate the LoginFormBean class.

Validation is performed from a method called validate(). The framework calls this method automatically after all the URL parameters have been transfered to the bean’s properties. Add the following method to the LoginFormBean inner class.

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errs = new ActionErrors();
 
    if (getUserId() == null || getUserId().length() == 0) {
        errs.add("userid_error", 
		   new ActionError("userid_missing"));
    }
    if (getPassword() == null || getPassword().length() == 0) {
        errs.add("password_error", new ActionError("password_missing"));
    }
    //Password must be 5 characters
    if (getPassword() != null && getPassword().length() < 5) {
        errs.add("password_error", new ActionError("password_short"));
    }
 
    return errs;
}

Here, values like userid_error indicate the field in the form where the error took place. Values like userid_missingindicate a key to the error message in the message bundle file.

Note: Make sure that you have added the validate() method within the LoginFormBean inner class and not the LoginFlowController class.

Save changes (Control+S).

SPECIFY ERROR HANDLER PAGE

Every action can specify a page that is displayed when validation fails. In our case, we will like to redisplay the form – that is show the index.jsp page.

Locate the Login() method and specify the error handler page using the @jpf:validation-error-forward XDoclet tag as shown in boldface below.

 /**
 * @jpf:action
 * @jpf:forward name="success" path="home.jsp"
 * @jpf:forward name="failed" path="login_failed.jsp"
 * @jpf:validation-error-forward path="index.jsp" name="error"
 */
 protected Forward Login(LoginFormBean form)

Save changes.

BUILD THE MESSAGE BUNDLE FILE

A message bundle (or resource bundle) file contains display text. It is often used to externalize translated multi-lingual text. In our case, we will store validation error messages there.

The message bundle file name must be specified for each Page Flow. To do that, scroll up the Page Flow source editor. Locate the line where the LoginFlowController class is declared.

 * </view-properties>
 * ::
 */
 public class LoginFlowController extends PageFlowController
 {

Specify the resourec bundle file name using the @jpf:message-resources XDoclet tag as shown in boldface below.

 * </view-properties>
 * ::
 * @jpf:message-resources resources="MyMessages"
 */
 public class LoginFlowController extends PageFlowController

Save and close the file.

The message bundle files are stored in the WEB-INF/classes folder as shown below.

Right click on the classes folder and select New->Other File Types.

Change the file name to MyMessages.properties. Click on Create.

Enter the following lines in the properties file.

error.prefix=<font color="red">
error.suffix=</font>
userid_missing=User id can not be empty.
password_missing=Password can not be empty.
password_short=Password must be at least 5 characters long.

Save and close the file.

DISPLAY ERROR MESSAGES

Open index.jsp in the editor. Click on the Source View tab.

Show the error messages using the <netui:error> tag as shown in boldface below.

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

Save and close the file.

TEST

Start the WebLogic server if it is not running (Tools->WebLogic Server->Start WebLogic Server).

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

Try to create all three error conditions:

  1. Missing user ID
  2. Missing password
  3. Password less than 5 characters long.

CONCLUSION

After you complete this tutorial, make sure that you know:

  1. How to add data input validation logic in the form bean class.
  2. How to specify the validation error handler page for the action.
  3. How to create a message bundle file and specify the file name for the Page Flow.
  4. How to display validation error messages for each input field.