@Named + @RequestScoped not working in JSF 2.0 with JBoss 7.1.1

I have a working @ManagedBean which I'd like to substitute with a @Named + @RequestScoped bean.

// Before
@ManagedBean
public class Login {
   ...
}

// After
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class Login {
   ...
}

Everything works fine as long as I use @ManagedBean . @Named without @RequestScoped works but creates a new instance for each EL-expression. @Named + @RequestScoped however yields an exception: Unable to add request scoped cache item when request cache is not active

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)
    ...

Empty beans.xml does exist. Are there any gotchas or bugs that I'm not aware of? I bet I'm missing something but it does look like a bug. I googled the exception message but haven't found anything but the source of the class which raises it. Thanks in advance!

Update: It is related with code I omitted. The login bean tries to invalidate the session in its constructor, which fails if it is a named bean (not always, if I spam F5 it seems to works after a few times) and works as a managed bean. Can somebody explain that behaviour?

public Login() {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            if (session != null) {
                session.invalidate();
            }
} 

Code comes from that tutorial: http://www.greenkode.com/2011/09/user-authentication-and-authorization-using-jaas-and-servlet-3-0-login/


Fixed it by removing invalidation of the session in the constructor. I still don't know why @ManagedBean works and @Named does not.


I had the same problem. Switched from @ManagedBean (which was working) to @Named which didn't work. I had a beans.xml file in WEB-INF which didn't solve the problem like most people suggest. You can add @Stateless along with @Named to make it work for reasons I don't know! If anyone can explain that I would love to hear it.

Anyway, I am guessing the 'real' way to make @Named work is this: @Named will work if you import the correct @RequestScoped annotation; from the javax.enterprise.context package. The @RequestScoped annotation from the javax.faces.bean package is not compatible with the @Named annotation. If you omit the correct @RequestScoped from being alongside @Named the bean will fire but it won't read any properties. This is a very late reply but I hope it helps anyone else miffed by this.

ps. I use Glassfish so I doubt it's a JBoss problem.

Alan


I reckon that you are either outside of one of these scenarios or that JBoss AS 7 has another issues like this one.

BTW, a request scoped bean is not required to be serializable. Probably @SteveTaylor mixed it with session scoped beans.

链接地址: http://www.djcxy.com/p/60610.html

上一篇: Win2008 R2 WER(错误报告)LocalDumps被什么拦截?

下一篇: @Named + @RequestScoped在JBoss 7.1.1的JSF 2.0中不起作用