6

@FacesConverter and @FacesValidator are not eligible for EJB or managed bean injection points up to JSF 2.2.

They are supposed to work with JSF 2.3 (currently available as a milestone only) using an additional managed attribute with @FacesConverter and @FacesValidator as mentioned here.

In JSF 2.1 very few JSF artifacts were injection targets. In JSF 2.2 injection was made possible in a huge amount of additional artefacts but the very ones where injection actually matters most, converters and validators, were mysteriously left in the cold.

In JSF 2.3 this has now finally been taken care of as the following artefacts have been added to the list of injection targets:

  • javax.faces.convert.Converter
  • javax.faces.validator.Validator
  • javax.faces.component.behavior.Behavior

However, in contrast to the artefacts already on this list these new 3 are not automatically injection targets. They will only become so when a new attribute called "managed" on the corresponding annotations @FacesConverter, @FacesValidator and @Behavior is set to true. Furthermore all these 3 annotations have been upgraded to being CDI qualifiers by adding the @Qualified annotation to their definition.

The existing attributes of @FacesConverter, @FacesValidator and @Behavior have not been modified, meaning they are all binding, as is the new attribute "managed".

The attribute managed is however, unavailable in Mojarra 2.3.0-m02.

Is it because of a milestone? Is it dependent upon a specific Weld/CDI version? I am currently going with GlassFish Server 4.1. Different artifact versions are mentioned here (The default Weld version supplied by the server version is 2.2.2 final).

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 1
    Just tried on Tomcat 8.0.21 + Weld 2.2.10 + Mojarra 2.3.0-m02 and I can see that it failed when `faces-config.xml` is still declared conform JSF 2.2. After I updated it to JSF 2.3, it works. Is your `faces-config.xml` declared conform JSF 2.3? I.e. `/web-facesconfig_2_3.xsd` and `version="2.3"` respectively (if the IDE errors on the XSD being unavailable, just ignore it). – BalusC May 04 '15 at 11:18
  • With `/web-facesconfig_2_3.xsd` and `version="2.3"`, the browser shows 500 (Internal Server Error). This is [untraceable](http://stackoverflow.com/q/27795529/1391249) in my real app. This however, works in a project with a single XHTML file where the IDE also shows `@FacesConverter(managed = false)` and `@FacesValidator(managed = false)` as default which in the real app is however, a compile-time error - `cannot find symbol symbol: method managed() location: @interface FacesValidator`. – Tiny May 04 '15 at 11:42
  • I posted an answer. As to your "real app", I suspect classpath pollution with an older versioned JSF API (not impl), perhaps via something like `javaee.jar`? – BalusC May 04 '15 at 11:56

1 Answers1

6

As you can see in Mojarra 2.3.0-m02's Application#createConverter() implementation, it checks if it's running in JSF 2.3 mode as per faces-config.xml version declaration before trying to grab a CDI-managed one.

In other words, in order to get @FacesConverter(managed=true), @FacesValidator(managed=true) and thus @Inject in those classes to work, you need to make sure that your webapp's faces-config.xml is declared conform JSF 2.3 as below:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">

    <!-- Config here. -->

</faces-config>

Noted should be that the IDE may error on the JSF 2.3 XSD file not being publicly available (yet), you can safely ignore that part, it won't be validated during runtime by JSF. You could alternatively stick to 2.2 XSD and ignore any IDE warning/error on an unsupported version.


The jdevelopment.nl blog author has been notified about this and he will make sure that in a next update the above is clarified in blog as well.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The IDE (NetBeans 8.0.2) also does not issue any error in a playground project. Therefore, the problem with the real application is a story apart and it is something to dig further in. Thank you. – Tiny May 04 '15 at 12:05
  • Small side-note: Don't forget the `web-facesconfig_2_2.xsd` part to be upgraded to `web-facesconfig_2_3.xsd` as well. :-) Meanwhile, you can do "manual" lookup by using `CDI.current().select(SomeBackingBean.class).get()`: https://stackoverflow.com/questions/24798529/how-to-programmatically-inject-a-java-cdi-managed-bean-into-a-local-variable-in – Roland Nov 02 '17 at 11:04