How to mix annotations with faces-config.xml
Using JBoss 6.0.0.Final, Richfaces 3.3.3.Final, MyFaces 2.0.6, facelets 1.1.15.B1 (a limitation of RF 3).
I'm on a legacy project which contains hundreds of beans defined in faces-config.xml
. I'd like to keep those defined in faces-config.xml but use annotations for new beans. However, when I've tried this I've not had success. The beans defined by annotation i.e.
@M开发者_StackOverflow中文版anagedBean
@ViewScoped
public class Foobar implements Serializable {
// ...
}
The bean is not accessible from my JSF page. I believe I've specified the 2.0 version in my faces-config.xml by using the proper header.
<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">
Is there anything else I need to do in the faces-config.xml
to allow annotations to also be used?
Annotated beans will fail in the following cases:
/WEB-INF/faces-config.xml
is not declared to conform to JSF 2.0.@ManagedBean
is ofjavax.annotation
package instead ofjavax.faces.bean
.Bean class is not been compiled/built into WAR's
/WEB-INF/classes
.Bean is packaged in a JAR file which is missing
/META-INF/faces-config.xml
.A wrong managed bean name is being used in EL, it should be the bean class name with 1st character lower cased according Javabeans spec. So in your particular example,
#{fooBar}
should work, but#{FooBar}
won't.Webapp is actually using JSF 1.x libs (you can read JSF version in server startup log).
精彩评论