My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 22 noiembrie 2015

JSF 2.3 Converters, validators and behaviors as injection targets

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors