我有一份工作@ManagedBean
,我想用@Named + @RequestScoped
豆子代替。
// Before
@ManagedBean
public class Login {
...
}
// After
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class Login {
...
}
只要我使用@ManagedBean
. @Named
没有@RequestScoped
工作,但为每个 EL 表达式创建一个新实例。@Named + @RequestScoped
但是会产生异常:当请求缓存不活动时,无法添加请求范围的缓存项
java.lang.IllegalStateException: Unable to add request scoped cache item when request cache is not active
at org.jboss.weld.context.cache.RequestScopedBeanCache.addItem(RequestScopedBeanCache.java:51)
at de.prosis.dafe.presentation.Login$Proxy$_$$_WeldClientProxy.getUsername(Login$Proxy$_$$_WeldClientProxy.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
...
空beans.xml
确实存在。是否有任何我不知道的陷阱或错误?我敢打赌我错过了一些东西,但它看起来确实像一个错误。我用谷歌搜索了异常消息,但除了引发它的类的来源之外什么也没找到。提前致谢!
更新: 它与我省略的代码有关。登录 bean 尝试在其构造函数中使会话无效,如果它是命名 bean(并非总是如此,如果我向 F5 发送垃圾邮件,它似乎在几次后工作)并作为托管 bean 工作。有人可以解释这种行为吗?
public Login() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
if (session != null) {
session.invalidate();
}
}