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

marți, 10 februarie 2015

JSF 2.3 - Using a CDI managed Validator / Converter

Using @Inject in JSF custom validators/converters can be very useful, but until JSF 2.3 was not supported. Nevertheless, in JSF 2.2.x (e.g. 2.2.7) you can use @Inject in a custom validator, as below:

import javax.inject.Inject;
...
@FacesValidator(value="myValidator")
public class MyValidator implements Validator {

    @Inject
    SomeBean someBean;
    ...
}

And, in a custom converter also:

import javax.inject.Inject;
...
@FacesConverter(value="myConverter")
public class MyConverter implements Converter{
   
    @Inject
    SomeBean someBean;

    ...
}

Even if the above approaches work fine, there are not listed (recommended to be used) as JSF 2.2 features. But, starting with JSF 2.3, validators and converters can be managed by CDI. For this, we just use a flag attribute named, managed, as below:

import javax.inject.Inject;
...
@FacesValidator(value="myValidator", managed=true)
public class MyValidator implements Validator {

    @Inject
    SomeBean someBean;
    ...
}

import javax.inject.Inject;
...
@FacesConverter(value="myConverter", managed=true)
public class MyConverter implements Converter{
   
    @Inject
    SomeBean someBean;

    ...
}

So, if the validators/converters will be managed by CDI, we can safely use the @Inject.

You can read more about this on this Java EE 8 ZEEF page and on this Java.net (CDI Validator | CDI Converter).

Until JSF 2.3 will be officially released, you can follow JSF 2.3 development via GitHub mirror, and/or try to build JSF Jars from SVN.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors