JSF在Spring中查看范围

时间:2012-10-22 05:29:26

标签: spring jsf jsf-2 view-scope

Spring 3.0中是否有像JSF @ViewScoped这样的范围?我有一个使用JSF + Spring的应用程序,其中支持bean由Spring管理。我在Spring中没有找到像JSF wiew作用域这样的范围。我看过博客Porting JSF 2.0’s ViewScope to Spring 3.0,但它对我不起作用。

这是我对自定义Spring范围的尝试:

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 {

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

        System.out.println("**************************************************");
        System.out.println("-------------------- Getting objects For View Scope ----------");
        System.out.println("**************************************************");
        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;
        }
    }

    public Object remove(String name) {
        System.out.println("**************************************************");
        System.out.println("-------------------- View Scope object Removed ----------");
        System.out.println("**************************************************");

        if (FacesContext.getCurrentInstance().getViewRoot() != null) {
            return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
        } else {
            return null;
        }
    }

    public void registerDestructionCallback(String name, Runnable callback) {
        // Do nothing
    }

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

    public String getConversationId() {
        return null;
    }

}

application-context.xml

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

5 个答案:

答案 0 :(得分:4)

最近我创建了maven工件来解决这个问题。

请参阅我的github javaplugs/spring-jsf存储库。

答案 1 :(得分:3)

我做了类似的事情,没有将bean移植到Spring。它对我有用。

@ManagedBean(name="bean")
@ViewScoped  // actual jsf viewscoped only with javax.faces.viewscoped import
public class Bean implements
Serializable {


@ManagedProperty(value="#{appService}")   // Spring Manged Bean and singleton
private transient AppService appService;

  // Getting AppService Object which is singleton in the application during deserialization 
 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
          stream.defaultReadObject();
          FacesContext context = FacesContext.getCurrentInstance();
          appService = (AppService)context.getApplication()
                .evaluateExpressionGet(context, "#{appService}", AppService.class);
       }
}

答案 2 :(得分:2)

public class ViewScopeCallbackRegistrer implements ViewMapListener {

@SuppressWarnings("unchecked")
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (event instanceof PostConstructViewMapEvent) {
PostConstructViewMapEvent viewMapEvent = (PostConstructViewMapEvent) event;
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent();
viewRoot.getViewMap().put(ViewScope.VIEW_SCOPE_CALLBACKS,
new HashMap<String, Runnable>());
} else if (event instanceof PreDestroyViewMapEvent) {
PreDestroyViewMapEvent viewMapEvent = (PreDestroyViewMapEvent) event;
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent();
Map<String, Runnable> callbacks = (Map<String, Runnable>) viewRoot
.getViewMap().get(ViewScope.VIEW_SCOPE_CALLBACKS);
if (callbacks != null) {
for (Runnable c : callbacks.values()) {
c.run();
}
callbacks.clear();
}
}
}

@Override
public boolean isListenerForSource(Object source) {
return source instanceof UIViewRoot;
}

}

答案 3 :(得分:1)

public class ViewScope implements Scope {

public static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks";

@Override
public synchronized Object get(String name, ObjectFactory<?> objectFactory) {
Object instance = this.getViewMap().get(name);
if(instance == null){
instance = objectFactory.getObject();
this.getViewMap().put(name, instance);
}
return instance;
}

@SuppressWarnings("unchecked")
@Override
public Object remove(String name) {
Object instance = this.getViewMap().remove(name);
if(instance == null){
Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);
if(callbacks != null)
callbacks.remove(name);
}
return instance;
}

/**
* Responsável por registrar uma chamada de destruição ao bean
* que será armazenadano [b]viewMap[/b] da [b]ViewRoot[/b](nossa página que será mostrada)
* @see #getViewMap()
* @param name - nome do bean
* @param runnable
*/  
@SuppressWarnings("unchecked")
@Override
public void registerDestructionCallback(String name, Runnable runnable) {
Map<String, Runnable> callbacks = (Map<String, Runnable>) this.getViewMap().get(VIEW_SCOPE_CALLBACKS);
if(callbacks != null)
callbacks.put(name, runnable);
}

@Override
public Object resolveContextualObject(String key) {
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext);
return facesResquestAttributes.resolveReference(key);
}

@Override
public String getConversationId() {
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesRequestAttributes facesResquestAttributes = new FacesRequestAttributes(facesContext);  
return facesResquestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId();
}

private Map<String, Object> getViewMap(){
return FacesContext.getCurrentInstance().getViewRoot().getViewMap();
}

}

答案 4 :(得分:-2)

我已经尝试过为Jsf 2.1&amp;的Jsf视图bean内存泄漏问题而解决的问题。 Jsf 2.2。请尝试以下链接Memory leak with ViewScoped bean?中的代码。它将在导航到下一页时清除会话中的视图bean。