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!
Posted at 07:45AM Jul 22, 2012
Posted by: Ralph
Category: General
You have 2 issues:
1) Fix your first managed bean annotation as follows:
@ManagedBean(name="loginMB")
@SessionScoped
public class LoginMB {
...
}
2) The code snippet which has the ManagedProperty annotation has to be inside a second managed bean as well.
Good Luck
Posted by Sam Mohamed on October 10, 2012 at 02:42 PM GMT #
Thanx! This safedmy day.
Posted by Groady on October 16, 2012 at 03:44 PM GMT #