Tuesday 18 September 2012

wcbase:usebean and JSP best Practices


JSP programming best practice: Use the Commerce-specific tag for bean activation

WebSphere Commerce data beans require activation prior to their use. WebSphere Commerce provides a Commerce-specific version of the useBean tag, <wcbase:useBean>, that performs data bean activation in Java-free manner and is the recommended method of data bean activation in store JSP pages.
As an example, the generic way of activating a category data bean is as follows:
<jsp:useBean id="categoryBean"
class="com.ibm.commerce.catalog.beans.CategoryDataBean">
<%
com.ibm.commerce.beans.DataBeanManager.activate(categoryBean,
request, response); %>
</jsp:useBean>
The recommended way of accomplishing the same task in WebSphere Commerce store pages is as follows:
<wcbase:useBean id="categoryBean"
Note: Data bean activation can fail and cause an exceptional condition. To comply with the JavaServer Pages Specification requirement that no output be written to the output stream when forwarding to other Web assets, including error pages, data beans should be activated at the beginning of the JSP page.


WebSphere Commerce useBean tag

The useBean tag instantiates a WebSphere Commerce data bean and automatically populates it for you. A new programming direction has also been undertaken with our starter stores that now use the JavaServer Pages Standard Tag Library (JSTL) to perform view logic, instead of Java code. At the same time, business logic has been moving into data beans. The combination of these steps allows for less Java code in a JSP page.
The useBean tag takes the following attributes:
id
Required: The ID that the new bean will have. This ID must be a valid Java identifier and must be unique as it is also used to create a new scripting variable for the JSP page. The ID must also be unique to a page.
classname
Required: The class for the data bean that will be instantiated.
scope
The scope in which the bean should be stored such as page, request, session, and application. The default scope is page.
In order to be able to use the new useBean tag library, a JSP page must first be told how to locate the tag. In order to locate and use the new tag, the following line must be located at the top of the JSP file:
<%@ taglib uri="http://commerce.ibm.com/base" prefix="wcbase"%>
This line will make the useBean tag available to the JSP page with the prefix of wcbase.
The following are examples of using the useBean tag:
  • A new OrderDataBean is created and is accessible on the JSP page as orderBean.
    <wcbase:useBean
id="orderBean"classname="com.ibm.commerce.order.beans.OrderDataBean"scope="page"/>
  • A new OrderDataBean is created and is accessible on the JSP page as orderBean. In this case, the useBean tag has a body. Inside of the bean's body tag, the <c:set> is used to set properties on the OrderDataBean.
    <wcbase:useBean id="orderBean" 
      classname="com.ibm.commerce.order.beans.OrderDataBean"
    scope="page">    
      <c:set value="${orderId[0]}" target="${orderBean}"
    property="orderId"/>
    </wcbase:useBean> 


<wcbase:useBean classname="com.ibm.commerce.fulfillment.beans.InventoryDataBean" id="SivaInventory" >
<c:set property="dataBeanKeyCatalogEntryId" target="${SivaInventory}" value="10002" />
<c:set property="dataBeanKeyFulfillmentCenterId" target="${SivaInventory}" value="10001" />
<c:set property="dataBeanKeyStoreId" target="${SivaInventory}" value="10001" />
</wcbase:useBean>

<c:out value="${SivaInventory.quantity}" />




<!-- <wcbase:useBean classname="com.ibm.commerce.catalog.beans.AttributeDataBean" id="attrib">
<c:set property="dataBeanKeyAttributeId" target="${attrib}" value="10004"></c:set>


</wcbase:useBean>
<c:out value="${attrib.name}"/> -->



3 comments:

  1. Hello Team,
    can you please let me know if I can use the below "catch" tag in the following
    case:"no output be written to the output stream when forwarding to other Web assets, including error pages"







    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,
    I want to know whether we can populate OrderDataBean other than orderId.

    ReplyDelete

Don't hesitate to ask query