3 Replies Latest reply on Feb 23, 2012 11:00 AM by sunkaram

    Problem with ViewScope

    adsonvinicius

      Hi, i'm starting with RF4 and i'm having some problems... At this time, i need to pass a query string value to one page, get it and process it. The problem it's because the page which i want to pass and to get the query string value are the same, to do that i saw at google i need to use view scope session, but with this scope i can't re-render after update the bean since my bean's scope is a view scope.

       

      Does anyone can help me? I accept others suggestions to solve this problem...

       

      In categorias.xhtml...

       

      <h:form>    

              <rich:panel style="padding:0" headerClass="outpanelHeader" rendered="#{categoriasMB.allCategorias != null}" >

                <div align="center">

                  <rich:dataGrid value="#{categoriasMB.allCategorias}" iterationStatusVar="it2" style="background-image:url(arquivos/background.png)" id="tabela" var="item" columns="3" elements="9">

                      <rich:column colspan="0" rowspan="0">

                        <div align="center">

                          <h:graphicImage url="#{config.caminho}#{item.imagem}"/>

                          <br/>

                          <h:outputText style="font-size:14px" styleClass="textoNome" value="#{item.nome}" />

                          <br/>

                          <h:commandButton value="Abrir" styleClass="no-decor" action="#{categoriasMB.abrirLink}">

                              <f:param name="id" value="#{item.id}" />

                          </h:commandButton>

                          <a4j:commandButton value="Editar" styleClass="no-decor" render="editPaneAI" execute="@this" oncomplete="#{rich:component('editPaneAI')}.show()">

                            <f:setPropertyActionListener target="#{categoriasMB.editedCategoria}" value="#{item}" />

                          </a4j:commandButton>

                          <a4j:commandButton value="Remover" styleClass="no-decor" execute="@this" oncomplete="#{rich:component('confirmPaneAI')}.show()">

                              <f:setPropertyActionListener target="#{categoriasMB.editedCategoria}" value="#{item}" />

                          </a4j:commandButton>

                        </div>

                    </rich:column>

                  <f:facet name="footer">

                      <rich:dataScroller></rich:dataScroller>

                      <a4j:commandButton value="Nova Categoria" onclick="#{rich:component('addPaneAI')}.show();" />

                  </f:facet>

                 </rich:dataGrid>

               </div>

             </rich:panel>

           </h:form>

       

       

      In categoriasMB

      ...

       

          public CategoriasMB() {

              ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

              int id = (Integer.parseInt(iexternalContext.getRequestParameterMap().get("id"));

              allCategorias = DAO.getCategorias(id);

              }

          }

       

        

      public String abrirLink(){

            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

            String id = externalContext.getRequestParameterMap().get("id");

            return "categorias?faces-redirect=true&id_categoria="+id;

           

         }

       

      ...

        • 1. Re: Problem with ViewScope
          sunkaram

          view scope bean data available (between the requests) in the page only when you return 'null' by JSF action method.

           

          I am seeing the jsf page code not using id parameter passing at return ( return "categorias?faces-redirect=true&id_categoria="+id;). Not sure, where are you using this id in the page.  If you need it, make it as a bean property and use it in the page.

          • 2. Re: Problem with ViewScope
            adsonvinicius

            Hi Sunkara, First thank you by your reply.

             

            You're right about the id parameter, i tried simplify my code, hidding some parts to let more easy to understand but i forgot to rename some variables, Let me explain what happend:

             

            I have one jsf page which get some data from DAO.getCategorias. At first the previous page (projetos.xhtml) call this page above (categorias.xhtml) passing id_projeto in URL (categorias?faces-redirect=true&id_projeto="+id). So i need to make a if-clause to make a difference in construtor's bean.

             

            public CategoriasMB() {

                    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

                    id = externalContext.getRequestParameterMap().get("id_projeto");

                   if(id == null){

                        id = externalContext.getRequestParameterMap().get("id_categoria");

                    }

                    DAO = new daoCategorias();

                    if(id != null){

                        allCategorias = DAO.getCategorias(Integer.parseInt(id));

                    }

                }

             

            I wanna use the same jsf page to show different contents (as a template), so i tried to solve it passing a parameter in URL. And it works fine. The problem it's because i need to use a ViewScope to get and update my displayed data for each time my page is browsed (because it's the same jsf page) but using a viewscope, i can't edit & save my bean because my it's not more valid.

             

            I think that using a bean property won't works since in view scope the bean will not valid anymore, is it right?

            • 3. Re: Problem with ViewScope
              sunkaram

              yes, if you don't want to return null in action method view scope will not work.