« Eclipse Juno - JST &... | Main | Eclipse Juno - Maven... »

Why is my JSF 2.0 managed Bean not working....

Today I run again into a problem when a managed bean was not working in my new JSF 2.0 application. I added the bean into a page element using expression language. 

<h2>
   <h:outputText value="#{loginMB.remoteUser}" />
</h2>

But my bean was never called. The special part is that in my case the loginMB is part of an jar which was added as a library into the web module. The bean itself is annotated as followed:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
....
@ManagedBean
@SessionScoped
public class LoginMB {
...
}

In this szenario it is necessary to add an empty faces-config.xml descriptor into the META-INF folder of the jar. Otherwise the bean will not be detected by the web container:

<?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>

In different to the faces-config.xml file an empty bean.xml file is optional.

@ManagedProperty

If your managed bean injects other manged beans uses the annotation @ManagedProperty

 	@ManagedProperty(value = "#{loginMB}")
private LoginMB loginMB = null;

public LoginMB getLoginMB() {
return loginMB;
}

public void setLoginMB(LoginMB loginMB) {
this.loginMB = loginMB;
}


In this case a setter and getter method for the field 'loginMB' must be provided!