Category Archives: JSF

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:

import javax.faces.view.ViewScoped;
import javax.inject.Named;

public class SenderView implements Serializable {

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:

<h:selectOneMenu value="#{myBean.someItem}">
    <f:selectItem itemLabel="---" noSelectionOption="true" />
    <f:selectItems value={myBean.items}" />



Injectable FacesContext

When you write a producer for FacesContext like this

public FacesContext produce() {
  return FacesContext.getCurrentInstance();

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:

public FacesContext produce() {
  return FacesContext.getCurrentInstance();

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:

<h:form id="login" onsubmit="document.getElementById('login').action='j_security_check';" prependId="false">
	<h:outputLabel for="j_username" value="Username:" />
	<h:inputText id="j_username" size="32" />

	<h:outputLabel for="j_password" value="Password:" />
	<h:inputSecret id="j_password" size="255" />

	<h:commandButton id="submit" value="Login" ajax="false" />


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 urls in the whole project and everything was working as expected.

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


<ui:composition xmlns=""
    <ui:repeat value="#{sessionController.testString}" var="v">


public class SessionController implements Serializable {
    public List<String> getTestString() {
        return Arrays.asList(new String[] {"a", "b", "c"});

When I change the ui namespace to the old everything works as expected.

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:

final Principal principal = FacesContext.getCurrentInstance()
if (null == principal) {
    return null;
return principal.getName();

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.