5 Replies Latest reply on Feb 14, 2008 4:53 PM by Sebastien Degardin

    Need help to understand who to use the PAGE context

    fredatwork Newbie

      Hello,

      I trying to understand how to use the page context with seam and I need some expertise.

      I built a very simple working sample with :
      - two html pages (sample2.xhtml and sample2next.xthml)
      - sample2.xhtml holds a button that throws an action Sample2Action.init.
      - this action initiates a POJO object named Sample2Context that holds a counter. It is initiated to 1 by the action. This object is injected from and outjected to the PAGE seam context.
      - pages.xml redirect to sample2next.html' upon completion of Sample2Action.init.

      Here is the sample2.xhtml page (it renders a simple 'Start' button) :

      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:s="http://jboss.com/products/seam/taglib">
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" lang="fr"/>
       <<title>Sample2</title>
      </head>
      <body>
      <h:form>
      
       <h:messages/>
       <h:commandButton action="#{Sample2Action.init}" value="Start"></h:commandButton>
      
      </h:form>
      
      </body>
      </html>


      Here is the sample2next.xhtml page that I get when you press on the start button in sample2.xhtml (it renders the counter and a 'Fresh' button which increments the counter) :
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:s="http://jboss.com/products/seam/taglib">
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" lang="fr"/>
       <title>Sample2Next</title>
      </head>
      <body>
      <h:form>
      
       <h:messages/>
      
       <h:outputLabel value="Counter :"/>
       <h:outputText value="#{Sample2Context.counter}"/>
       <h:commandButton value="Refresh" action="#{Sample2Action.refresh}"/>
      
      </h:form>
      
      </body>
      </html>


      Here is the POJO I wanna store in the page :
      @Name("Sample2Context")
      public class Sample2Context implements Serializable {
      
       private int counter;
      
       /**
       * @return the counter
       */
       public int getCounter() {
       return counter;
       }
      
       /**
       * @param counter the counter to set
       */
       public void setCounter(int counter) {
       this.counter = counter;
       }
      
       /**
       * Increment counter by 1
       */
       public void increment() {
       counter++;
       }
      
       /**
       * @see java.lang.Object#toString()
       */
       @Override
       public String toString() {
       return "[Context = " + counter + "]";
       }
      
      }


      Here is the action bean :
      @Stateless(name="Sample2Action")
      @Name("Sample2Action")
      public class Sample2Bean implements Sample2Local {
      
       // Page context object
       @In(value="Sample2Context", create=true, required=false)
       @Out(value="Sample2Context", scope=ScopeType.PAGE, required=true)
       private Sample2Context context;
      
       // Messages
       @In
       private FacesMessages facesMessages;
      
       /**
       * @see sample2.Sample2Local#init()
       */
       public void init() {
       context.increment();
       facesMessages.add(FacesMessage.SEVERITY_INFO, "Initiated page context " + context);
       System.out.println("Initiated page context " + context);
       }
      
       /**
       * @see sample2.Sample2Local#refresh()
       */
       public void refresh() {
      
       Object object = Contexts.lookupInStatefulContexts("Sample2Context");
       System.out.println("Context object : " + object);
       Object pageObject = Contexts.getPageContext().get("Sample2Context");
       System.out.println("Page context object : " + pageObject);
      
       System.out.println("In refresh with : " + context);
       context.increment();
       System.out.println("Incremented page context " + context);
       facesMessages.add(FacesMessage.SEVERITY_INFO, "Incremented page context " + context);
       }
      
       /**
       * @see sample2.Sample2Local#remove()
       */
       @Remove
       public void remove() {
       }
      
      }


      Now here is my problem and question :
      Once the start button is pressed, sample2next.xhtml renders a counter at 0. However, the Sample2Action.init action initiated this counter to 1. Everything happens as the completion of Sample2Action.init does not outject the POJO Sample2Context to the PAGE context, and the action Sample2Action.refresh retrieves a newly created object.

      Why is that ?