4 Replies Latest reply on Oct 26, 2007 8:50 AM by balteo

    Working example of a4j:push [Please help Ilya]

    balteo

      Hello,

      I am trying to construct a working example of a4j:push that I might post on the wiki/cookbook and here what I came up with:

      The javabean (REQUEST SCOPE)

      package pack;
      
      import java.util.Date;
      import java.util.EventListener;
      import java.util.EventObject;
      import org.ajax4jsf.event.PushEventListener;
      
      
      /**
       *
       * @author Administrator
       */
      public class TheBean {
      
       /** Creates a new instance of TheBean */
       public TheBean() {
      
       }
       private Date date;
       private PushEventListener listener = new MyPushEventListener();
      
       public void addListener(EventListener listener) {
       synchronized (listener) {
       if (this.listener != listener) {
       this.listener = (PushEventListener) listener;
       }
       }
       }
      
       public Date getDate() {
       return new Date();
       }
      
       public void push() {
       synchronized (this.listener) {
       this.listener.onEvent(new EventObject(this));
       }
       }
      
      }
      class MyPushEventListener implements PushEventListener {
      
       public void onEvent(EventObject evt) {
       System.out.println(evt.getSource());
       //DO SOME ACTION IN HERE
       }
      
      
      
      }
      


      and the fist jsp: receiver

       <f:view>
       <a4j:status startText="in progress" stopText="done"/>
      
       <a4j:form>
       <a4j:push reRender="msg" eventProducer="#{theBean.addListener}" interval="100"/>
       <a4j:outputPanel id="msg" ajaxRendered="true">
       <h:outputText value="#{theBean.date}">
       <f:convertDateTime type="time"/>
       </h:outputText>
       </a4j:outputPanel>
       <a4j:log level="ALL" popup="true" hotkey="L"/>
       </a4j:form>
       </f:view>
      


      The receiver
       <f:view>
       <a4j:status startText="in progress" stopText="done"/>
      
       <h:form>
       <a4j:region>
       <a4j:commandButton value="Push!!" action="#{theBean.push}"/>
       <a4j:log level="ALL" popup="true" hotkey="L"/>
       </a4j:region>
       </h:form>
       </f:view>
      


      My msg is not updated...

      Can anyone help,

      Julien.

        • 1. Re: Working example of a4j:push [Please help Ilya]

          I can't help but I'm (and many others in this forum, too, I think) very interested in that.

          • 2. fmarwede
            balteo

            Hello fmarwede,

            I am glad you too are interested in a4j:push.

            Have you tried a4j:push personally or do you have code you could share?

            I will definitely post info if and when I get information on my side.

            Julien.

            • 3. Re: Working example of a4j:push [Please help Ilya]
              ilya_shaikovsky

              using just your code for bean and next page

              <!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:a4j="https://ajax4jsf.dev.java.net/ajax"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:rich="http://richfaces.ajax4jsf.org/rich"
               xmlns:ui="http://java.sun.com/jsf/facelets">
               <ui:composition template="/templates/common.xhtml">
               <ui:define name="body">
               <a4j:status startText="in progress" stopText="done"/>
              
               <a4j:form>
               <a4j:push reRender="msg" eventProducer="#{pushBean.addListener}" interval="100"/>
               <a4j:outputPanel id="msg" >
               <h:outputText value="#{pushBean.date}">
               <f:convertDateTime type="time"/>
               </h:outputText>
               </a4j:outputPanel>
               <a4j:commandButton value="Push!!" action="#{pushBean.push}" ajaxSingle="true"/>
               <a4j:log popup="false"/>
               </a4j:form>
               </ui:define>
               </ui:composition>
              </html>


              I'm able to see that push working. Checked with enabled setted to false - button doesn't change anything and setting to true - time updated after button clicked.

              • 4. Re: Working example of a4j:push [Please help Ilya]
                balteo

                Thanks a lot for you reply Ilya.
                I forgot to mention that the two JSP fragments go into separate JSP pages. I reworked the example and came up with something more complex and that make sense but that does not work.

                from faces-config.xml

                <managed-bean>
                 <managed-bean-name>theBean</managed-bean-name>
                 <managed-bean-class>pack.TheBean</managed-bean-class>
                 <managed-bean-scope>request</managed-bean-scope>
                 </managed-bean>
                 <managed-bean>
                 <managed-bean-name>myApplicationBean</managed-bean-name>
                 <managed-bean-class>pack.MyApplicationBean</managed-bean-class>
                 <managed-bean-scope>application</managed-bean-scope>
                 </managed-bean>
                


                TheBean.java

                package pack;
                
                import java.util.Date;
                import java.util.EventListener;
                import java.util.EventObject;
                import javax.faces.context.FacesContext;
                
                
                /**
                 *
                 * @author Administrator
                 */
                public class TheBean {
                
                 private Date date;
                 private MyPushEventListener listener;
                
                 public TheBean() {
                 MyApplicationBean app_bean = (MyApplicationBean) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("myApplicationBean");
                 this.listener = app_bean.getListener();
                 }
                
                 public void addListener(EventListener listener) {
                 synchronized (listener) {
                 if (this.listener != listener) {
                 MyApplicationBean app_bean = (MyApplicationBean) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("myApplicationBean");
                 this.listener = app_bean.getListener();
                 }
                 }
                 }
                
                 public Date getDate() {
                 return new Date();
                 }
                
                 public void push() {
                 synchronized (this.listener) {
                 MyApplicationBean app_bean = (MyApplicationBean) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("myApplicationBean");
                 this.listener = app_bean.getListener();
                 this.listener.onEvent(new EventObject(this));
                 }
                 }
                }
                
                


                the application-scoped bean:

                package pack;
                
                /**
                 *
                 * @author jumartin
                 */
                
                public class MyApplicationBean {
                
                 private MyPushEventListener listener;
                
                 public MyApplicationBean() {
                 listener = new MyPushEventListener();
                 }
                
                 public MyPushEventListener getListener() {
                 return listener;
                 }
                
                
                
                }
                
                


                in a jsp page invoked before other pages
                 <%
                 application.setAttribute("myApplicationBean", new pack.MyApplicationBean());
                 %>
                


                the push event listener
                package pack;
                
                import java.util.EventObject;
                import org.ajax4jsf.event.PushEventListener;
                
                public class MyPushEventListener implements PushEventListener {
                
                 public void onEvent(EventObject evt) {
                 System.out.println(evt.getSource());
                 System.out.println(this);
                 //DO SOME ACTION IN HERE
                 }
                
                }
                


                the TWO jsps (fragments)
                receiver.jsp
                <a4j:form>
                 <rich:message for="date" passedLabel="ok" ajaxRendered="true" showDetail="true"/>
                
                 <a4j:push reRender="date" immediate="true" eventProducer="#{theBean.addListener}" interval="100"/>
                 <a4j:outputPanel id="date" ajaxRendered="true">
                 <h:outputText value="#{theBean.date}">
                 <f:convertDateTime type="time"/>
                 </h:outputText>
                 </a4j:outputPanel>
                 <a4j:log level="ALL" popup="false" hotkey="L"/>
                 </a4j:form>
                
                
                **************
                Pusher.jsp
                <f:view>
                 <a4j:status startText="in progress" stopText="done"/>
                
                 <h:form>
                 <a4j:region>
                 <a4j:commandButton value="Push!!" action="#{theBean.push}" ajaxSingle="true"/>
                 <a4j:log level="ALL" popup="false" hotkey="L"/>
                 </a4j:region>
                 </h:form>
                 </f:view>
                
                
                


                I have spent a good part of the afternoon working on this and I am still not able to make it work.

                Any clue? I changed to ajaxSingle=true

                Julien.