Skip to main content

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.

Comments

Popular posts from this blog

CKEDITOR 3.x - Simplest Ajax Submit Plugin

  I have assumed that you have downloaded and got started with CKEDITOR. Step 1 – The html file is shown below: <html> <head> <title>Writer</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <style> .cke_contents { height: 400px !important; } </style> </head> <body> <form action="sample_posteddata.php" method="post"> <textarea id="editor" > </textarea> <script type="text/javascript"> //<![CDATA[ CKEDITOR.replace( 'editor', { fullPage : true, uiColor : '#9AB8F3', toolbar : 'MyToolbar' }); //]]> </script> </form> </body> </html> Note that the jquery js...

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s...

How to implement Cache Aside Pattern with Spring?

Problem You want to boost application performance by loading data from a cache and prevent the network trip to the persistent store (and also the query execution). This can be achieved by loading data from a cache. However, you want to load data on demand or lazily. Also, you want the application to control the cache data management – loading, eviction, and retrieval. Forces Improve performance by loading data from cache lazily. Application code controls cache data management. The underlying caching system does not provide read-through, write-through/write-behind strategies (strange really ??). Solution Use cache aside design pattern to solve the problems outlined above. This is also one of many caching patterns/strategies. I believe it is named in this because aside from managing the data store, application code is responsible for managing the cache also. Let's now try to understand how this caching technique works and then explore how it solves the proble...