Tutorials > Program model > Create new business logic

< Previous | Next >


Create a new controller command

In this step, we will create a new controller command, called MyNewControllerCmd. Initially, this command returns only the MyNewView view.

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

In general, creating a new controller command involves the following steps:

  1. Register the new command in the command registry.

  2. Create an interface for the command.

  3. Create an implementation class for the command.

  4. Create and loading access control policies for the command.

  5. Test the command.


Procedure

  1. Register MyNewControllerCmd

    In this section of the tutorial, we will create a new controller command called MyNewControllerCmd. This command must be registered in the command registry. In particular, the interface must be registered in the struts-config-ext.xml file, and the association between the interface and its implementation class gets registered in the CMDREG table.

    Tip: In the following steps, the insert statement into the CMDREG table is not absolutely necessary. By default, the interface uses the default implementation, and as such, this association between the interface and implementation class does not really need to be specified in the command registry. It is included here for the purpose of completeness and good programming practice.

    1. Start the test environment.

    2. Register MyNewControllerCmd in the CMDREG table:

      1. Open a browser and type the following URL: http://localhost/webapp/wcs/admin/servlet/db.jsp

      2. Enter the following SQL statement:

        insert into CMDREG (STOREENT_ID, INTERFACENAME, DESCRIPTION, CLASSNAME, TARGET)
           values (CD_storeent_ID,
                   'com.ibm.commerce.sample.commands.MyNewControllerCmd',                  
                   'This is a new controller command for the business logic tutorial.',                   
                   'com.ibm.commerce.sample.commands.MyNewControllerCmdImpl','Local');
        

        where CD_storeent_ID is the unique identifier for the store.

      3. Click Submit Query to run the SQL statement.

    3. Register the interface in the struts-config-ext.xml file:

      1. The file struts-config-ext.xml should still be open for editing in the Struts Configuration File editor.

      2. In the Action Mappings tab, in the Action Mappings section, click Add. Replace the default path value /action1 and enter /MyNewControllerCmd.

      3. In the Action Mapping attributes section, in the Type field, enter com.ibm.commerce.struts.BaseAction.

      4. In the Action Mapping attributes section, in the Parameter field, enter com.ibm.commerce.sample.commands.MyNewControllerCmd. The parameter field contains the interface name of the business logic to execute.

      5. Save the changes and close the file.

    4. Update the registry component named Struts Configuration:

      1. Open the Administration Console and select Site on the Administration Console Site/Store Selection page.

      2. From the Configuration menu, click Registry. A list of registry components for the site displays.

      3. Select the check box for the Struts Configuration registry component and click Update. The Registry window reloads listing the status for the selected components as "Updating."

      4. Click Refresh to reload the Registry window. When updating is complete, the status column reads "Updated."

  2. Create the MyNewControllerCmd interface

    According to the WebSphere Commerce programming model, all new controller commands must have an interface, as well as an implementation class. For this tutorial, a base for the interface is provided in the sample code. The base is split into sections that are commented out. As you progress through the tutorial, you uncomment various sections of the code.

    To create the MyNewControllerCmd interface:

    1. In the Enterprise Explorer view, navigate to the Other Projects > WebSphereCommerceServerExtensionsLogic project.

    2. Navigate to the src directory and expand the com.ibm.commerce.sample.commands package.

    3. Double-click the MyNewControllerCmd.java interface file to open it for editing.

    4. Uncomment Section 1 by deleting the "/*" before the section and "*/" after the section and introducing the following code into the interface:

      /// Section //
      
      // set default command implement class
      
        static final String defaultCommandClassName = 
           "com.ibm.commerce.sample.commands.MyNewControllerCmdImpl";
          
      
      /// End of section
      

      This section of code specifies that by default, the interface will use the MyNewControllerCmdImpl implementation class.

    5. Save the change to the interface.

  3. Create the MyNewControllerCmdImpl implementation class

    Once the interface is created, the next step is to create the implementation class for the command.

    To create the MyNewControllerCmdImpl implementation class:

    1. Double-click the MyNewControllerCmdImpl.java class to open it.

    2. In the Outline view, select the performExecute method to view its source code.

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

      /// Section //////////
      
          /// create a new TypedProperties for output purpose.  
          
        TypedProperty rspProp = new TypedProperty();
      
      /// End of section //////// 
      

      This code creates a new TypedProperty object that is used to hold the command's response properties. TypedProperty is a hash table that WebSphere Commerce uses to hold data from the Request Object.

    4. In the source code for the performExecute method, uncomment Section 5. This action introduces the following code into the method:

      /// Section ///////////
      
          /// see how controller command call a JSP
         
        rspProp.put(ECConstants.EC_VIEWTASKNAME, "MyNewView");  
        setResponseProperties(rspProp);
      
      /// End of section//////////
      

      This section of code accomplishes two main tasks. First, it is a requirement of the WebSphere Commerce programming model that all controller commands return a view. In this section, it specifies that the view to be returned is the MyNewView that you previously created. Additionally, it sets the command's response properties to be the new rspProp object.

    5. Save the changes.

  4. Create and loading access control policies for the command

    You must specify access control policies for the new command. In this case, the command-level access control policy specifies that all users are allowed to execute the command. Note that this type of access control policy is acceptable for the development environment, but it might not be suitable for other circumstances.

    The access control policy is defined by the MyNewControllerCmdACPolicy.xml file, which you placed into the following directory, as part of the tutorial's prerequisite steps: WC_EAR\xml\policies\xml.

    To load the new policy:

    1. Stop the test environment.

    2. At a command prompt, navigate to the following directory: WCDE_INSTALL/\bin

    3. Run the acpload command, which has the following form:

      acpload db_host_name db_name db_user db_password inputXMLFile schema_name
      

      Where:

      db_host_name

      is the hostname of the machine on which the development database runs. This parameter is only required for remote databases.

      db_name

      is the name of the development database

      db_user

      is the name of the database user

      db_password

      is the password for the database user

      inputXMLFile

      is the XML file containing the access control policy specification. In this case, specify MyNewControllerCmdACPolicy.xml.

      schema_name

      • The name of the database user who created the database, in uppercase.

      • The name of the user who owns the tables.

      • Do not specify this parameter. This parameter is not applicable.

      For example,

      acpload Demo_Dev dbuser dbuserpwd MyNewControllerCmdACPolicy.xml DBUSER
      

    4. Run the acpload command... acpload MyNewControllerCmdACPolicy.xml

    5. Navigate to the WCDE_INSTALL/\logs directory. Inspect the acpload.log and messages.txt files to ensure that the access control policy loaded successfully. The messages.txt file might not exist if the load completed successfully. Also check if policy files were created successfully in the WC_EAR\xml\policies\xml directory: MyNewControllerCmdACPolicy_idres.xml and MyNewControllerCmdACPolicy_xmltrans.xml. These two files are created as part of a successful idresgen utility process. If any other error files are generated in this directory, this indicates that there was a problem.

  5. Test MyNewControllerCmd

    Now that the interface, implementation class, command registration, and access control information have all been created, you can test the new controller command.

    To test the new code:

    1. Start the test environment.

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

    3. Select the file index.jsp and from its pop-up menu select 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
      

      After a few seconds, the new JSP page displays, as shown in the following screen capture:

< Previous | Next >


+

Search Tips   |   Advanced Search