JSF 2.2 ViewScoped

JSF 2.2 has the missing ViewScoped annotation working with CDI. Simply use @javax.faces.view.ViewScoped on your bean. Be careful NOT to mix up with @javax.faces.bean.ViewScoped.

Example view scoped class:

If you use the wrong annotation you will get weld errors when trying to inject an instance of the class.

JSF SelectOneMenu with Nothing Selected

To add “nothing selected” to a dynamic h:selectOneMenu you can use the noSelectionOption=”true” attribute like this:

 

 

Injectable FacesContext

When you write a producer for FacesContext like this

do not forget to specify a scope! Otherwise you will get an Exception when accessing the object when it is injected in a session scoped bean. So the correct version looks like this:

JSF Form Auth with h:form

Using form based authentication in JSF always makes me look for a login form that uses h:form instead of plain html. Here is a form that uses JAAS with proper xhtml using the JSF h:form tag:

 

ui:repeat bug in JSF 2.2

I found a strange behavior in JSF 2.2: When using the new jcp namespaces for ui:repeat it renders something like this:

To get rid of this I went back to the old java.sun.com/jsf urls in the whole project and everything was working as expected.

Here is how I was able to reproduce this with glassfish4:

XHTML:

Controller:

When I change the ui namespace to the old http://java.sun.com/jsf/facelets everything works as expected.

GET Parameter in JSF

You can read GET parameters with the RequestParameterMap as follows:

JBoss AS7, JSF, Get Logged in User

if you use JAAS to login to your application, you are able to get the active user(name) the following way:

JEE6 CDI and JSF annotation hell

If you are in a JEE6 environment with CDI and JSF be very careful with choosing annotations. Always prefer the ones from javax.enterprise.context.* if there are also the same ones under javax.faces.. as JSF you surely run into troubles if you mix them. CDI annoteted beans are managed by the container and beans annotated with the annotations from faces are managed by the JSF container. E.g. this can result in SessionScoped beans that are initialized more than once for a single session.

Generally I think you should not use the @ManagedBean annotation in a JEE6 environment. I prefer @Named.