I build a short page with JSF-Compnents which displays and increments a value from a @ConversationScoped Bean. This page is able to end the Conversation and is getting a new Bean after ending the old Conversation. Here is what it looks like:
<!DOCTYPE html>
<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.org/ui">
<h:head />
<h:body>
<h:form>
<h:outputText id="i" value="#{test.i}" />
<h:commandButton value="increment"
actionListener="#{test.increment}"
update="i cid" />
<h:outputText id="cid"
value="#{javax.enterprise.context.conversation.id}" />
<h:commandButton value="end"
actionListener="#{test.endConversation}"
update="i cid" />
</h:form>
</h:body>
</html>
The code for the Bean is quite simple:
package de.burghard.britzke.test.beans;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@ConversationScoped
public class Test implements Serializable {
@Inject Conversation conversation;
private int i;
@PostConstruct
public void init() {
conversation.begin();
System.out.println("init conversation"+conversation.getId());
}
public int getI() { return i; }
public void setI(int i) { this.i = i; }
public void increment() { i++;System.out.println(i); }
public void endConversation() {
System.out.println("ending conversation "+conversation.getId());
conversation.end();
}
}
Everything works well, when using the standard h:commandButton Components. But using the Primefaces Components p:commandButton then every click on the 'increment' Button grabs a new Bean instance because the cid-Parameter is not passed to the Server. It has been stated that this is not a primefaces issue. But why is it working with the standard components and not with the primefaces ones? I have been able to pass the cid-Parameter by explicitely embedding a f:param component into the commandButton Components but after destroying the Conversation the parameter is sent with no value producing an error. But it should even work without the f:param component.
Is there a short tutorial how to work with Primefaces and Conversation scoped beans (without explicitely passing the cid-Parameter)?
The problem with the f:param
in case of ended conversation has been that the param should not be passed with invalid values (null) for the parameter cid
. So correcting this with the following code worked well:
<!DOCTYPE html>
<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.org/ui">
<h:head />
<h:body>
<h:form id="form">
<h:outputText value="#{test.i}" />
<p:commandButton value="increment"
actionListener="#{test.increment}"
update="@parent">
<f:param
name="#{not empty javax.enterprise.context.conversation.id?'cid':''}"
value="#{javax.enterprise.context.conversation.id}" />
</p:commandButton>
<h:outputText
value="#{javax.enterprise.context.conversation.id}" />
<p:commandButton value="end" actionListener="#{test.endConversation}"
update="@parent">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}" />
</p:commandButton>
</h:form>
</h:body>
</html>
But I would like the primefaces components to do the conversation handling without the need for page programmers to explicitly pass a f:param
. They should behave like standard JSF (MyFaces) components do.