-1

Reffering to this Question h:commandLink not working when inside a list

I have the same Problem in my Application. I would like to try a ViewScoped bean, but cause using Spring 2.0 I dont have the chance to place my bean into View Scope. Any other workarounds, I could try.

Would be nice if u can give me a hint.

Community
  • 1
  • 1
Amokkx
  • 1

1 Answers1

0

You can port the view scope to spring:

package com.yourdomain.scope;

import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

public class ViewScope implements Scope {

    public Object get(String name, ObjectFactory objectFactory) {
        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;
        }
    }

    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    public String getConversationId() {
        return null;
    }

    public void registerDestructionCallback(String name, Runnable callback) {
        //Not supported
    }

    public Object resolveContextualObject(String key) {
        return null;
    }
}

Register the new scope in spring configuration file

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="view">
                <bean class="com.yourdomain.scope.ViewScope"/>
            </entry>
        </map>
    </property>
</bean>

And than use it with your bean

@Component
@Scope("view")
public class MyBean {

    ...

}
Maxim Manco
  • 1,954
  • 1
  • 12
  • 19