Announcement Announcement Module
No announcement yet.
View scope with Spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • View scope with Spring


    I would like to have a bean with view scope, in order to user ajax in the view tier (it's a JSF and facelets application).

    How can I implement a view scope with Spring? I've seen some examples in the web, regarding the implementation of Scope interface. None of them works.

    Please note that I do not want to use Spring Web Flow.

  • #2

    I am of the opnion that the Request Scope should actually suffice your needs, be it a ajax call or a reqular post call. However if you are very certain that this scope is a must then have a look at example found here.



    • #3

      I'm sure that request scope isn't enought for many use cases. I have a complex GUI, with many components that follow a particular state machine.

      Without a view scope, it's (near) impossible to implement this GUI using Ajax calls.

      Thanks for the link provided, but the example provided did not work.


      • #4

        After going through little documentation I found that this scope is somewhat similar to viewstate implementation in .NET platform. One approach to your problem could be to have two extra methods in your bean say toJSON and static fromJSON and have a hidden variable to store the bean data in JSON format. For security purpose you can use some encryption+b64 conversion to store the value in hidden field. In controller all you will have to do is to invoke the static fromJSON method of the bean passing it the hidden viewstate and let it return the bean instance. I am using this technique in my project successfully.



        • #5
          View scope with Spring [HibernateDaoSupport.getSession() NullPointerException]


          I got a bug in this example when you use Spring 3 and Hibernate 3.

          HibernateDaoSupport re-serialize, but got a NullPointerException in getSession() method.

          Then we need to re-inject (re-configure) dependencies of Spring Beans like a ManagedBean and your Services by @Autowired annotation.

          	<!-- View scope -->
          	<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
          		<property name="scopes">
          				<entry key="view">
          					<bean class="com.magoit.util.jsf.scope.ViewScope" />

          package com.magoit.util.jsf.scope;
          import java.util.Map;
          import javax.faces.context.FacesContext;
          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;
          import org.springframework.beans.factory.ObjectFactory;
          import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
          import org.springframework.beans.factory.config.Scope;
          import org.springframework.web.context.ContextLoader;
          import org.springframework.web.context.WebApplicationContext;
          public class ViewScope implements Scope {
          	final Logger logger = LoggerFactory.getLogger(ViewScope.class);
          	public Object get(final String name, final ObjectFactory objectFactory) {
          		final Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
          		if (viewMap.containsKey(name)) {
          			Object bean = viewMap.get(name);			
          			// restore a transient autowired beans after re-serialization bean
          			WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
          			AutowireCapableBeanFactory autowireFactory = webAppContext.getAutowireCapableBeanFactory();
          			if (webAppContext.containsBean(name)) {
          				// Reconfigure resored bean instance.
          				bean = autowireFactory.configureBean(bean, name);
          			// end restore
          			return bean;
          		} else {
          			final Object object = objectFactory.getObject();
          			viewMap.put(name, object);
          			return object;
          	public String getConversationId() {
          		return null;
          	public void registerDestructionCallback(final String name, final Runnable callback) {
          		// Not supported
          	public Object remove(final String name) {
          		return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
          	public Object resolveContextualObject(final String key) {
          		return null;
          Enjoy this.



          • #6
            Hey Marcelo, I think you answer could be helpful in my case. Then I tested it out but without no success. I getting the same issue still! Whenever I have a ManagedBean that has a ViewScope my StoredProcedures seems to get lost, I mean, it seems to not injecting DataSource anymore. This only happens with JSF ViewScope.