HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Creating a Pet Store Application"
Vol. 9, Issue 11, p. 32

	



Listing 1

public class CartBean extends BaseBean {
  private Cart cart;

  ...
  public String addItemAction() {
    String itemId =  
      FacesUtils.getRequestParameter(ITEM_ID_PARAMETER_NAME);
    try {
      Item item = 
        this.getServiceLocator().getCatalogService().getItem(itemId);
      this.cart.addItem(item);
    } catch(MyPetStoreException me) {
      String msg = "Could not add item to cart ";
      FacesUtils.addErrorMessage(msg + ": Internal Error");
      return NavigationResults.FAILURE;
    }
    return NavigationResults.SUCCESS;
  }

  ...
  public Cart getCart() {
    return this.cart;
  }
}

Listing 2

<managed-bean>
  <description>Backing bean for the shopping cart.</description>
  <managed-bean-name>cartBean</managed-bean-name>
  <managed-bean-class>
    mypetstore.view.bean.CartBean
  </managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
    <property-name>serviceLocator</property-name>
    <value>#{serviceLocatorBean}</value>
  </managed-property>
</managed-bean>

Listing 3

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<f:subview id="cart">
<h:form id="cartForm">
<h:outputText value="Your Shopping Cart is Empty" 
  styleClass="title"     rendered="#{cartBean.numberOfItems <= 0}"/>
<h:outputText value="Shopping Cart" styleClass="title"
  rendered="#{cartBean.numberOfItems > 0}"/>
<h:panelGrid columns="1" styleClass="box"
  rendered="#{cartBean.numberOfItems > 0}">
<h:dataTable id="cartTable" value="#{cartBean.cartItemList}"
  var="cartItem" styleClass="standard"
  columnClasses="cartColumn1, cartColumn2, cartColumn3, cartColumn4">
  <h:column>
    <h:outputLink  value="item.jsf?itemId=#{cartItem.item.itemId}">
      <h:outputText value="#{cartItem.item.attribute1}" />
      <h:outputText value="#{cartItem.item.productName}"/>
    </h:outputLink>
  </h:column>

  ...
  <h:column>
    <h:inputText value="#{cartItem.quantity}" size="5"/>
  </h:column>

  ...
</h:dataTable>

...
  <h:commandButton value="Update Cart"
    action="#{cartBean.updateAction}"/>

...
</h:panelGrid>
<h:panelGrid styleClass="standard" columnClasses="rightAlign"
rendered="#{cartBean.numberOfItems > 0}">
  <h:outputLink  value="createOrder.jsf">
    <h:outputText value="Check Out" />
  </h:outputLink>
</h:panelGrid>
</h:form>
</f:subview>

Listing 4

<servlet>
  <servlet-name>Tiles Servlet</servlet-name>
  <servlet-class>
      org.apache.struts.tiles.TilesServlet
  </servlet-class>
  <init-param>
    <param-name>definitions-config</param-name>
    <param-value>/WEB-INF/tiles.xml</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
</servlet>

Listing 5

<f:view>

    ...
  <table width="100%" height="100%" border="0" cellpadding="5"
    cellspacing="0">
   <tr height="100" valign="top">
    <td colspan="3">
      <tiles:insert attribute="header" flush="false"/>
    </td>
   </tr>
   <tr valign="top">

     ...
     <td width="60%" valign="top">
       <tiles:insert attribute="body" flush="false"/>
     </td>

     ...
   </tr>
  </table>
</f:view>

Listing 6

<tiles-definitions>
  <definition name=".mainLayout" path="/layout.jsp">
    <put name="header" value="/tiles/header.jsp"/>
    <put name="sider" value="/tiles/sider.jsp"/>
    <put name="footer" value="/tiles/footer.jsp"/>
    <put name="mylist" value="/tiles/mylist.jsp"/>
    <put name="advicebanner" value="/tiles/adviceBanner.jsp"/>
  </definition>
</tiles-definitions>

Listing 7

public class Cart {
  private final Map itemMap = 
    Collections.synchronizedMap(new HashMap());
  private final List itemList = 
    Collections.synchronizedList(new ArrayList());

  ...
  public void addItem(Item item) {
    CartItem cartItem = (CartItem)itemMap.get(item.getItemId());
    if (cartItem == null) {
	cartItem = new CartItem();
	cartItem.setItem(item);
	cartItem.setQuantity(1);
	itemMap.put(item.getItemId(), cartItem);
	itemList.add(cartItem);
    }
  }


  ...
  public double getSubTotal() {
    double subTotal = 0;
    Iterator items = this.getCartItemList().iterator();
    while (items.hasNext()) {
      CartItem cartItem = (CartItem) items.next();
	subTotal += cartItem.getTotalPrice();
    }
    return subTotal;
  }
}

Listing 8

<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager"
 class="org.springframework.orm.hibernate.HibernateTransactionManager">
  <property name="sessionFactory">
    <ref local="sessionFactory"/>
  </property>
</bean>

<!-- Catalog Service Definition -->
<bean id="catalogServiceTarget"
  class="mypetstore.model.service.impl.CatalogServiceImpl">
  <property name="catalogDao"><ref local="catalogDao"/></property>
</bean>

<!-- Transactional proxy for the Catalog Service -->
<bean id="catalogService" class=
  "org.springframework.transaction.interceptor.
  TransactionProxyFactoryBean">
  <property name="transactionManager">
    <ref local="transactionManager"/>
  </property>
  <property name="target">
    <ref local="catalogServiceTarget"/>
  </property>
  <property name="transactionAttributes">
    <props>
	<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
	<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
  </property>
</bean>

Listing 9

<bean id="sessionFactory" 
  class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  <property name="mappingResources">
    <list>
      <value>mypetstore/model/businessobject/Item.hbm.xml</value>

      ...
    </list>
  </property>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">
        net.sf.hibernate.dialect.MySQLDialect
      </prop>

      ...
    </props>
  </property>
  <property name="dataSource"><ref bean="dataSource"/></property>
</bean>

Listing 10

<hibernate-mapping package="mypetstore.model.businessobject">
  <class name="Item" table="ITEM">
    <id name="itemId" column="ITEMID" unsaved-value="null">
      <generator class="assigned"/>
    </id>
    <property name="listPrice" column="LISTPRICE"/>

    ...
    <many-to-one name="product" class="Product"
      column="PRODUCTID" cascade="none"/>

    ...
  </class>
</hibernate-mapping>
  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.