5

I have a Session scoped bean

import javax.faces.bean.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class SessionBean implements Serializable{

I inyect the object in one Filter...

public class FiltroSeguridad implements Filter{

@Inject
private SessionBean sessionBean;

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  HttpServletRequest httpRequest = (HttpServletRequest) request;
  sessionBean.setRutaRedirect(httpRequest.getRequestURI());
}
}

But, I inyect SessionBean in the next interaction...

@Model
public class CuentaUsuarioWebBean implements Serializable{
 @Inject
 private SessionBean sessionBean;

public void loginUsuario() throws IOException{
   sessionBean.getRutaRedirect();
}

}

But the property getRutaRedirect() returns null

I change the import by CDI annotations it still doesn't work (javax.enterprise.context.SessionScoped), same with JSF annotation (javax.faces.bean.ManagedBean and @ManagedProperty).

Thanks.

PD: Sorry for my English!

1
  • 1
    This works only if you have a /WEB-INF/beans.xml file (which a bit sane IDE would already autogenerate when you choose the CDI facet and/or which is already mentioned in a bit sane CDI tutorial, if you've read a tutorial). Do you have this file?
    – BalusC
    Jan 21, 2013 at 2:32

2 Answers 2

30

You can't mix annotations from those two packages you're using javax.faces.bean.SessionScoped for JSF, and import javax.inject.Named for CDI. Both reflect different injection mechanisms and cannot be mixed on the same bean. You have to pick both annotations(for Injection and Bean Scoping) from the same package. Use the following sets from their corresponding packages

For CDI-based bean definitions

javax.enterprise.context.SessionScoped //for bean scoping
javax.inject.Named //for bean declaration
javax.inject.Inject //for injection

For JSF-based bean definitions

javax.faces.bean.SessionScoped //for bean scoping
javax.faces.bean.ManagedBean //for bean declaration
javax.faces.bean.ManagedProperty //for bean injection

EDIT: Now I understand your requirements better, use the following construct to inject a JSF managed bean

 @ManagedProperty(value="#{yourBeanName}")
 SessionBean yourSessionBean;

Also note that within JSF, you can inject only beans of a wider scope than their targets e.g. you can inject a @SessionScoped bean into a @RequestScoped bean and not the other way around

But since JSF managed beans are deprecated, better to use the CDI managed ones. In that case you can inject shorter scoped beans in wider scoped ones

5
  • Hello, I try that. But dont works. I import javax.faces.bean.SessionScoped with javax.faces.bean.ManagedBean. I import import javax.inject.Named with javax.inject.Named but the bean dont change the property when I set a value. Thanks. Jan 20, 2013 at 22:18
  • @JuanPabloGómezUribe, you can't use @Inject for a JSF bean. @inject is only applicable to beans within the context of CDI. For beans under JSF, use @ManagedProperty. See updated answer
    – kolossus
    Jan 20, 2013 at 22:28
  • Hi, thanks for reply. I prove with JSF-Based bean definitions and try inject the bean SessionBean with @ManagedProperty(value="#{sessionBean}"), but this is injected as null (the bean have set and get). And use de CDI-Based and have the initial problem. Thanks. Jan 21, 2013 at 1:10
  • @JuanPabloGómezUribe what's your environment(JSF version and App server)?
    – kolossus
    Jan 21, 2013 at 1:50
  • Hi, JSF 2.0 and GlassFish 3.1 Jan 21, 2013 at 13:00
5

Forget about managed beans. It won't work with a Filter that way. If you insist on using it then do it properly by follow the answer provided here:

How implement a login filter in JSF?

Now regarding CDI, if you problem is that a specific value is null and you have verified this by actually getting a NPE or so (Since for example Eclipse debugger sometimes get's it wrong). Then double check that you used correct SessionScoped like described by kolossus and also check what BalusC said (beans.xml).

A good way to see if CDI is working is to ask the manager for the bean. Put this debug code somewhere and try it:

@Inject
BeanManager manager;

@PostConstruct
private void test() {
    Bean<?> bean = (Bean) manager.resolve(manager.getBeans("ANY_NAMED_BEAN_EL_NAME"));
        System.out.println(bean);

}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.