The same @PostConstruct calls twice, the data doesn't survive

Hi I am learning JSF / Primefaces. whenever a user clicks on a link, it should be redirected to an edit page with information, but it's empty.

I tried to debug, after the flow happened:

  • the user clicks on a link to the item they want to edit
  • QuoteStatusList.init () is called
  • QuoteStatusForm.init () is called
  • QuoteStatusForm.edit is called
  • QuotestatusForm.quoteStatus bean is filled with information
  • return "edit"
  • foward in quoteStatusForm.xhtml
  • and QuoteStatusForm.init () is called again, all filled data is lost

I found this one , but now I am only using jsf annotation to manage the beans view

QuoteStatusList.java

@ManagedBean
@RequestScope    
public class QuoteStatusList extends BasePage implements Serializable {
        @PostConstruct
        public void init(){
            log.debug("initing...");
        }
    ...
    }

      

QuoteStatusForm.java

@ManagedBean
@ViewScope
    public class QuoteStatusForm extends BasePage implements Serializable {
    @PostConstruct
        public void init(){
            log.debug("initing...");
        }

    public String edit() {
            log.debug("editing..");
             if (idQuoteStatus != null && idQuoteStatus != 0) {
                quoteStatus = quoteStatusManager.get(idQuoteStatus);
            } else {
                quoteStatus = new QuoteStatus();
            }  
            return "edit";
        }

    }

      

BasePage.java

@ManagedBean
@RequestScoped
public class BasePage {
    //nothing is injected
//no other @postConstruct function
}

      

QuoteStatusList.xhtml

<h:commandLink action="#{quoteStatusForm.edit}" value="#{quoteStatus.idQuoteStatus}">
                        <f:param name="idQuoteStatus" value="#{quoteStatus.idQuoteStatus}"/>
                    </h:commandLink>

      

person-config.xml

 <navigation-rule>
        <from-view-id>/quoteStatusList.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>edit</from-outcome>
            <to-view-id>/quoteStatusForm.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/quoteStatusForm.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>edit</from-outcome>
            <to-view-id>/quoteStatusForm.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

      

+3


source to share


1 answer


What you are experiencing is the appropriate behavior for @RequestScoped

both @ViewScoped

beans.

  • @RequestScoped

    - Beans of this area will not save redirects / redirects to another page. This means that if you are on a page maintained by a bean of this scope, whenever you issue a new HTTP request (either ajax, or a full page refresh, or a redirect), an instance of that bean you are running, destroyed , invalidated, ceases to exist ... Along with all of its member variables

  • @ViewScoped

    - Beans of this area will also not save full forward / forward. However, they will survive page refresh and ajax. This means that as long as you stay on the same page (backed by a @ViewScoped

    bean) don't return any navigation case, you are guaranteed to be working with the same bean instance.




How are these Beans supposed to communicate then? If leaving one page means you lose everything the bean supports, what are your options (you have to ask)? Well, there are several ways that JSF Beans can interact. Read the gospel on bean interoperability




So what happens in your case

  • QuoteStatusList

    the list is destroyed when you move far from the page it links to. This also means that when you return, you are dealing with a completely new instance of that bean (and therefore init

    called twice)

  • QuoteStatusForm

    was destroyed because you returned edit

    from that bean, causing the instance you are working with to be destroyed and recreated on page load




What to do:

To avoid being destroyed QuoteStatusForm

, you can simply return null

fromedit

+1


source







All Articles