简体   繁体   中英

CDI @ViewScoped bean functionality unavailable

I am using JSF 2.2.14 with Spring Boot 1.4.4 and I have defined a custom view scope as follows:

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;
    }
}

and registered it in the Spring Boot main class as follows :

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

when managing my view scope bean with spring as follows:

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

the page works fine but I get the warning:

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

I get this warning only the first time I access the page, so I am concerned if this warning means that I am doing something wrong or may cause problems in the future.

You just have to add these depency on pom.xml of your maven project:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM