@Inject to pass params to a CDI @Named bean via URL

This works only with the in JSF 2.3 introduced javax.faces.annotation.ManagedProperty. @Inject @ManagedProperty(“#{param.id}”) private String id; The now deprecated javax.faces.bean.ManagedProperty annotation works only in JSF @ManagedBean classes. I.e. in instances which are managed by JSF. It does not work in instances which are managed by CDI @Named. Further, you’ve made another mistake: you’re trying to prepare … Read more

Accessing injected dependency in managed bean constructor causes NullPointerException

Injection can only take place after construction simply because before construction there’s no eligible injection target. Imagine the following fictive example: UserInfoBean userInfoBean; UserDao userDao = new UserDao(); userInfoBean.setDao(userDao); // Injection takes place. userInfoBean = new UserInfoBean(); // Constructor invoked. This is technically simply not possible. In reality the following is what is happening: UserInfoBean … Read more

ViewParam vs @ManagedProperty(value = “#{param.id}”)

<f:viewParam>: Sets the value during update model values phase only (since it extends UIInput). The set value is not available during @PostConstruct, so you need an additional <f:event type=”preRenderView” listener=”#{bean.init}” /> inside the <f:metadata> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <f:viewAction> for that instead. Allows for nested … Read more