Saturday, November 6, 2010

JSF 2.0 – First Managed Bean

The window shop, landing page is not complete. I have not shown the new product arrivals for this month in the center of the screen. In order to do that we need a managed bean which will retrieve data from the database. Later on we will integrate EJB 3.x stateless session beans to fetch the data for us. For now our managed bean will cook up the data to set the ball rolling. In this process we also discover the first domain object – Product. Going forward this will be turned into a JPA entity for CRUD operations.
Here is how the domain object looks like:
/**
* 
*/
package com.windowshop.domain.entities;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
* @author Dhrubo
* 
*/
public class Product {
private int productId;
private String productName;
private Date createdDate;
private String thumnailLocation;
private String model;
private double price;
private String currency;

public Product() {}

public Product(int productId, String productName, Date createdDate,
String thumnailLocation, String model, double price, String currency) {
super();
this.productId = productId;
this.productName = productName;
this.createdDate = createdDate;
this.thumnailLocation = thumnailLocation;
this.model = model;
this.price = price;
this.currency = currency;
}

public int getProductId() {
return productId;
}

public void setProductId(int productId) {
this.productId = productId;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public Date getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

public String getThumnailLocation() {
return thumnailLocation;
}

public void setThumnailLocation(String thumnailLocation) {
this.thumnailLocation = thumnailLocation;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getCurrency() {
return currency;
}

public void setCurrency(String currency) {
this.currency = currency;
}

public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}


The managed bean is shown in the listing below
/**
* 
*/
package com.windowshop.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;

import com.windowshop.domain.entities.Product;

/**
* @author Dhrubo
*
*/
@ManagedBean(name="productController")
public class ProductController {

public List getProductsByMonth(){

//TODO - Remove to session bean and JPA to fetch from db
List products = new ArrayList();
Product product = null;
for(int i = 1 ; i <= 34; i++) {
product = new Product(i,"Abc",new Date(),"","",i+1.20d,"Rs");
products.add(product);
}

return products;
}
}


Note that with JSF 2 you can do away with faces-config.xml. This file is just optional. Instead you can turn a POJO into a managed bean with the ManagedBean annotation. One attribute is the name of the managed bean. In this case the managed bean is named “productController”. In case you do not want to name the managed bean it will by default be named with the class name part of the fully qualified class name. There are other attributes which I will take up as and when we come across the need for them. This managed bean will have a default request scope. I will discuss different scopes later. For the moment just know that for managed beans the default scope is request.

Now you will want to use this managed bean in JSF pages. The listing below (newproducts.xhtml) shows the new JSF which fills the missing piece.

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">

<f:view contentType="text/html">
<h:form>
<h:outputText value="New Products for November 2010"  style="font-size:.9em"/>

<p:dataGrid var="product" value="#{productController.productsByMonth}" columns="4"
rows="12" paginator="true" effect="true"
paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} 
{PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="8,12,16">

<p:column>
<p:panel header="#{product.model}" style="text-align:center">
<h:panelGrid columns="1" style="width:100%">
<!--
<p:graphicImage value="/images/cars/#{car.manufacturer}.jpg"/> 
-->
<h:outputText value="#{product.productName}" />

<p:commandLink title="View Detail" action="#">

</p:commandLink>
</h:panelGrid>
</p:panel>
</p:column>

</p:dataGrid>
</h:form>
</f:view>
</html>




The layout and other pages have been updated and they are now in the SVN.

No comments:

Post a Comment