Primefaces Components and the @ConversationScoped Beans

来源:互联网 发布:css 实战手册 知乎 编辑:程序博客网 时间:2024/06/18 07:34

http://forum.primefaces.org/viewtopic.php?f=3&t=23679


关于 jsf2.2的一个问题。

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:

Code: Select all
<!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:
Code: Select all
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.

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'. Changing the Parameter name to a different one if no conversation is present is a workaround:

Code: Select all
    <!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>


Unfortunally the <f:param> tag is not supported within <p:ajax> components. This leads to a new conversation for every ajax request. Thus @ConversationScoped beans are not possible with ajax-enabled Primefaces applications today - right?

0 0
原创粉丝点击