My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

joi, 12 noiembrie 2015

JSF Scopes Tutorial - DeltaSpike Window Scope

The DeltaSpike window scope is like a JSF session scope per window
If you need an out of the box window-scope like a session scope per window, then this post is for you. The DeltaSpikeJSF module, comes with @WindowScoped - "the data is bound to a window/tab and it is not shared between windows (like the session scope does). Usually you need the window-scope instead of the session-scope. There are not a lot of use-cases which need shared data between windows.".

Brief overview of using window scope with JSF:

So, after you add DeltaSpike in your project, you can try this simple example (the component <ds:windowId> (xmlns:ds="http://deltaspike.apache.org/jsf") is required to enable the full control of the DeltaSpike window handling; basically, it will be useful to avoid exceptions that indicate that the windowId is not set in theWindowContext):

·         a simple index.xhtml page
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ds="http://deltaspike.apache.org/jsf">
 <h:head>
  <title></title>
 </h:head>
 <h:body> 
  <ds:windowId/> 
  <h:form>
   Name:
   <h:inputText value="#{playerBean.name}"/>
   <h:commandButton value="Save" action="done"/>           
  </h:form>
 </h:body>
</html>

·         a simple done.xhtml page
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ds="http://deltaspike.apache.org/jsf">
 <h:head>
  <title></title>
 </h:head>
 <h:body> 
  <ds:windowId/>         
  Saved name: #{playerBean.name}       
 </h:body>
</html> page

·         and, a simple bean annotated with DeltaSpike@WindowScoped
package beansds;

import java.io.Serializable;
import javax.inject.Named;
import org.apache.deltaspike.core.api.scope.WindowScoped;

@Named
@WindowScoped  // for comparison, you can switch to SessionScoped and test again
public class PlayerBean implements Serializable{
   
 private String name = "-";

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }           
}

First test (using DeltaSpike @WindowScoped):
·         first tab - initial request
·         first tab - typed name Rafael Nadal, clicked Save button (so, postback)
·         open second tab with URL, http://localhost:8080/DeltaSpikeWindowScope/faces/index.xhtml

Notice that the "-" appears instead of Rafael Nadal text. This is the effect of @WindowScoped.

Second test (using CDI, @SessionScoped):
·         first tab - initial request
·         first tab - typed name Rafael Nadal, clicked Save button (so, postback)
·         open second tab with URL, http://localhost:8080/DeltaSpikeWindowScope/faces/index.xhtml

Notice that the Rafael Nadal text appears. This is the effect of @SessionScoped. The session scoped information is shared between tabs/windows!

The complete code is available here.

See you in the next post about CDI conversation scope.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors