Spring 中的 JSF 视图范围 [英] JSF View scope in Spring

查看:20
本文介绍了Spring 中的 JSF 视图范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3.0 中是否有类似 JSF @ViewScoped 的作用域?我有一个使用 JSF+Spring 的应用程序,其中支持 bean 由 Spring 管理.我在 Spring 中没有找到像 JSF wiew 范围这样的任何范围.我看到博客 将 JSF 2.0 的 ViewScope 移植到 Spring 3.0,但它对我不起作用.

Is there any scope like JSF @ViewScoped in Spring 3.0? I have an application using JSF+Spring where backing beans are managed by Spring. I didn't find any scope like JSF wiew scope in Spring. I saw the blog Porting JSF 2.0’s ViewScope to Spring 3.0, but it didn't work for me.

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

Here's my attempt on the custom Spring scope:

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>

推荐答案

最近我创建了 maven artifact 来解决这个问题.

Recently I've created maven artifact which will solve this problem.

查看我的 github javaplugs/spring-jsf 存储库.

See my github javaplugs/spring-jsf repository.

这篇关于Spring 中的 JSF 视图范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆