9 Replies Latest reply on Dec 28, 2009 11:53 AM by Jaaouane Aymen

    Need to understand the @factory

    Jaaouane Aymen Newbie

      Hello,
      I have an xhtml page which displays a list, this list is built by a factory in a stareless session bean.
      when the pages is demanded for the first request, the factory method is called and the list is well displayed. after the page is rendered, when we do a sbmitt on this page, the first thing done by the seam life cycle is to call this factory again, and before calling the apply request values values.I remarked it by debugging the source code.
      I want to understand why the factory method is called in the beginning of the life cycle when the pages is requested by post method.
      I hope my question is clear.

        • 1. Re: Need to understand the @factory
          Adrian Mitev Master

          The idea behind @Factory is to lazy create an object and store it in the context when a variable with the factory name gets requested. When the object is created it's stored in the same scope as the factory or in the scope of the owning component if the factory scope isn't specified. Probably your scope is EVENT, so the factory is called on each request and after the response is sent to the browser, the variable is destroyed.

          • 2. Re: Need to understand the @factory
            Jaaouane Aymen Newbie
            Hello, thank you Adrian for your help,
            I know very well that my variable is destroyed after the response is sent because my component scope is EVENT.
            but what i do not understand why the factory is called the first thing after i submit the page.
            As i understand from the lifecycle of JSF, the life cycle must update the model values of the form and after invoke the application, so why the factory is called before to instansiate the backing bean of the model.
            my factory variable is needed only to render the response, so why it is instansieted?
            I debug my code, i put a message in the constructor of Project(my baking bean for rthe form), and a message in the factory method, when the request is get(first request), the backing bean is first instansieted and after the factory is called.
            but after a submit, i see that the factory is called and after the backingbean is instansieted to update model.
            why? is  the resoreView that calls the factory?

            To understand me better, this is my xhtml page :

                    <h:form>
                    <s:validateAll>
                            <h:inputText value="#{projet.nom}" size="15" required="true"/>
                            <br />
                            <h:inputText value="#{projet.description}" size="15" required="true"/>
                            </s:validateAll>
                            <h:messages/>
                            <br />
                            <h:commandButton type="submit" value="creer Projet"
                                    action="#{projetManager.ajouterProjet}" />
                    </h:form>
                    <h:dataTable value="#{projets}" var="project">
                            <h:column>
                                    <h:outputText value="#{project.nom}" />
                            </h:column>
                            <h:column>
                                    <h:outputText value="#{project.description}" />
                            </h:column>
                    </h:dataTable>
                   
            after a submit, the factory is called first thing, because i think that my variable #{projets} is only needed to render the response.
            • 3. Re: Need to understand the @factory
              Adrian Mitev Master

              dataTable requires it's value not only for rendering but also for decode so it will call your factory even on submit without rerendering the same page. Keep your factory variable in page scope and you'll fix the problem.

              • 4. Re: Need to understand the @factory
                Jaaouane Aymen Newbie

                Thank you very much Adrian for your help, my problem was fixed.
                I am a beginner in using seam and i was very happy by your help.
                I want to understand better what is happening in background, i do not understand why the datatable do the
                decode in the jsf lifecycle. As i understand, the purpose of this phase in jsf life cycle is that each component updates its value, and this value will be searched by raquest.getParameter();
                but when this is a table, there will not be values which will be sent in the form. So why the datatable calls the decode method? i think that my question is turning in a jsf question.
                when it is a textbox or date field, it is logic to call decode but when it is a datatable, i do not understand the purpose.
                and i have an other issue, the model of datatable can be obtained from the viewstate field, the view state contains the state of the page, so the seam engine can obtain the model of this datatable in the last request, so why it calls the factory? i think that there is not a need to call the factory, because the model can be obtained.

                • 5. Re: Need to understand the @factory
                  Adrian Mitev Master

                  The dataTable performs decode because there could be ValueHolder components inside it.
                  Your model is not kept in the view state so it can't be restored. If you use the page scope, seam will store your object in the view state (this is how the page scope is implemented).

                  • 6. Re: Need to understand the @factory
                    Jaaouane Aymen Newbie

                    Hello Adrian,
                    Thank you very much again Adrian.


                    Your model is not kept in the view state : is it specefic for datatables or for all components?


                    For example if i have a textbox which binds to a seam component and this component is request scoped, the value will not be stored in the view state?

                    • 7. Re: Need to understand the @factory
                      Adrian Mitev Master

                      Your model is not kept in the view state : is it specefic for datatables or for all components?

                      The jsf components don't keep your beans and this is common for all components.




                      For example if i have a textbox which binds to a seam component and this component is request scoped, the value will not be stored in the view state?


                      The inputText component doesn't keep your bean(seam components)'s property in the view state.

                      • 8. Re: Need to understand the @factory
                        Jaaouane Aymen Newbie

                        Hello Adrian,
                        I am sorry beacuse my question was ambigous that's why you do not understand my question,
                        Your model is not kept in the view state : is it specefic for datatables or for all components?
                        in this question, i wanted to say the value and not the bean. it is obvious that jsf doen not keep the bean in the view state but the value is kept. so this is my new question and this is the accurate version of my question :
                        the value is not kept in the view state : is it specefic for datatables or for all components?


                        when i show the example of the inputtext, it is obvious that its value is kept in the view state and not the bean, and in the next reuqest, jsf will know the last value of the input text.
                        So why the value of the datatable is obtained from the factory method and not from the view(the value and not the bean)?

                        • 9. Re: Need to understand the @factory
                          Jaaouane Aymen Newbie

                          hello,
                          I am sorry and i will make my question clearer.
                          if the value of a datatable is stored in the page scope, the value will be stored in the view state. and if not, seam will call the factory method again when we submit in this page. This point is clear.
                          so for ValueHolders, is it the same or it is different?
                          if we have for example an inputtext which binds to a proporty of  component that its scope is request. its value will be stored in the view state or  not? or we must keep the component in the page sope to have the value of the inputtext stored in the viewstate?