View Source

h1. Using Window Scope Tutorial

WindowScoped is a custom scope added by ICEfaces 2.0 to JSF 2. The purpose of this scope is to fill the niche between ViewScoped and SessionScoped by providing a slightly longer object lifespan than ViewScoped, but without the overhead of SessionScoped.

This tutorial assumes the reader has an understanding of JSF and ICEfaces and creating and working with projects related to those technologies. The focus is not to teach those basics, but instead to learn more about WindowScoped.

The goal of this tutorial is to create a basic ICEfaces 2.0 project and examine the differences between View, Window, and Session scoped beans. The example will contain two basic pages that are navigated between, and some output information about our various scopes so we can see when a bean is created and destroyed.
\\
\\
----
{panel}Here is the entire list of steps worked through during this tutorial:

# [Make the easyAjaxPush Project|#step1]
# [Add ICEfaces and ICEpush|#step2]
# [Create color.xhtml|#step3]
# [Create MessageBean.java|#step4]
# [Create ColorBean.java|#step5]
# [Create TextModel.java|#step6]
# [Deploy the Application|#step7]
# [Adding Ajax Push|#step8]
** [ColorBean Setup|#step8a]
** [PushRenderer.addCurrentSession|#step8b]
** [PushRenderer.render|#step8c]
# [Re-Deploy the Application|#step9]
* [Tutorial Source Code Downloads|#downloads]
{panel}
----

h3. Development Tools Used

The following tools were used to create the project.
* [Eclipse|http://eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr1] IDE for Java EE Developers - Version Helios
* [Tomcat 7.x|http://tomcat.apache.org/download-70.cgi] Web Server
* [Java 6.x|http://www.oracle.com/technetwork/java/javase/downloads/]
* [ICEfaces 2.0 Beta 2|http://www.icefaces.org/JForum/posts/list/17890.page]

h3. {anchor:step1}1. Make the {{windowScope}} Project

* Using Eclipse create a new Dynamic Web Project called {{windowScope}}
** Target runtime: Apache Tomcat v7.0
** Dynamic web module version: 3.0
** Configuration: JavaServer Faces v2.0 Project (Mojarra)

h3. {anchor:step2}2. Add ICEfaces

Add the {{icefaces.jar}} to your project from the ICEfaces 2 bundle. This can be added to the project through a custom User Library or by putting it into {{windowScope/WEB-INF/lib/}}. The approach doesn't matter as long as the jar is included in the deployed {{war}} file.

h3. {anchor:step3}3. Create 3 Beans with Different Scopes

h4. {anchor:step3a}3a. Create {{ViewBean.java}}

Create a new Java class file called {{ViewBean}} in the package org.icefaces.tutorial.windowscope.beans and paste the code below:

{code:title=ViewBean.java}
package org.icefaces.tutorial.windowscope.beans;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="viewBean")
@ViewScoped
public class ViewBean implements Serializable {
private Timestamp created;

public ViewBean() {
created = new Timestamp(System.currentTimeMillis());
}

public Timestamp getCreated() {
return created;
}

public void setCreated(Timestamp created) {
this.created = created;
}
}
{code}

h4. {anchor:step3b}3b. Create {{SessionBean.java}}

Create a new Java class file called {{SessionBean}} in the package org.icefaces.tutorial.windowscope.beans and paste the code below:

{code:title=SessionBean.java}
package org.icefaces.tutorial.windowscope.beans;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
private Timestamp created;

public SessionBean() {
created = new Timestamp(System.currentTimeMillis());
}

public Timestamp getCreated() {
return created;
}

public void setCreated(Timestamp created) {
this.created = created;
}
}
{code}

h4. {anchor:step3c}3c. Create {{WindowBean.java}}

Create a new Java class file called {{WindowBean}} in the package org.icefaces.tutorial.windowscope.beans and paste the code below:

{code:title=WindowBean.java}
package org.icefaces.tutorial.windowscope.beans;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean(name="windowBean")
@CustomScoped(value = "#{window}")
public class WindowBean implements Serializable {
private Timestamp created;

public WindowBean() {
created = new Timestamp(System.currentTimeMillis());
}

public Timestamp getCreated() {
return created;
}

public void setCreated(Timestamp created) {
this.created = created;
}
}
{code}