As you probably know JSF 2.2 comes with a
solid support for injection in JSF artifacts. However, some artifacts were left
out, and here we are talking about converters, validators and behaviors.
Starting with JSF 2.3 this gap has been filled and we can inject (use @Inject) in
@FacesConverter (javax.faces.convert.Converter),
@FacesValidator (javax.faces.validator.Validator) and @FacesBehavior
(javax.faces.component.behavior.Behavior).
JSF 2.3 is currently available as a milestone
only (Mojarra 2.3.0 Milestone 4), and in order to test this facility you need
to:
- specify a new
attribute called managed on the corresponding annotations;
- add in WEB-INF the faces-config.xml with JSF 2.3 XSD;
So, for a converter,
we have:
@FacesConverter(value = "fooConverter", managed = true)
public class FooConverter
implements Converter {
@Inject
// artifact
@Override
public Object
getAsObject(FacesContext context, UIComponent component, String value) {
...
// use the injected artifact
...
}
@Override
public String
getAsString(FacesContext context, UIComponent component, Object value) {
...
// use the injected artifact
...
}
}
You can see a complete simple example here.
I've tested under Payara 4.1.1 with Mojarra 2.3.0 Milestone 4.
For a validator,
we have:
@FacesValidator(value = "fooValidator", managed = true)
public class FooValidator
implements Validator {
@Inject
// artifact
@Override
public void
validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException
{
...
// use the injected artifact
...
}
}
You can see a complete simple example here.
I've tested under Payara 4.1.1 with Mojarra 2.3.0 Milestone 4.
For a behavior,
we have:
@FacesBehavior(value = "fooBehavior", managed = true)
public class FooBehavior extends ClientBehaviorBase {
@Inject
// artifact
@Override
public String
getScript(ClientBehaviorContext behaviorContext) {
...
// use the injected artifact
...
}
}
You can see a complete simple example here.
I've tested under Payara 4.1.1 with Mojarra 2.3.0 Milestone 4.
In all three cases you need the JSF
2.3 XSD, 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">
</faces-config>
Niciun comentariu :
Trimiteți un comentariu