Parse and validate the parameters in MyNewControllerCmd

In this lesson, you learn how to modify the controller command to use parameters that are passed through the URL that calls the controller command. The command includes validation logic to ensure that all required parameters are supplied and that the parameter values are appropriate.

About this task

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 {
}

In the following steps, you add 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 lesson, you learn how to perform the following tasks:
  • Add fields to a command to correspond to URL parameters
  • Use the getRequestProperties method to enable populating these fields with URL input parameters
  • Catch missing parameter exceptions
  • Pass URL parameters to a view

Procedure

  1. Add fields to the MyNewControllerCmd command. In this step, you create two fields for URL parameters and add the fields to 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 accepts the input value of bonus reward points.
    1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.
    2. Open the MyNewControllerCmd.java file for editing.
    3. Locate and uncomment section 2 to add the fields and corresponding getter methods to the interface. The uncommented section 2 code should resemble:
      /// Section 2 ////////////////////////////////////////////////
      
      // 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 2//////////////////////////////////////////
      
    4. Save your changes.
    5. Open the MyNewControllerCmdImpl.java file for editing.
    6. Locate and uncomment section 1 to add the fields and corresponding getter methods to the implementation class. The uncommented section 1 code should resemble:
      /// Section 1 ////////////////////////////////////////////////
      
      /// 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 1 /////////////////////////////////////////
      
    7. Save your changes.
  2. Modify the validateParameters method to add error checking and parameter validation logic. The modified method 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.
    1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.
    2. Open the MyNewControllerCmdImpl.java file for editing.
    3. In the Outline view, select the validateParameters method.
    4. Locate and uncomment section 1 to add code to check input parameters. The try block determines whether 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. The uncommented section 1 code should resemble:
      
      /// Section 1 /////////////////////////////////////////////////
      
      /// 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 1/////////////////////////////////////////////
      
    5. Right-click the src package; select Source > Organize Imports. All errors should be resolved in the project.
    6. Save your changes.

  3. Modify MyNewDataBean data bean to add fields and their associated getter methods to the MyNewDataBean data bean to allow URL parameters to be available to the JSP pages.
    1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.databeans.
    2. Open the MyNewDataBean.java file for editing.
    3. Locate and uncomment section 2 to add code to check input parameters. The try block determines whetherf 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. The section 2 code you uncomment should resemble:
      /// Section 2 ///////////////////////////////////////////
      
        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 2 ////////////////////////////////////
      
    4. Right-click the src package; select Source > Organize Imports. All errors should be resolved in the project.
    5. Save your changes.
  4. Modify the MyNewJSPTemplate to include code to pass input parameters to the JSP page. You pass the parameters by setting fields in the data bean with the values of the input parameters.
    1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.
    2. Open the MyNewControllerCmdImpl.java file for editing.
    3. In the Outline view, select the performExecute method.
    4. Locate and uncomment section 3c. This code sets the values in the data bean object so the values can be available to the JSP pages. The section 3c code you uncomment should resemble:
      /// Section 3C/////////////////////////////////////////
      
        // pass the input information to the databean
        mndb.setUserName(this.getUserName());
        mndb.setPoints(this.getPoints());
      
      /// end of section 3C/////////////////////////////////////////
      
    5. Save your changes.
  5. Modify the MyNewJSPTemplate to display the URL parameters
    1. In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
    2. Open both the MyNewJSPTemplate_All.jsp file and the MyNewJSPTemplate.jsp file for editing.
    3. In the MyNewJSPTemplate_All.jsp file, locate and copy the code in section 6 markers. Paste the code into the section 6 markers in the MyNewJSPTemplate.jsp file to add the following code 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> 
      
    4. Save your changes.
  6. Test the URL parameter values
    1. Start or restart the WebSphere Commerce test server
    2. In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
    3. Right-click the index.jsp file; click Run As > Run on Server. Your storefront page displays in the web browser.
      Note: If prompted to select a server, select Choose an existing server and click Finish.
    4. In the web browser, enter the following URL: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd

      The JSP page displays in the web browser. Both parameters are excluded from the URL and with no parameters passed to the command, a generic error page displays: Screen capture of MyNewControllerCmd JSP page.

      The console in Rational Application Developer shows information similar to the following message:
      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_installdir\wasprofile\logs\server1\SystemOut.log and trace.log. The console is of limited length and cannot display the entire contents of SystemOut.log.
    5. In the web browser, enter the following URL: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=abc&input2=1000. A valid parameter is provided for both parameters. The MyNewJSPTemplate page displays in the web browser and 1000 points displays for the user:

      Screen capture of My new JSP template page with valid parameters shown