首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CDI注射FacesContext

CDI注射FacesContext
EN

Stack Overflow用户
提问于 2017-01-19 00:49:41
回答 1查看 6.1K关注 0票数 8

我有我的豆:

代码语言:javascript
复制
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import br.com.dropper.web.dao.UsuarioDAO;
import br.com.dropper.web.model.Usuario;
import br.com.dropper.web.util.JpaUtil;

@Named
@SessionScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private FacesContext context;

    @Inject
    private Usuario usuario;


    //TODO: Persistencia e Transacao controladas por EJB
    private EntityManager em = new JpaUtil().getEntityManager();
    private UsuarioDAO usuarioDAO = new UsuarioDAO(em);



    public Usuario getUsuario() {
        return usuario;
    }

    public String autenticar() {

        Usuario usuario = usuarioDAO.obterUsuarioPorEmail(this.usuario);

        if (usuario == null) {
            context.addMessage(null, new FacesMessage("Usuario não encontrado."));
            context.getExternalContext().getFlash().setKeepMessages(true);
            return "login?faces-redirect=true";
        } else {
            context.getExternalContext().getSessionMap().put("usuarioLogado", usuario);
            return "dashboardImagem.xhtml?faces-redirect=true";
        }
    }

    public String cadastrarUsuario() {
        System.out.println("Redirecionando para cadastroUsuario.xhtml");
        return "cadastroUsuario.xhtml?faces-redirect=true";
    }

    public String logout() {
        context.getExternalContext().getSessionMap().remove("usuarioLogado");
        context.getExternalContext().invalidateSession();
        return "login.xhtml?faces-redirect=true";

    }

}

我的工厂:

代码语言:javascript
复制
package br.com.dropper.web.factory;

import java.io.Serializable;

import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;

public class FacesContextFactory implements Serializable{

    private static final long serialVersionUID = 1L;

    @Produces
    @ViewScoped
    public FacesContext getFacesContext(){
        return FacesContext.getCurrentInstance();
    }

}

当我运行我的应用程序时,我得到了这个异常:

代码语言:javascript
复制
Caused by: org.jboss.weld.exceptions.IllegalProductException: WELD-000053: Producers cannot declare passivating scope and return a non-serializable class: Producer for Producer Method [FacesContext] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ViewScoped public br.com.dropper.web.factory.FacesContextFactory.getFacesContext()] declared on Managed Bean [class br.com.dropper.web.factory.FacesContextFactory] with qualifiers [@Any @Default]
    at br.com.dropper.web.factory.FacesContextFactory.getFacesContext(FacesContextFactory.java:16)

当我将produces方法更改为@RequestScoped时,我的FacesContext只注入一次,在我的第二页xhtml中,我得到了一个null指针=(

最新情况: pom

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.dropper</groupId>
    <artifactId>dropper-web</artifactId>
    <version>0.1</version>
    <packaging>war</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- <dependency>
            <groupId>dropper-web</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1212</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/postgresql-9.4.1212.jre6.jar</systemPath>
        </dependency>  -->

        <dependency>
            <groupId>dropper-web</groupId>
            <artifactId>bootstrap</artifactId>
            <version>1.0.10</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/bootstrap-1.0.10.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>dropper-web</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/commons-io-2.5.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>dropper-web</groupId>
            <artifactId>cupertino</artifactId>
            <version>1.0.10</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cupertino-1.0.10.jar</systemPath>
        </dependency>


        <dependency>
            <groupId>dropper-web</groupId>
            <artifactId>primefaces-6.0</artifactId>
            <version>6.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/primefaces-6.0.jar</systemPath>
        </dependency>

    </dependencies>


    <build>
        <finalName>dropper-web</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Att

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 09:34:33

FacesContext本身确实不是Serializable。更重要的是,绝对不是 @ViewScoped。它实际上比@RequestScoped短,但是在JSF2.3发布之前--这是改进的CDI支持附带的--这样您就可以在没有定制生产者的情况下只使用@Inject FacesContext,您可以更少地或者更安全地使用@Produces @RequestScoped

代码语言:javascript
复制
@Produces
@RequestScoped
public FacesContext getFacesContext(){
    return FacesContext.getCurrentInstance();
}

当您使用RequestDispatcher#forward()ExternalContext#dispatch()在同一个请求中显式地执行转发时,这仍然会失败。然后你将面对com.sun.faces.context.FacesContextImpl.assertNotReleased的java.lang.IllegalStateException。然而,这是一种非常罕见的情况,通常只在设计糟糕的(ajax)异常处理程序中执行。另见a.o.。在JSF错误处理程序中使用ExternalContext.dispatch会导致页面呈现损坏

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41731978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档