我有我的豆子:
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"; } }
而我的工厂:
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(); } }
当我运行我的应用程序时,我得到这个例外:
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)
当我将produce方法更改为@RequestScoped时,我的FacesContext只注入一次,而在我的第二页xhtml中,我得到一个nullpointer =(
更新:pom
4.0.0 br.com.dropper dropper-web 0.1 war javax javaee-api 7.0 provided javax javaee-api provided dropper-web bootstrap 1.0.10 system ${project.basedir}/src/main/webapp/WEB-INF/lib/bootstrap-1.0.10.jar dropper-web commons-io 2.5 system ${project.basedir}/src/main/webapp/WEB-INF/lib/commons-io-2.5.jar dropper-web cupertino 1.0.10 system ${project.basedir}/src/main/webapp/WEB-INF/lib/cupertino-1.0.10.jar dropper-web primefaces-6.0 6.0 system ${project.basedir}/src/main/webapp/WEB-INF/lib/primefaces-6.0.jar dropper-web org.apache.maven.plugins maven-compiler-plugin 3.1 1.8
ATT
事实FacesContext
本身并非如此Serializable
.更重要的是,绝对不是 @ViewScoped
.它实际上比实际上更短@RequestScoped
,但是直到JSF 2.3发布,它带有改进的CDI支持,所以你可以在@Inject FacesContext
没有自定义生产者的情况下,可以更少或更安全地使用它@Produces @RequestScoped
.
@Produces @RequestScoped public FacesContext getFacesContext(){ return FacesContext.getCurrentInstance(); }
这种情况仍然会失败的一种情况是当您使用RequestDispatcher#forward()
或在同一请求中明确执行转发时ExternalContext#dispatch()
.然后,您将在com.sun.faces.context.FacesContextImpl.assertNotReleased中面对java.lang.IllegalStateException.然而,这是一种非常罕见的情况,通常只在设计糟糕的(ajax)异常处理程序中执行.另请参阅使用JSF错误处理程序中的ExternalContext.dispatch导致损坏的页面呈现.