Tutorials > Program model > Create new business logic
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:
- The minimum requirements for code contained in a controller command
- How to create the new controller command interface and implementation class
- How to configure a controller command to return a view
- How to register a controller command in the command registry
- How to set up access control for a controller command
In general, creating a new controller command involves the following steps:
- Register the new command in the command registry.
- Create an interface for the command.
- Create an implementation class for the command.
- Create and loading access control policies for the command.
- Test the command.
Procedure
- 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.
- Start the test environment.
- Register MyNewControllerCmd in the CMDREG table:
- Open a browser and type the following URL: http://localhost/webapp/wcs/admin/servlet/db.jsp
- 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.
- Click Submit Query to run the SQL statement.
- Register the interface in the struts-config-ext.xml file:
- The file struts-config-ext.xml should still be open for editing in the Struts Configuration File editor.
- In the Action Mappings tab, in the Action Mappings section, click Add. Replace the default path value /action1 and enter /MyNewControllerCmd.
- In the Action Mapping attributes section, in the Type field, enter com.ibm.commerce.struts.BaseAction.
- 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.
- Save the changes and close the file.
- Update the registry component named Struts Configuration:
- Open the Administration Console and select Site on the Administration Console Site/Store Selection page.
- From the Configuration menu, click Registry. A list of registry components for the site displays.
- 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."
- Click Refresh to reload the Registry window. When updating is complete, the status column reads "Updated."
- 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:
- In the Enterprise Explorer view, navigate to the Other Projects > WebSphereCommerceServerExtensionsLogic project.
- Navigate to the src directory and expand the com.ibm.commerce.sample.commands package.
- Double-click the MyNewControllerCmd.java interface file to open it for editing.
- 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 sectionThis section of code specifies that by default, the interface will use the MyNewControllerCmdImpl implementation class.
- Save the change to the interface.
- 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:
- Double-click the MyNewControllerCmdImpl.java class to open it.
- In the Outline view, select the performExecute method to view its source code.
- 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.
- 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.
- Save the changes.
- 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:
- Stop the test environment.
- At a command prompt, navigate to the following directory: WCDE_INSTALL/\bin
- Run the acpload command, which has the following form:
acpload db_host_name db_name db_user db_password inputXMLFile schema_nameWhere:
For example,
- 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.
acpload Demo_Dev dbuser dbuserpwd MyNewControllerCmdACPolicy.xml DBUSER
- Run the acpload command... acpload MyNewControllerCmdACPolicy.xml
- 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.
- 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:
- Start the test environment.
- Navigate to the Stores > WebContent > ConsumerDirect_name directory.
- Select the file index.jsp and from its pop-up menu select Run on Server. The store home page displays in the Web browser.
- In the Web browser enter the following URL:
http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmdAfter a few seconds, the new JSP page displays, as shown in the following screen capture: