@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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With