How do I force an application-scoped bean to instantiate at application startup?

My first thought was to use an old style ServletContextListener contextInitialized() method and from there use an ELResolver to manually request the instance of my managed bean (thus forcing the initialization to happen). Unfortunately, I can’t use an ELResolver to trigger the initialization at this stage because the ELResolver needs a FacesContext and the FacesContext … Read more

Field.get(obj) returns all nulls on injected CDI managed beans, while manually invoking getters return correct values

That’s indeed expected behavior. The CDI managed bean instance is in essence a serializable proxy instance of an autogenerated class which extends the original backing bean class and delegates in all public methods further to the actual instance via public methods (like as how EJBs work). The autogenerated class looks roughly like this: public CDIManagedBeanProxy … Read more

How and when is a @ViewScoped bean destroyed in JSF?

It will be destroyed when a postback with a non-null outcome is been performed, or, the number of (logical) views in session has exceeded and the particular view is the first one in LRU chain (in Mojarra, that’s configureable by com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews context parameters, each with a default value of 15), or, the number … Read more

JSF managed-bean EJB injection

You are mixing the responsibilities of EJBs and JSF managed beans. The faces-config.xml registers only JSF artifacts, such as managed beans and not EJBs. Your registration in faces-config.xml <managed-bean> <managed-bean-name>personManager</managed-bean-name> <managed-bean-class>ejb.PersonManager</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> does basically exactly the same as @ManagedBean @SessionScoped public class PersonManager { // … } In other words, you’re registering the class … 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

How to pass JavaScript variables as parameters to JSF action method?

Let JS set them as hidden input values in the same form. <h:form id=”formId”> <h:inputHidden id=”x” value=”#{bean.x}” /> <h:inputHidden id=”y” value=”#{bean.y}” /> <h:commandButton value=”submit” onclick=”getVars()” action=”#{bean.submit}” /> </h:form> function getVars() { // … var x = locationInfo.lng; var y = locationInfo.lat; document.getElementById(“formId:x”).value = x; document.getElementById(“formId:y”).value = y; } The command button action method could just … Read more

Why are there different bean management annotations

javax.enterprise.context.SessionScoped(JSR 346) and all other annotations under the javax.enterprise.context.* package maintain the context of CDI. CDI provides an alternative, versatile and more powerful mechanism for dependency injection, bean and general resource management within the Java EE space. It’s an alternative to JSF managed beans and it’s set to even supersede the JSF bean management mechanism … Read more

How to invalidate session in JSF 2.0?

Firstly, is this method correct? Is there a way without touching the ServletAPI? You can use ExternalContext#invalidateSession() to invalidate the session without the need to grab the Servlet API. @ManagedBean @SessionScoped public class UserManager { private User current; public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return “/home.xhtml?faces-redirect=true”; } // … } what will happen to my current … Read more

Calling a JavaScript function from managed bean

PrimeFaces 6.2+ Use PrimeFaces#executeScript(): public void submit() { // … PrimeFaces.current().executeScript(“alert(‘peek-a-boo’);”); } NOTE: works only when submit() is invoked by Ajax. PrimeFaces 6.2- Use RequestContext#execute(): public void submit() { // … RequestContext.getCurrentInstance().execute(“alert(‘peek-a-boo’);”); } NOTE: works only when submit() is invoked by Ajax. JSF 2.3+ Use PartialViewContext#getEvalScripts(): public void submit() { // … FacesContext.getCurrentInstance().getPartialViewContext().getEvalScripts().add(“alert(‘peek-a-boo’);”); } NOTE: … Read more