【问题标题】:CDI @ViewScoped bean functionality unavailableCDI @ViewScoped bean 功能不可用
【发布时间】:2017-01-28 08:34:48
【问题描述】:

我将 JSF 2.2.14 与 Spring Boot 1.4.4 一起使用,并且我定义了一个自定义视图范围,如下所示:

public class FacesViewScope implements Scope {

    public static final String NAME = "view";

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext == null) {
            throw new IllegalStateException("FacesContext.getCurrentInstance() returned 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;
        }
    }

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

    @Override
    public String getConversationId() {
        return null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
        // Not supported by JSF for view scope
    }

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

并在Spring Boot主类中注册如下:

 @Bean
        public static CustomScopeConfigurer customScopeConfigurer() {
            CustomScopeConfigurer configurer = new CustomScopeConfigurer();
            configurer.setScopes(Collections.<String, Object>singletonMap(
                    FacesViewScope.NAME, new FacesViewScope()));
            return configurer;
        }

使用 spring 管理我的视图范围 bean 时如下:

@Component("testBean")
@Scope("view")

页面运行良好,但我收到警告:

c.s.f.application.view.ViewScopeManager  : CDI @ViewScoped bean functionality unavailable

我仅在第一次访问该页面时收到此警告,因此我担心此警告是否意味着我做错了事或将来可能会导致问题。

【问题讨论】:

    标签: spring jsf spring-boot jsf-2.2


    【解决方案1】:

    您只需在 maven 项目的 pom.xml 中添加这些依赖项:

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>
    

    【讨论】:

    • 为什么我们需要添加这个新的依赖?
    • 将 javax.faces 从 2.2.6 升级到 2.2.20 由于某种原因破坏了 ViewScoped,添加 cdi-api 修复了它,感谢 @Cleo
    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2011-12-10
    • 2015-05-02
    • 1970-01-01
    • 2019-04-10
    相关资源
    最近更新 更多