Tutorials > Program model > Create new business logic

< Previous | Next >


Parse and validate URL parameters in MyNewControllerCmd

In this step, we will modify the controller command to use parameters that are passed in through the URL that calls the controller command. The command includes validation logic to ensure all required parameters are supplied and that the parameter values are appropriate.

The validateParameters method that is currently in your new command is only a command stub. It consists of the following code:

public void validateParameters() throws ECApplicationException {
}

You must now add customized parameter checking to the command and pass the URL parameters to the JSP page. When modifying the validateParameters method, you add new fields that correspond to the URL parameters.

In this section of the tutorial, you will learn the following information:

Add new fields to MyNewControllerCmd

In this step, we will create two new fields for the URL parameters and add them to both the command interface and implementation class. One URL parameter is a string value that holds a user name and the second is an integer that will be used to accept an input value of bonus points.


Procedure

  1. In the Enterprise Explorer view, navigate to the following package: WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.

  2. Open the MyNewControllerCmd.java interface to view its source code.

  3. Uncomment Section 2 to add the new fields and corresponding getter methods to the interface:

    /// Section //////////
    
    // set interface methods
      
      
      public java.lang.Integer getPoints() ;
      
      public java.lang.String getUserName() ;
    
      public void setPoints(java.lang.Integer newPoints) ;
    
      public void setUserName(java.lang.String newUserName)  ;
    
      
    /// End of section/////////
    

  4. Save the changes.

  5. Open the MyNewControllerCmdImpl.java class to view its source code.

  6. Uncomment Section 1 in the main class to add the new fields as well as their corresponding getter and setter methods to the class:

    /// Section //////////
    
    /// create and implement controller command's fields and accessors 
    /// (setter/getter methods)
    
      private java.lang.String userName = null;
      private java.lang.Integer points;
      
        
      public java.lang.Integer getPoints() {
        return points;
      }
      
      public java.lang.String getUserName() {
        return userName;
      }
    
      public void setPoints(java.lang.Integer newPoints) {
        points = newPoints;
      }
    
      public void setUserName(java.lang.String newUserName) {
        userName = newUserName;
      }
    
    /// End of Section ////////
    

  7. Save the changes.

  8. Catch missing parameters and validating values

    In this section, we will modify the validateParameters method to introduce error checking and parameter validation logic. Once modified, the code checks for the following conditions:

    • The first parameter is required. If the first input parameter is not provided, a "parameter not found" exception is thrown. In this case, the generic error page displays to the customer.

    • The second input parameter is optional. As such, if the second input parameter is not provided, a "parameter not found" exception is not thrown. Instead, the value for the second input parameter is defaulted to zero and the customer is not affected by the error. Processing continues.

    To add error checking and parameter validation:

    1. Open the MyNewControllerCmdImpl.java file to view its source code

    2. In the Outline view, select its validateParameters method.

    3. In the source code of the validateParameters method, uncomment Section 1 to introduce the following code into the method:

      /// Section ///////////
      
      /// uncomment to check parameters
          
          final String strMethodName = "validateParameters";
          
        TypedProperty prop = getRequestProperties();
      
        /// retrieve required parameters 
        try { 
          setUserName(prop.getString("input1"));
          
        } catch (ParameterNotFoundException e) {
          /// the next exception uses _ERR_CMD_MISSING_PARAM ECMessage object 
           /// defined in ECMessage class
          throw new ECApplicationException(ECMessage._ERR_CMD_MISSING_PARAM, 
              this.getClass().getName(), strMethodName,        ECMessageHelper.generateMsgParms(e.getParamName()));
        }
      
        /// retrieve optional Integer
        // set input2 = 0 if no input value 
        setPoints(prop.getInteger("input2",0));
      
      /// End of section////////////
      

      The preceding code snippet checks the two input parameters. The try block determines if the first parameter exists; if not, an exception is thrown. Since the second parameter is optional, this code sets the value to zero if it is either missing or an incorrect value.

    4. At this point, there are three remaining compilation errors.

      To correct then, click the red indicator to the right of the editor's scroll bar. Your cursor is highlights one of the three compilation errors.

    5. Right-click the highlighted text and select Source > Organize Imports.

    6. Save the changes.

  9. Add new fields to MyNewDataBean

    In this section, we will add new fields and their associated getter methods to the MyNewDataBean data bean, so that the URL parameters will be available to the JSP page.

    To modify MyNewDataBean:

    1. Navigate to WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.databeans > > MyNewDataBean.java to view its source code.

    2. Uncomment Section 2 to introduce the following code into the class:

      /// Section //////////
      
        private java.lang.String userName = null;
        private java.lang.Integer points;
        
        
        public String getUserName() {
          return userName;
        }
        
        public void setUserName(java.lang.String newUserName) {
          userName = newUserName;
        }
        
        
        public Integer getPoints() {
          return points;
        }
        
        public void setPoints(java.lang.Integer newPoints) {
          points = newPoints;
        }
        
        
      /// End of Section ////////
      

    3. Save the changes.

  10. Pass URL parameters to the view

    In this section, we will include the code to pass the input parameters to the JSP page. You will pass the parameters by setting fields in the data bean with the values of the input parameters.

    To pass the URL parameters:

    1. Open MyNewControllerCmdImpl.java.

    2. In the Outline view, select the performExecute method.

    3. In the source code of the performExecute method, uncomment Section 3C, introducing the following code into the method:

      /// Section ////////
      
        // pass the input information to the databean
        mndb.setUserName(this.getUserName());
        mndb.setPoints(this.getPoints());
      
      /// end of section ////////
      

      This code sets the values in the data bean object so that the values will be available to the JSP page.

    4. Save the changes.

  11. Modify MyNewJSPTemplate to display the URL parameters

    In this step, we will modify the MyNewJSPTemplate.jsp file to add a new section that displays the URL input parameters:

    1. If the JSP files are not already open:

      1. In the Enterprise Explorer view, navigate to the Stores > WebContent > ConsumerDirect_name sub-folder.

      2. Highlight both the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files, right-click then click Open.

    2. Copy Section 6 from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file to introduce the following text into the JSP page:

      <fmt:message key="UserName" bundle="${tutorial}" />
      <c:out value="${mndbInstance.userName}"/>
      <br>
       
      <fmt:message key="Points" bundle="${tutorial}" /> 
      <c:out value="${mndbInstance.points}"/>
      <br> 
        
      

    3. Save the changes.

  12. Test URL parameter values

    1. Start or restart the test environment.

    2. Navigate to the Stores > WebContent > ConsumerDirect_name directory.

    3. Select the index.jsp file and from its pop-up menu select Run As > Run on Server. The store home page displays in the Web browser.

    • Case 1: The first test case excludes both parameters from the URL. After the store home page displays, enter the following URL:

      http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd
      

      Since no parameters are passed to the command, a generic error page displays as in the following screen capture:

      The console in Rational Application Developer shows information similar to the following:

      6730e546 CommerceSrvr E com.ibm.commerce.sample.commands.MyNewControllerCmdImpl validateParameters 
      CMN0206E Please check all fields. "input1" is a required field. 
      

      If you do not see this error in the console, check WCDE_INSTALL/\wasprofile\logs\server1\SystemOut.log and trace.log. The console is of limited length and cannot display the entire contents of SystemOut.log.

    • Case 2: The next test case is to use a valid first parameter, but omit the second parameter. In this case, you expect that no error should be detected, since by default, a zero value will be used for the missing second parameter. Enter the following URL: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc The MyNewJSPTemplate page displays and a zero value is shown for input2.

    • Case 3: In this test case, a valid parameter is provided for the first input parameter, and an invalid parameter is provided for the second parameter (a string is used rather than an integer). Similar to the last case, you should not see an error, since the error handling changes the second input parameter to a zero. Enter the following URL:

      http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc&input2=abc
      

      The MyNewJSPTemplate page displays and a zero value is shown for input2.

    • Case 4: In this case, valid parameters are used for both of the URL input parameters. Enter the following URL:

      http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc&input2=1000
      

      The MyNewJSPTemplate page displays and 1000 points display for the user.

< Previous | Next >


+

Search Tips   |   Advanced Search