1 Reply Latest reply on Jun 26, 2007 2:11 AM by Andy Gibson

    How to run a page action only once? or How to maintain the c

    Grant Rettke Novice

      Hi folks,

      We've got a single requirement in our app where the user can come in on a RESTful URL. The idea is that they make a request and then leave, there is no more interaction. Originally I set this up as a single page with an action. However, I didn't know how to specify that the action only be executed once, so it got run each time the page was submitted after validation errors for example. I ended up coding around it by maintaining state in the component, but that was hacky, as the component didn't need to maintain anything beyond page conversations.

      My solution was to create a restful url whose sole purpose in life is to jumpstart a component only once:

      <page view-id="/prepareInspectionRequest.xhtml"
       action="#{inspectionRequestAction.preparePage}">
       <param name="accountNumber"
       value="#{inspectionRequestForm.accountNumber}"/>
       <param name="userProfile"
       value="#{inspectionRequestForm.userProfile}"/>
      
       <navigation from-action="#{inspectionRequestAction.preparePage}">
       <rule if="#{inspectionRequestAction.validRequest}">
       <redirect view-id="/inspectionRequest.xhtml"/>
       </rule>
       <rule if="#{not inspectionRequestAction.validRequest}">
       <redirect view-id="/message.xhtml"/>
       </rule>
       </navigation>
       </page>
      


      Is this the best approach?

      The conversation scopes of these components defaults to page, which could be fine. After the redirect, the component has lost its state. How to redirect and maintain "page conversation" state.

        • 1. Re: How to run a page action only once? or How to maintain t
          Andy Gibson Novice

          If I'm understanding the problem right, you could put the initialization code into a @Create annotated method which would be called when when the backing bean is created as long as the backing bean is used only with this page. I think you would have to make the bean conversational scope, and run the page within the conversational scope. I must admit I don't fully understand the page scope. I think in page scope it creates and destroys the bean each time. (I still don't get the differences between page and even scopes).

          Another option might be simply to outject a flag into the conversational scope using something like :


          
          @In @Out(Scope=CONVERSATION)
          private Boolean isPrepared;
          


          Set this flag in your initialization scope and it will be passed in and out of the bean on each action. I think this could work, but I'm not sure.

          Of course then you can access the #{isPrepared} value from your page navigation logic if you still need it.