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 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 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">
xmlns:ds="http://deltaspike.apache.org/jsf">
<h:head>
<title></title>
</h:head>
<h:body>
<ds:windowId/>
<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">
xmlns:ds="http://deltaspike.apache.org/jsf">
<h:head>
<title></title>
</h:head>
<h:body>
<ds:windowId/>
<ds:windowId/>
Saved name: #{playerBean.name}
</h:body>
</html> page
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 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
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!
Niciun comentariu :
Trimiteți un comentariu