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

luni, 23 februarie 2015

DeltaSpike - Use window-scope (@WindowScoped) as a session per window scope

If you need an out of the box window-scope like a session scope per window, then this post is for you. The DeltaSpike, JSF 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 the WindowContext):

·         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!

Complete code on GitHub (tested under GlassFish 4.1 and WildFly 8.2 Final)

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors