How can I get a message bundle string from inside a managed bean?

You can get the full qualified bundle name of <message-bundle> by Application#getMessageBundle(). You can get the current locale by UIViewRoot#getLocale(). You can get a ResourceBundle out of a full qualified bundle name and the locale by ResourceBundle#getBundle(). So, summarized: FacesContext facesContext = FacesContext.getCurrentInstance(); String messageBundleName = facesContext.getApplication().getMessageBundle(); Locale locale = facesContext.getViewRoot().getLocale(); ResourceBundle bundle = ResourceBundle.getBundle(messageBundleName, … Read more

Calling Primefaces dialog box from Managed Bean function

You can, by using the RequestContext (or PrimeFaces if you are using the version 6.2 or higher) class. Suppose you have the following: <p:dialog id=”myDialogID” widgetVar=”myDialogVar”> …. </p:dialog> So the way you do in the facelet itself, i.e. onclick=myDialogVar.show();, the same can be done in your managed bean like so: For PrimeFaces <= 3.x RequestContext … Read more

JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view?

I’ll assume that the session scope is not an option, otherwise this question makes little sense. You can do it using Tomahawk <t:saveState>. Add the following line somewhere to the page: <t:saveState value=”#{bean}” /> RichFaces <a4j:keepAlive> does also the same: <a4j:keepAlive beanName=”#{bean}” /> Or if there is room, upgrade to at least JSF 2.x and … Read more

Read resource bundle properties in a managed bean

Assuming that you’ve configured it as follows: <resource-bundle> <base-name>com.example.i18n.text</base-name> <var>text</var> </resource-bundle> If your bean is request scoped, you can just inject the <resource-bundle> as @ManagedProperty by its <var>: @ManagedProperty(“#{text}”) private ResourceBundle text; public void someAction() { String someKey = text.getString(“some.key”); // … } Or if you just need some specific key: @ManagedProperty(“#{text[‘some.key’]}”) private String someKey; … Read more

How to set -Dorg.apache.el.parser.COERCE_TO_ZERO=false programmatically

You can set the system properties programmatically using System#setProperty(). System.setProperty(“org.apache.el.parser.COERCE_TO_ZERO”, “false”); However, you need to ensure that this is been set before JSF/EL ever get initialized. Best place would be a ServletContextListener. public class Config implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent event) { System.setProperty(“org.apache.el.parser.COERCE_TO_ZERO”, “false”); } @Override public void contextDestroyed(ServletContextEvent event) { // NOOP … Read more

JSF does not populate @Named @RequestScoped bean with submitted input values

From your bean: import javax.faces.bean.RequestScoped; import javax.inject.Named; @Named(“loginRequest”) @RequestScoped public class LoginRequest { You’re mixing CDI and JSF annotations. You can and should not do that. Use the one or the other. I don’t know what’s the book is telling you, but most likely you have chosen the wrong autocomplete suggestion during the import of … Read more

NullPointerException while trying to access @Inject bean in constructor

You’re expecting that the injected dependency is available before the bean is constructed. You’re expecting that it works like this: RequestBean requestBean; requestBean.sessionBean = sessionBean; // Injection. requestBean = new RequestBean(); // Constructor invoked. This is however not true and technically impossible. The dependencies are injected after construction. RequestBean requestBean; requestBean = new RequestBean(); // … Read more

How to reference JSF managed beans which are provided in a JAR file?

You need to have a JSF 2.0 compliant /META-INF/faces-config.xml file in the commons-web-1.0.jar file in order to get JSF to scan the JAR file for classes with JSF annotations like @ManagedBean and auto-register them. <?xml version=”1.0″ encoding=”UTF-8″?> <faces-config xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd” version=”2.0″> </faces-config> JSF does namely not scan every class of every single JAR … Read more

JSF managed bean naming conventions

There is no strict convention specified by JSF itself. I’ve seen the following conventions: FooBean FooBacking FooManager FooController FooManagedBean Or even just Foo which is then placed in a specific package like com.example.controller, com.example.backing or even com.example.view, etc. I myself tend to use FooManager for application and session scoped beans (e.g. DataManager, UserManager, LocaleManager, etc) … Read more