Initializing a JSF ManagedBean

Sometimes things really are easy – the fact that I Googled how to do this now seems pretty silly. The more I use JSF the more I like it 🙂

To initialize the state of the ManagedBean that a JSF page is using (like to preload data, or initialize other displayed values), just call the code from the bean’s constructor. Simple as that.

For example

@ManagedBean
public class ExampleController
{
    private String exampleProperty1;
    private String exampleProperty2;

    public ExampleController()
    {
    //example init code here, e.g. to init property values
    }

    ...

}

JSF error on Glassfish: PWC6228: #{…} not allowed in a template text body

If you forget to include the JSF taglibs in a JSP that’s part of a JSF app, you’ll get this rather obscure error on Glashfish 3.1.1:

WARNING: StandardWrapperValve[faces]: PWC1406: Servlet.service() for
servlet faces threw exception org.apache.jasper.JasperException:
/configure.jsp(13,34) PWC6228: #{...} not allowed in a template
text body.

 

Add the taglibs to fix:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

Edit:
I’ve subsequently realized in following some JSF tutorials that apparently there’s substantial difference between JSF 1.2 and 2.0, and that the errors I was running into were because I wrongly assumed JSF2.0 used JSP pages (it used XHTML Facelets instead), and so most of the issues I was running into were as a result of trying to do things the wrong way for 2.0.