3

Hi I'm learning JSF/Primefaces. whenever user click on a link it should forward to the edit page with the information, but it's empty.

I tried to debug, following flow happens:

  • user click the link of the element that wants to edit
  • QuoteStatusList.init() is called
  • QuoteStatusForm.init() is called
  • QuoteStatusForm.edit is called
  • QuotestatusForm.quoteStatus bean is filled with the information
  • return "edit"
  • foward to quoteStatusForm.xhtml
  • and QuoteStatusForm.init() is called again, all datas filled are lost

I found this but I'm now only using jsf annotation to manage view beans

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>

faces-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>
Community
  • 1
  • 1
Yichz
  • 9,250
  • 10
  • 54
  • 92
  • What's `quoteStatus` from `value="#{quoteStatus.idQuoteStatus}"` supposed to be? – kolossus Oct 03 '14 at 04:28
  • QuoteStatus a plain java bean, String name, String description. idQuoteStatus = an Integer – Yichz Oct 03 '14 at 04:29
  • Does that actually work? If it's not a `@ManagedBean` or scoped within one of the 4 (or 5) web application scopes, it would serve no purpose there – kolossus Oct 03 '14 at 04:33

1 Answers1

1

What you're experiencing is appropriate behaviour for @RequestScoped and @ViewScoped beans.

  1. @RequestScoped - Beans of this scope will not survive a redirect/forward to another page. That means that if you're on a page backed by a bean of this scope, whenever you issue a new HTTP request (either ajax, a full-on page refresh, or a redirect), the instance of that bean you're working on is destroyed, voided, ceases to exist. Along with all its member variables

  2. @ViewScoped - Beans of this scope will also not survive a full redirect/forward. They will however survive page refreshes and ajax. What this means is that, as long as you stay on the same page (backed by a @ViewScoped bean), don't return any navigation case, you're guaranteed to be working with the same instance of the bean.


How are these beans supposed to communicate then? If leaving one page means that you lose everything the backing bean contains, what your options (you should be asking)? Well, there are a number of ways that JSF beans can communicate. Read through the gospel on inter-bean communication


So what's happening in your case

  1. QuoteStatusList list is destroyed when you navigate away from the page that it backs. This also means that when you come back, you're dealing with a brand new instance of that bean (and that's why init is called twice)

  2. QuoteStatusForm was destroyed because you returned edit from that bean, causing the instance you're working with to be destroyed and recreated on page load


What to do:

To avoid destroying QuoteStatusForm, you can just return null from edit

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • I tried t o return null from edit(), if I do that the page will alwasy stays in the quoteStatusList.xhtml page. because it calls the edit() function first, gather the information, then redirect to quoteStatusform.xhtml. – Yichz Oct 03 '14 at 05:14
  • That's not what your config says @Kossel. You're returning `edit`, just to return to the same page you were on. Why? – kolossus Oct 03 '14 at 05:16
  • Also, how are you getting access to `quoteStatusManager` from within the `edit` method @Kossel. You're not instantiating it by yourself are you? – kolossus Oct 03 '14 at 05:21
  • exactly, I should return void there. but for somehow, if I return null or void, the page stuck at the listing page, because no rule tells the application to display the form page. that's why I put "return edit" – Yichz Oct 03 '14 at 05:21
  • I think you misunderstand me, I'm referring to the `edit` outcome from `quoteStatusForm`. Return null in that `edit` method @Kossel – kolossus Oct 03 '14 at 05:31
  • I understood you :S if I return null in the edit(), the page will stay at the listing page... because it's not forwarding to the detail form page.. (quoteStatusForm.xhtml) – Yichz Oct 03 '14 at 05:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62383/discussion-between-kossel-and-kolossus). – Yichz Oct 03 '14 at 05:42