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

Why do you need Spring Cloud Config server?

Last month I wrote a primer on concepts around 12 factor app. Before getting into the details of the Spring Cloud Config Server, I must refresh on the principle #3 from the list presented in that post.

3 – ConfigurationStore config in the environments
Configuration information must be separate from the source code. This may seem so obvious, but often we are guilty of leaving critical configuration parameters in the scattered in the code. Instead, applications should have environment specific configuration files. The sensitive information like database password or API key should be stored in these environment configuration files in encrypted format.
 The key takeaways from this postulate for a cloud-native microservices application are:
Do not store configuration as part of the deployable unit (in the case of lead microservice - inside the jar or war if you are still deploying war like the good old days). Instead, store it in an external location and make it easily accessible during run-…

Changing the root web application in Tomcat 6

These days I am very busy developing a large and complex web application, which is nearing its first release. This web based product runs on Tomcat 6 and will be deployed on http://www.mydomain.com
But if I deploy my web application on Tomcat 6, I need to specify the context root. Well this is no good, why will the users of this website be willing to remember the context root. What if the context root changes. In other words the users would not be interest in typing the following - http://www.mydomain.com/myappcontext to visit this site.
Hence it is necessary that I change the default / root web application of Tomcat and point it to my application. I wanted to try and test this on localhost first. As always I Googled and found a number of posts and none seem to work. I also read the documentation it was helpful in understanding the concepts but was not effective as I could not get it working. Finally after few hours of trial and error one of my colleague Joyeeta Majumdar found the so…

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.2Listing 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</scope>