1
votes

I am using primefaces 5.1, jsf 2.2 (Mojarra), spring 4.1.6, prettyfaces 2.3.3. I have a xhmtl which contains a tabview with more than one tab and spring managed view scope bean (viewBean). The issue is values of the page (view.xhmtl) and that of zeroth tab is displayed. but the rest of the data in remaining tabs is not being displayed.ie, the value in New Deals tab is not being displayed.

When newDeals object of viewBean is made static, the value gets displayed.

I went through jsf viewscoped bean - set values for every page(tabs), Using JSF with multiple tabs in one browser, and would like to know if what i understood from the above links is correct. ie, we have to use session scope for a bean or page having multiple tabs in a view xhtml??

The project is uses prettyfaces for redirection.

Update
In http://balusc.blogspot.in/2011/09/communication-in-jsf-20.html#ManagedBeanScopes, it is mentioned that

@ViewScoped: a bean in this scope lives as long as you're interacting with the same JSF view in the browser window/tab. It get created upon a HTTP request and get destroyed once you postback to a different view.

So how come the (1st to last) tab's data is not loaded in a page consisting of a tabview (with multiple tabs) while the 0th tab's data is loaded?

Code is as belows

List.xhmtl

<p:dataScroller value="#{listBean.pdtVOList}" var="pdt">
   <p:commandLink value="#{pdt.name}" action="#{viewBean.viewPdtFrmList(pdt.id)}" ></p:commandLink>
</p:dataScroller>

ListBean

@Scope("view")
@Component
public class ListBean implements Serializable {
  private List<Product> pdtVOList; //include getters and setters
  public void fillPage() {
    pdtVOList = ownerService.fillProductList(); //gets all product from db
  }

}

viewBean is

@Component
@Scope("view")
public class ViewBean implements Serializable {
   private static Product pdt = new Product();
   private NewDeals newDeals = new NewDeals();
   //include getter and setters

   public void fillPage() {
      //is Empty
   }
   public void viewPdtFrmList(String pdtIdStr) {
      pdt = new Product();
    //set pdt values
      setPdt(getPdtValuesFromDB(pdtIdStr));

      newDeals = pdtRepository.getNewDealsCOmbo();

      FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/pdt/view");

   }

   public getPdtValuesFromDB(String pdtIdStr) {
     //get values from database
     pdt = pdtRepository.getValuesFromDB(Integer.parseInt(pdtIdStr));
   }
}

Product Model

private int id;
private String name; //Include getter and setter

pdtView.xhtml

<h:outputText value="#{viewBean.pdt.id}"/>
<h:outputText value="#{viewBean.pdt.name}"/>
<p:tabView id="tabview" style="width:100%;float:right;">
    <p:tab id="clientPricing" title=" Pricing">
        <h:outputText value="#{viewBean.pdt.price}"/>
    </p:tab>
    <p:tab title="New Deals">
        <h:outputText value="#{viewBean.newDeals.name}"/>
    </p:tab>
</p:tabView>

prettyconfig.xml

<url-mapping id="ownerandpetsview"> 
    <pattern value="/pdt/view" />          
    <view-id value="/WEB-INF/views/pdtView.xhtml" />
 </url-mapping>

web.xml

    <filter>
        <filter-name>Pretty Filter</filter-name>
        <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
    </filter>

   <filter-mapping>
        <filter-name>Pretty Filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

web.xml

<context-param>
        <param-name>javax.faces.STATE_SAVING</param-name>
        <param-value>true</param-value>
    </context-param
1
since its view bean it loses its state when you press tab and instance newDeals is instantiated again.vels4j
so you mean that on tabchange i should reinitialze the newdeals object..right?Eva Mariam

1 Answers

0
votes

You can make it work this by viewparam, have a viewparam in pdtView.xhtml

    <f:metadata>
        <f:viewParam name="key" value="#{viewBean.key}"/>
    </f:metadata>

and initilize product from the key

    @Component
    @Scope("view")
    public class ViewBean implements Serializable {

        private static Product pdt = new Product();
        private NewDeals newDeals = new NewDeals();
        //include getter and setters

        public String key;

        public void fillPage() {
            //is Empty
        }

        @PostConstruct
        public void loadProduct () {
          getPdtValuesFromDB(key);
        }

        public void viewPdtFrmList(String pdtIdStr) {
            pdt = new Product();
            //set pdt values
            setPdt(getPdtValuesFromDB(pdtIdStr));

            newDeals = pdtRepository.getNewDealsCOmbo();

            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/pdt/view");

        }

        public getPdtValuesFromDB(String pdtIdStr) {
            //get values from database
            pdt = pdtRepository.getValuesFromDB(Integer.parseInt(pdtIdStr));
        }
    }

finally from the list.xhtml you need to call the view page with parameter like view.xhtml?key=pdt.id