How to mix annotations with faces-config.xml

爷,独闯天下 提交于 2019-12-03 13:55:24

问题


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.

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


回答1:


Annotated beans will fail in the following cases:

  1. /WEB-INF/faces-config.xml is not declared to conform to JSF 2.0.

  2. @ManagedBean is of javax.annotation package instead of javax.faces.bean.

  3. Bean class is not been compiled/built into WAR's /WEB-INF/classes.

  4. Bean is packaged in a JAR file which is missing /META-INF/faces-config.xml.

  5. 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.

  6. Webapp is actually using JSF 1.x libs (you can read JSF version in server startup log).



来源:https://stackoverflow.com/questions/7519151/how-to-mix-annotations-with-faces-config-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!