JSP pages and data beans

A data bean is a Java bean that is used within a JSP page to provide dynamic content. A data bean normally provides a simple representation of an HCL Commerce entity bean. The data bean encapsulates properties that can be retrieved from or set within the entity bean. As such, the data bean simplifies the task of incorporating dynamic data into JSP pages.

A data bean is activated in one of the following ways:

  • by means of the wcbase:useBean tag (recommended)
  • by means of the following call:
    
    com.ibm.commerce.beans.DataBeanManager.activate(data_bean, request, response)
    

    where data_bean is the data bean to be activated, request is an HTTPServletRequest object, and response is an HTTPServletResponse object.

Store developers should consider properties of the store and globalization issues when developing JSP pages.

Data beans security consideration

A particular coding practice for the use of data beans minimizes the chance for malicious users to access your database in an unauthorized manner. Insert, select, update and delete parts of SQL statements should be created at development time. Use parameter inserts to gather runtime input information.

An example of using a parameter insert to collect runtime input information follows:


select * from Order where owner =?

In contrast, you should avoid using input strings as a way to compose the SQL statement. An example of using an input string follows:


select * from Order where owner = "input_string"

Types of data beans

A data bean is a Java bean that is mainly used to provide dynamic data in JSP pages. There are two types of data beans: smart data beans and command data beans.

A smart data bean uses a lazy fetch method to retrieve its own data. This type of data bean can provide better performance in situations where not all data from the access bean is required, since it retrieves data only as required. Smart data beans that require access to the database should extend from the access bean for the corresponding entity bean and implement the com.ibm.commerce.SmartDataBean interface. For example, the ProductDataBean data bean extends the ProductAccessBean access bean, which corresponds to the Product entity bean.

Some smart data beans do not require database access. For example, the PropertyResource smart data bean retrieves data from a resource bundle, rather than the database. When database access is not required, the smart data bean should extend the SmartDataBeanImpl class.

A command data bean relies on a command to retrieve its data and is a more lightweight data bean. The command retrieves all attributes for the data bean at once, regardless of whether the JSP page requires them. As a result, for JSP pages that use only a selection of attributes from the data bean, a command data bean may be costly in terms of performance time. While access control can be enforced on a data bean level when using the smart data bean, this is not true for command data bean. Only use command data bean if using a smart data bean is impractical.

Command data beans can also extend from their corresponding access beans and implement the com.ibm.commerce.CommandDataBean interface.

Data bean interfaces

Data beans implement one or all of the following Java interfaces:

  • com.ibm.commerce.SmartDataBean.
  • com.ibm.commerce.CommandDataBean
  • com.ibm.commerce.InputDataBean (optional)

Each Java interface describes the source of data from which a data bean is populated. By implementing multiple interfaces, the data bean can access data from a variety of sources. More information about each of the interfaces is provided below.

SmartDataBean interface

A data bean implementing the SmartDataBean interface can retrieve its own data, without an associated data bean command. A smart data bean usually extends from the access bean of a corresponding entity bean. When a smart data bean is activated, the data bean manager invokes the data bean's populate method. Using the populate method, the data bean can retrieve all attributes, except attributes from associated objects. For example, if the data bean extends from an access bean class for an entity bean, the data bean invokes the refreshCopyHelper method. All the attributes from the corresponding entity bean are populated into the smart data bean automatically. However, if the entity bean has associated objects, the attributes from those objects are not retrieved. The main advantages of using smart data beans are:

  • Implementation is simple and there is no need to write a data bean command.
  • When new fields are added to the entity bean, changes in the data bean are not required. After the entity bean has been modified, the access bean must be regenerated (using the tools in WebSphere Commerce Developer). As soon as the access bean has been regenerated, all the new attributes are automatically available to the smart data bean.
  • Entity beans often contain attributes representing associated objects. For performance reasons, the smart data bean does not automatically retrieve these attributes. Instead, it is preferable to delay retrieval of these attributes until they are required, as shown in the following diagram:

    Click the D link for description.

    For more information about implementing a lazy fetch retrieval, refer to Lazy fetch data retrieval.

CommandDataBean interface

A data bean implementing the CommandDataBean interface retrieves data from a data bean command. A data bean of this type is a lightweight object; it relies on a data bean command to populate its data. The data bean must implement the getCommandInterfaceName() method (as defined by the com.ibm.commerce.CommandDataBean interface) which returns the interface name of the data bean command.

InputDataBean interface

A data bean implementing the InputDataBean interface retrieves data from the URL parameters or attributes set by the view.

Attributes defined in this interface can be used as primary key fields to fetch additional data. When a JSP page is invoked, the generated JSP servlet code populates all the attributes that match the URL parameters, and then activates the data bean by passing the data bean to the data bean manager. The data bean manager then invokes the data bean's setRequestProperties() method (as defined by the com.ibm.commerce.InputDataBean interface) to pass all the attributes set by the view. It should be noted that the following code is required in order for the data bean to be activated:


com.ibm.commerce.beans.DataBeanManager.activate(data_bean, request, response)

where data_bean is the data bean to be activated, request is an HTTPServletRequest object, and response is an HTTPServletResponse object.

Data bean activation

Data beans can be activated using either the activate or silentActivate methods that are found in the com.ibm.commerce.beans.DataBeanManager class. The activate method is a full activation method in which the activation event is only successful if all attributes are available. If even one attribute is unavailable, an exception is thrown for the whole activation process.

The silentActivate method does not throw exceptions when individual attributes are unavailable.

Invoking controller commands from within a JSP page

Do not invoke controller commands from within JSP pages.

Lazy fetch data retrieval

When a data bean is activated, it can be populated by a data bean command or by the data bean's populate() method. The attributes that are retrieved come from the data bean's corresponding entity bean. An entity bean may also have associated objects, which themselves, have a number of attributes.

If, upon activation, the attributes from all the associated objects were automatically retrieved, a performance problem may be encountered. Performance may degrade as the number of associated objects increase.

Consider a product data bean that contains a large number of cross-sell, up-sell or accessory products (associated objects). It is possible to populate all associated objects as soon as the product data bean is activated. However, populating in this manner may require multiple database queries. If not all attributes are required by the page, multiple database queries may be inefficient.

In general, not all attributes are required for a page, therefore, a better design pattern is to perform a lazy fetch as illustrated below:


getCrossSellProducts () {
         if (crossSellDataBeans == null){
                 crossSellDataBeans= getCrossSellDataBeans();    
        }
         return crossSellDataBean;
          }