3 Replies Latest reply on Mar 30, 2011 6:10 PM by nbelaevski

    viewScope problem

    balteo

      Hello,

      I am trying to get a viewScoped spring bean to work with jsf and richfaces.

      Here is how I implemented the viewScope:

       

      package com.jeanbaptistemartin.util;

      import java.util.Map;

      import javax.faces.context.FacesContext;

      import org.springframework.beans.factory.ObjectFactory;

      import org.springframework.beans.factory.config.Scope;

       

      /**

      * Implements the JSF View Scope for use by Spring. This class is registered as a Spring bean with the CustomScopeConfigurer.

      */

      public class ViewScope implements Scope {

       

          @Override

          public Object get(String name, ObjectFactory<?> objectFactory) {

              if (FacesContext.getCurrentInstance().getViewRoot() != null) {

                  Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

                  if (viewMap.containsKey(name)) {

                      return viewMap.get(name);

                  } else {

                      Object object = objectFactory.getObject();

                      viewMap.put(name, object);

                      return object;

                  }

              } else {

                  return null;

              }

          }

       

          @Override

          public Object remove(String name) {

              if (FacesContext.getCurrentInstance().getViewRoot() != null) {

                  return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);

              } else {

                  return null;

              }

          }

       

          @Override

          public void registerDestructionCallback(String name, Runnable callback) {

              // Do nothing

          }

       

          @Override

          public Object resolveContextualObject(String key) {

              return null;

          }

       

          @Override

          public String getConversationId() {

              return null;

          }

      }

       

      Here is my jsf page:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"

            xmlns:ui="http://java.sun.com/jsf/facelets"

            xmlns:h="http://java.sun.com/jsf/html"

            xmlns:f="http://java.sun.com/jsf/core"

            xmlns:pretty="http://ocpsoft.com/prettyfaces"

            xmlns:a4j="http://richfaces.org/a4j"

            xmlns:rich="http://richfaces.org/rich"

            xmlns:jbm="http://www.jeanbaptistemartin.com/facelets/functions">

          <body>

              <ui:composition template="WEB-INF/include/template.xhtml">

                  <ui:define name="title-bar-title">

                      <title lang="#{view.locale.language}">

                          <h:outputText value="#{msg['menu.galerie']}"/>

                          <h:outputText value=" #{msg['titre.separateur']} " />

                          <h:outputText value="#{msg['artiste.titre']}" />

                      </title>

                  </ui:define>

                  <ui:define name="meta-description">

                      <meta name="description" content="#{msg['meta.description.galerie']}" lang="#{view.locale.language}"/>

                  </ui:define>

                  <ui:define name="javascript">

                      <script language="javascript" type="text/javascript"  src="#{facesContext.externalContext.requestContextPath}/js/jquery.formulaire-inscription.js"/><!-- todo: relocate -->

                  </ui:define>

       

                  <ui:define name="locale-component">

                      <pretty:link mappingId="galerieAction" hreflang="fr" lang="fr">

                          <f:param value="fr" />

                          <h:outputText value="#{msg['langue.francais.libelle']}" />

                      </pretty:link>

                      <h:outputText value=" #{msg['langue.separateur']} "/>

                      <pretty:link mappingId="galerieAction" hreflang="en" lang="en">

                          <f:param value="en" />

                          <h:outputText value="#{msg['langue.anglais.libelle']}" />

                      </pretty:link>

                  </ui:define>

       

                  <ui:define name="menu">

                      <ui:include src="WEB-INF/include/menu.xhtml">

                          <ui:param name="page" value="galerie"/>

                      </ui:include>

       

                  </ui:define>

       

                  <ui:define name="title">

                      <ul>

                          <li><strong><pretty:link mappingId="rootAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.accueil']}" /></pretty:link></strong></li>

                          <li><strong><h:outputText value="#{msg['menu.galerie']}"/></strong></li>

                      </ul>

                  </ui:define>

       

                  <ui:define name="body">

                      <a4j:outputPanel layout="block" styleClass="wrapper">

                          <rich:dataGrid value="#{galerieView.sculptures}" var="sculpture" columns="3" styleClass="datagrid-galerie" iterationStatusVar="iterations">

                              <h:panelGroup rendered="#{!iterations.last}">

                                  <p>

                                      <pretty:link mappingId="sculptureAction" styleClass="miniature-link">

                                          <f:param value="#{view.locale.language}"/>

                                          <f:param value="#{jbm:normaliserURL(sculpture.sculpturei18nMap[view.locale.language].titre)}" />

                                          <f:param value="#{sculpture.sculptureID}" />

                                          <h:graphicImage value="#{initParam['com.jeanbaptistemartin.static.url']}/#{sculpture.photoMiniature}" styleClass="miniature-image" alt="#{sculpture.sculpturei18nMap[view.locale.language].titre}, #{sculpture.sculpturei18nMap[view.locale.language].matiere}, #{jbm:renvoyerAnnee(sculpture.annee)}."/>

                                      </pretty:link>

                                  </p>

                                  <p>

                                      <pretty:link mappingId="sculptureAction" styleClass="miniature-link-text">

                                          <f:param value="#{view.locale.language}"/>

                                          <f:param value="#{jbm:normaliserURL(sculpture.sculpturei18nMap[view.locale.language].titre)}" />

                                          <f:param value="#{sculpture.sculptureID}" />

                                          <h:outputText value="#{sculpture.sculpturei18nMap[view.locale.language].titre}, " />

                                          <h:outputText value="#{sculpture.sculpturei18nMap[view.locale.language].matiere}" />

                                      </pretty:link>

                                  </p>

                              </h:panelGroup>

                              <h:panelGroup rendered="#{iterations.last}" styleClass="miniature-link">

                                  <p id="sellette">

                                      <img src="#{facesContext.externalContext.requestContextPath}/images/200x200.gif" class="miniature-image" alt="" onclick="slideUp()"/>

                                  </p>

                                  <div class="formulaireInscriptionSculpture">

                                      <h:form id="formulaireInscriptionSculpture" prependId="false">

                                          <ui:include src="WEB-INF/include/formulaire-inscription-sculpture.xhtml"/><!-- todo: ne fonctionne pas... -->

                                      </h:form>

                                  </div>

                              </h:panelGroup>

                              <rich:jQuery name="slideUp" query="slideUp({duration:'slow'});slideDownForm()" selector="#sellette"/>

                              <rich:jQuery name="slideDownForm" query="slideDown({duration:'slow'})" selector="div.formulaireInscriptionSculpture" />

                              <rich:jQuery name="slideDownSellette" query="slideDown({duration:'slow'})" selector="#sellette"/>

                          </rich:dataGrid>

                      </a4j:outputPanel>

                  </ui:define>

              </ui:composition>

          </body>

      </html>

       

      The problem I have is that now that I have switched to viewScope, my page is empty i.e. this:

      #{galerieView.sculptures}

      is empty.

      Can anyone please help?

      Thanks in advance,

      J.

        • 1. viewScope problem
          ilya_shaikovsky

          so just check in debug why null returned from get method.

          • 2. viewScope problem
            balteo

            Hello Ilya,

             

            It seems that it does not return null...

             

            Here is the code for the get method:

            @Override

                public Object get(String name, ObjectFactory<?> objectFactory) {

                    if (FacesContext.getCurrentInstance().getViewRoot() != null) {

                        Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

                        if (viewMap.containsKey(name)) {

                            return viewMap.get(name);

                        } else {

                            Object object = objectFactory.getObject();

                            viewMap.put(name, object);

                            log.debug("object: "+object);

                            return object;

                        }

                    } else {

                        log.debug("################ : GET VIEW ROOT NOT FOUND");

                        return null;

                    }

                }

             

            Here is what it prints when I load the Galery page:

            DEBUG [13:21:31,204] (ViewScope.java:25) - object: com.jeanbaptistemartin.view.GalerieView@a765c

             

            I must be missing something else...

             

            Any idea?

             

            J.

            • 3. viewScope problem
              nbelaevski

              Try implementing another methods, e.g. resolveContextualObject(String key)