Tutorials > Program model > Create new business logic

< Previous | Next >


Use a data bean to pass information from MyNewControllerCmd to MyNewView

In this step, we will learn how to create a new data bean that passes information to the JSP page. You will add code to determine if the view was called directly, or if it was called by the controller command. In the latter case, the JSP page should also display the name of the command that called it.

In order to pass this information to the view, you create a new data bean called MyNewDataBean. The MyNewJSPTemplate is also modified so that it can display the new information.

MyNewDataBean is used strictly for making information available from the controller command to the JSP page. In contrast, later in the tutorial we will learn how to create a new data bean to make information from the database available to the JSP page.

You might wonder why this tutorial is using a data bean just to make information available from the controller command. There are two reasons for creating this bean:

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

Create MyNewDataBean

The MyNewDataBean data bean is used to pass information to the MyNewJSPTemplate.jsp page. As with other sections of the tutorial, a base for the data bean is provided in the sample code. The code is split into sections that are commented out. As you progress through the tutorial, you uncomment various sections of the code.

To create MyNewDataBean:


Procedure

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

  2. Open MyNewDataBean.java to view its source code.

  3. In source code for the main class, uncomment Section 1 to introduce the following code into the class:

     /// Section ////////////
    /// create fields and accessors (setter/getter methods) 
    
      private java.lang.String callingCommandName = null;
      private boolean calledByControllerCmd = false;
      
      
      public java.lang.String getCallingCommandName() {
        return callingCommandName;
      }
      
      public void setCallingCommandName(java.lang.String newCallingCommandName) 
      {
        callingCommandName = newCallingCommandName;
      }
      
      public boolean getCalledByControllerCmd() {
        return calledByControllerCmd;
      }
      
      public void setCalledByControllerCmd(boolean newCalledByControllerCmd) 
      {
        calledByControllerCmd = newCalledByControllerCmd;
      }
      
    /// End of Section ////////
    

    The preceding code introduces two variables that display information when the view is returned by a controller command, rather than being called directly by the URL for the view.

  4. Save the changes.

  5. Instantiating MyNewDataBean and setting its attributes using MyNewControllerCmd

    In this section we will modify MyNewControllerCmdImpl to instantiate MyNewDataBean and set the attributes of this bean.

    To modify MyNewControllerCmdImpl:

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

    2. Open MyNewControllerCmdImpl.java to view its source code.

    3. In the Outline view, select its performExecute method.

    4. In the source code for the performExecute method, uncomment Sections 3A and 3B to introduce the following code into the method:

      /// Section ////////
      
        /// instantiate the MyNewDataBean databean and set the properties, 
        ///  then add the instance to resProp for response
        
        MyNewDataBean mndb = new MyNewDataBean();
        mndb.setCallingCommandName(this.getClass().getName());
        mndb.setCalledByControllerCmd(true);
      
      /// end of section ////////
      
      /// Section ////////
        rspProp.put("mndbInstance", mndb);
      
      /// end of section ////////
      

      The preceding code snippet instantiates the MyNewDataBean object, sets two parameters in the object (indicating that it was called by a controller command and which command called it), and puts the data bean object into the response properties so that it will be made available to the JSP template.

    5. At this point, there is one remaining compilation error.

      To correct it, click the red indicator to the right of the editor's scroll bar. Your cursor highlights MyNewDataBean.

    6. Right-click MyNewDataBean and select Source > Organize Imports.

    7. Save the changes.

  6. Use MyNewDataBean in MyNewJSPTemplate

    In this section, we will modify MyNewJSPTemplate to indicate whether the view was returned by a controller command. If it was returned by a controller command, it should also display the name of that controller command. The JSP template uses JSTL tags for the conditional logic to determine this, based upon values from MyNewDataBean.

    To modify the JSP template:

    1. If the JSP template files are not already open:

      1. In the Project Navigator view, navigate to the Stores > WebContent > ConsumerDirect_name directory.

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

    2. Copy Section 5 from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file. This introduces the following text into the JSP template:

      <!-- SECTION 5 -->
      
      <c:if test="${mndbInstance.calledByControllerCmd}">
        
      <fmt:message key="Example" bundle="${tutorial}" />
      <br />
        
      <fmt:message key="CalledByControllerCmd" bundle="${tutorial}" /> 
           
      <br />
        
      <fmt:message key="CalledByWhichControllerCmd" bundle="${tutorial}" /> 
        
      <b><c:out value="${mndbInstance.callingCommandName}" /></b>
      <br /> 
           
      <br />
      </c:if>
      
      <!-- END OF SECTION 5 -->
      

      This section of code uses the JSTL <if> tag to determine whether to display information about the calling controller command. We use the tag to retrieve translatable text from the resource bundle for the tutorial.

    3. Save the changes.

  7. Test the modified JSP template

    1. 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.

    4. In the Web browser, enter the following URL:

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

      The JSP template displays, as shown in the following screen capture:

    5. Enter the URL to call the view directly, and notice the difference in the information that displays:

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

      The JSP template displays, as shown in the following screen capture:

< Previous | Next >


+

Search Tips   |   Advanced Search