JSF 2.3 new features
By Constantin Alin
Latest updated version: JSF 2.3-m04
What we will cover? (I)
• JSF support for Iterable interface in UIData and UIRepeat
• JSF support for Map interface in UID...
What we will cover? (II)
• Converters, validators and behaviors as injection targets
• Generics support in Converter and V...
What we will cover? (III)
• Indefinitely Facelets cache during Production stage by default
• Align <f:convertDateTime/> to...
JSF support for Iterable interface
in UIData and UIRepeat
JSF support for Iterable interface in UIData (I)
Starting with JSF 2.2, UIData supports Iterable directly, which includes ...
JSF support for Iterable interface in UIRepeat (I)
In JSF 2.2 and prior versions, UIRepeat does not support Iterable direc...
JSF support for Iterable interface in UIRepeat (II)
2. Using OmniFaces API, setToList() (if EL 2.2 is not available):
<ui:...
JSF support for Iterable interface in UIRepeat (III)
Starting with JSF 2.3, UIRepeat will support Iterable directly.
JSF 2...
JSF support for Map interface in UIData
and UIRepeat
JSF support for Map interface in UIData (I)
In JSF 2.2 and prior versions, UIData does not support Map directly.
Workaroun...
JSF support for Map interface in UIData (II)
2. Using JSF 2.2 API (if EL 2.2 is available):
<h:dataTable value="#{fooBean....
JSF support for Map interface in UIData (III)
Starting with JSF 2.3, UIData will support Map directly.
JSF 2.3 example:
<h...
JSF support for Map interface in UIRepeat (I)
In JSF 2.2 and prior versions, UIRepeat does not support Map directly.
Worka...
JSF support for Map interface in UIRepeat (II)
2. Using OmniFaces API, mapToList() (if EL 2.2 is not available):
<ui:repea...
JSF support for Map interface in UIRepeat (III)
Starting with JSF 2.3, UIRepeat will support Map directly.
JSF 2.3 example...
JSF support for custom types in UIData
and UIRepeat
JSF support for custom types in UIData and UIRepeat (I)
Starting with JSF 2.3, custom DataModel wrappers can be registered...
JSF support for custom types in UIData and UIRepeat (II)
JSF 2.3 example:
// Wrapper model
@FacesDataModel(forClass = FooC...
JSF support for custom types in UIData and UIRepeat (III)
JSF 2.3 example (continued):
// Use collection in your beans
@Na...
JSF support for custom types in UIData and UIRepeat (IV)
JSF 2.3 example (continued):
// Use collection in <h:dataTable/> ...
Default CDI producers for the most
important JSF artifacts
Default CDI producers for the most important JSF artifacts (I)
Prior versions of JSF and JSF 2.2 uses static entry methods...
Default CDI producers for the most important JSF artifacts (II)
This approach has at least two disadvantages:
• the code b...
Default CDI producers for the most important JSF artifacts (III)
More JSF 2.2 examples:
Map<String, String> requestMap = F...
Default CDI producers for the most important JSF artifacts (IV)
The solution? Provide default producers for the most impor...
Default CDI producers for the most important JSF artifacts (V)
The list of JSF 2.3 artifacts that can be currently injecte...
Converters, validators and behaviors as
injection targets
Converters, validators and behaviors as injection targets (I)
In JSF 2.2 and prior versions, converters, validators and be...
Converters, validators and behaviors as injection targets (II)
JSF 2.3 example (Complete example here):
@FacesConverter(va...
Converters, validators and behaviors as injection targets (III)
JSF 2.3 example:
@FacesValidator(value = "fooValidator", m...
Converters, validators and behaviors as injection targets (IV)
JSF 2.3 example:
@FacesBehavior(value = "fooBehavior", mana...
Generics support in Converter and
Validator interfaces
Generics support in Converter and Validator interfaces (I)
In JSF 2.2 and prior versions, writing custom converters and va...
Generics support in Converter interface (I)
JSF 2.2 example:
@FacesConverter(value = "UserConverter")
public class UserCon...
Generics support in Converter interface (II)
JSF 2.3 example:
@FacesConverter(value = "UserConverter")
public class UserCo...
Generics support in Validator interface (I)
JSF 2.2 example:
@FacesValidator(value = "UserValidator")
public class UserVal...
Generics support in Validator interface (II)
JSF 2.3 example:
@FacesValidator(value = "UserValidator")
public class UserVa...
Generics for
ExternalContext#getInitParameterMap
Generics for ExternalContext#getInitParameterMap (I)
The JSF 2.2 ExternalContext#getInitParameterMap() returns a raw Map.
...
Generics for ExternalContext#getInitParameterMap (II)
In JSF 2.3, this method was extended to support generics types.
JSF ...
The new PostRenderViewEvent system
event
The new PostRenderViewEvent system event (I)
Starting with JSF 2.0 we had, among others, the PreRenderViewEvent that gets
...
The new PostRenderViewEvent system event (II)
JSF 2.3 example:
<h:body>
<f:event type="preRenderView" listener="#{fooBean....
Indefinitely Facelets cache during
Production stage by default
Indefinitely Facelets cache during Production stage by default (I)
JSF can cache Facelets using the javax.faces.FACELETS_R...
Indefinitely Facelets cache during Production stage by default (II)
In the development stage, the default 2 second value i...
Indefinitely Facelets cache during Production stage by default (III)
For both cases, JSF 2.3 will use the javax.faces.appl...
Align <f:convertDateTime/> to JDK 8
date and time classes
Align <f:convertDateTime/> to JDK 8 date and time classes (I)
In JSF 2.2 and prior versions, the type attribute of the <f:...
Align <f:convertDateTime/> to JDK 8 date and time classes (II)
JSF 2.2 example (type="time"):
<h:inputText value="#{fooBea...
Align <f:convertDateTime/> to JDK 8 date and time classes (III)
JSF 2.2 example (type="both"):
<h:inputText value="#{fooBe...
Align <f:convertDateTime/> to JDK 8 date and time classes (IV)
In JSF 2.3, the <f:convertDateTime/> built-in converter has...
Align <f:convertDateTime/> to JDK 8 date and time classes (V)
JSF 2.3 example (type="offsetTime"):
<h:inputText value="#{f...
Align <f:convertDateTime/> to JDK 8 date and time classes (VI)
JSF 2.3 example (type="localDate"):
<h:inputText value="#{f...
Align <f:convertDateTime/> to JDK 8 date and time classes (VII)
If the converter type attribute value is date, time or bot...
Class level bean validation
Class level bean validation (I)
JSF has support for bean validation since version 2.0.
However, this support is somehow li...
Class level bean validation (II)
For those using OmniFaces, they can validate a given bean at the class level by
using the...
Class level bean validation (III)
To use the new JSF 2.3 <f:validateWholeBean> tag, it needs to be explicitly
enabled in w...
Class level bean validation (IV)
JSF 2.3 example:
<h:form>
Name: <h:inputText value="#{contactBean.name}">
<f:validateBean...
Class level bean validation (V)
JSF 2.3 example (ContactValidator class implementation):
public class ContactValidator imp...
Class level bean validation (VI)
JSF 2.3 example (ValidContact class implementation):
@Constraint(validatedBy = {ContactVa...
Class level bean validation (VII)
JSF 2.3 example (ContactBean class implementation – the backing bean):
@Named
@RequestSc...
Class level bean validation (VIII)
JSF 2.3 example (ContactGroup interface implementation):
package validateBean;
public i...
References
• Arjan Tijms' Weblog: What's new in JSF 2.3?
• JSF & OmniFaces Fans: JSF 2.3 Tutorial
• JSF 2.3 examples: JSF-...
Thank you!
Thank you for watching!
If you like it, please share!
You can find me on:
Twitter: @ConstantinMAlin
LinkedIn: h...
Nächste SlideShare
Wird geladen in …5
×

JSF 2.3 new features

3.785 Aufrufe

Veröffentlicht am

Discover & lean JSF 2.3 new features. The presentation covers JSF 2.3-m04 and will be updated after the next milestone release.

Veröffentlicht in: Technologie
0 Kommentare
5 Gefällt mir
Statistik
Notizen
  • Als Erste(r) kommentieren

Keine Downloads
Aufrufe
Aufrufe insgesamt
3.785
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
198
Aktionen
Geteilt
0
Downloads
81
Kommentare
0
Gefällt mir
5
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

JSF 2.3 new features

  1. 1. JSF 2.3 new features By Constantin Alin Latest updated version: JSF 2.3-m04
  2. 2. What we will cover? (I) • JSF support for Iterable interface in UIData and UIRepeat • JSF support for Map interface in UIData and UIRepeat • JSF support for custom types in UIData and UIRepeat • Default CDI producers for the most important JSF artifacts
  3. 3. What we will cover? (II) • Converters, validators and behaviors as injection targets • Generics support in Converter and Validator interfaces • Generics for ExternalContext#getInitParameterMap • The new PostRenderViewEvent system event
  4. 4. What we will cover? (III) • Indefinitely Facelets cache during Production stage by default • Align <f:convertDateTime/> to JDK 8 date and time classes • Class level bean validation
  5. 5. JSF support for Iterable interface in UIData and UIRepeat
  6. 6. JSF support for Iterable interface in UIData (I) Starting with JSF 2.2, UIData supports Iterable directly, which includes any type of Collection. JSF 2.2 example: <h:dataTable value="#{fooBean.fooHashSet}" var="t"> <h:column> #{t.property} </h:column> </h:dataTable> Read more here
  7. 7. JSF support for Iterable interface in UIRepeat (I) In JSF 2.2 and prior versions, UIRepeat does not support Iterable directly. Workarounds: 1. Using JSF 2.2 API (if EL 2.2 is available): <ui:repeat value="#{fooBean.fooHashSet.toArray()}" var="t"> <h:column> #{t.property} </h:column> </ui:repeat>
  8. 8. JSF support for Iterable interface in UIRepeat (II) 2. Using OmniFaces API, setToList() (if EL 2.2 is not available): <ui:repeat value="#{of:setToList(fooBean.fooHashSet)}" var="t"> <h:column> #{t.property} </h:column> </ui:repeat>
  9. 9. JSF support for Iterable interface in UIRepeat (III) Starting with JSF 2.3, UIRepeat will support Iterable directly. JSF 2.3 example: <ui:repeat value="#{fooBean.fooHashSet}" var="t"> <h:column> #{t.property} </h:column> </ui:repeat> Read more here
  10. 10. JSF support for Map interface in UIData and UIRepeat
  11. 11. JSF support for Map interface in UIData (I) In JSF 2.2 and prior versions, UIData does not support Map directly. Workarounds: 1. Using JSF 2.2 API: <h:dataTable value="#{fooBean.fooMap.entrySet()}" var="t"> <h:column> #{t.key} </h:column> <h:column> #{t.value} </h:column> </h:dataTable>
  12. 12. JSF support for Map interface in UIData (II) 2. Using JSF 2.2 API (if EL 2.2 is available): <h:dataTable value="#{fooBean.fooMap.entrySet().toArray()}" var="t"> <h:column> #{t.key} </h:column> <h:column> #{t.value} </h:column> </h:dataTable>
  13. 13. JSF support for Map interface in UIData (III) Starting with JSF 2.3, UIData will support Map directly. JSF 2.3 example: <h:dataTable value="#{fooBean.fooMap}" var="t"> <h:column> #{t.key} </h:column> <h:column> #{t.value} </h:column> </h:dataTable> Complete example here Read more here
  14. 14. JSF support for Map interface in UIRepeat (I) In JSF 2.2 and prior versions, UIRepeat does not support Map directly. Workarounds: 1. Using JSF 2.2 API (if EL 2.2 is available): <ui:repeat value="#{fooBean.fooMap.entrySet().toArray()}" var="t"> <h:outputText value="#{t.key} - #{t.value}"/> </ui:repeat>
  15. 15. JSF support for Map interface in UIRepeat (II) 2. Using OmniFaces API, mapToList() (if EL 2.2 is not available): <ui:repeat value="#{of:mapToList(fooBean.fooMap)}" var="t"> <h:outputText value="#{t.key} - #{t.value}"/> </ui:repeat>
  16. 16. JSF support for Map interface in UIRepeat (III) Starting with JSF 2.3, UIRepeat will support Map directly. JSF 2.3 example: <ui:repeat value="#{fooBean.fooMap}" var="t"> <h:outputText value="#{t.key} - #{t.value}"/> </ui:repeat> Complete example here Read more here
  17. 17. JSF support for custom types in UIData and UIRepeat
  18. 18. JSF support for custom types in UIData and UIRepeat (I) Starting with JSF 2.3, custom DataModel wrappers can be registered using the @FacesDataModel annotation. This feature give us support for custom data types in UIData and UIRepeat. To achieve this, two steps are required: • create a DataModel wrapper for a specific type and register it using the @FacesDataModel annotation • designate the type this wrapper is able to handle via forClass attribute
  19. 19. JSF support for custom types in UIData and UIRepeat (II) JSF 2.3 example: // Wrapper model @FacesDataModel(forClass = FooCollection.class) public class FooCollectionModel<E> extends DataModel<E> { @Override public E getRowData() { // access FooCollection here } @Override public void setWrappedData(Object fooCollection) { // likely just store fooCollection } // Other methods omitted for brevity }
  20. 20. JSF support for custom types in UIData and UIRepeat (III) JSF 2.3 example (continued): // Use collection in your beans @Named public class FooBean { public FooCollection<Foo> getFoo() { // return fooCollection } } Custom DataModel wrappers cannot (yet) override any of the build-in types.
  21. 21. JSF support for custom types in UIData and UIRepeat (IV) JSF 2.3 example (continued): // Use collection in <h:dataTable/> or <ui:repeat/> (works out of the box) <h:dataTable value="#{fooBean.foo}" var="t"> <h:column> #{t} - #{t.property} </h:column> </h:dataTable> Complete example here Read more here
  22. 22. Default CDI producers for the most important JSF artifacts
  23. 23. Default CDI producers for the most important JSF artifacts (I) Prior versions of JSF and JSF 2.2 uses static entry methods and chaining to access JSF artifacts like FacesContext, ExternalContext, ServletContext, etc. JSF 2.2 examples: ExternalContext externalContext = FacesContext.getCurrentInstance() .getExternalContext(); ServletContext servletContext = (ServletContext) FacesContext .getCurrentInstance().getExternalContext().getContext();
  24. 24. Default CDI producers for the most important JSF artifacts (II) This approach has at least two disadvantages: • the code becomes verbose and hard to read • it is even harder to intuit how an artifact can be obtained
  25. 25. Default CDI producers for the most important JSF artifacts (III) More JSF 2.2 examples: Map<String, String> requestMap = FacesContext.getCurrentInstance() .getExternalContext().getRequestParameterMap(); Map<String, Object> viewMap = FacesContext.getCurrentInstance() .getViewRoot().getViewMap();
  26. 26. Default CDI producers for the most important JSF artifacts (IV) The solution? Provide default producers for the most important JSF artifacts and use the Java EE approach to inject them when needed! JSF 2.3 examples: @Inject private ExternalContext externalContext; @Inject private ServletContext servletContext;
  27. 27. Default CDI producers for the most important JSF artifacts (V) The list of JSF 2.3 artifacts that can be currently injected is available here. More JSF 2.3 examples: @Inject @RequestMap private Map<String, Object> requestMap; @Inject @ViewMap private Map<String, Object> viewMap; Complete example here Read more here
  28. 28. Converters, validators and behaviors as injection targets
  29. 29. Converters, validators and behaviors as injection targets (I) In JSF 2.2 and prior versions, converters, validators and behaviors are not injection targets. Starting with JSF 2.3, we can use the CDI injection mechanism in JSF artifacts annotated with @FacesConverter, @FacesValidator and @FacesBehavior. You will also need to use the new managed attribute on the corresponding annotation and set its value to true. Read more here
  30. 30. Converters, validators and behaviors as injection targets (II) JSF 2.3 example (Complete example here): @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 } }
  31. 31. Converters, validators and behaviors as injection targets (III) JSF 2.3 example: @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 } } Complete example here
  32. 32. Converters, validators and behaviors as injection targets (IV) JSF 2.3 example: @FacesBehavior(value = "fooBehavior", managed = true) public class FooBehavior extends ClientBehaviorBase { @Inject // artifact @Override public String getScript(ClientBehaviorContext behaviorContext) { // use the injected artifact } } Example here
  33. 33. Generics support in Converter and Validator interfaces
  34. 34. Generics support in Converter and Validator interfaces (I) In JSF 2.2 and prior versions, writing custom converters and validators also involves casting Object values to a custom type, since the Converter and Validator interfaces use an Object as their input value. Starting with JSF 2.3, both Converter and Validator have been parameterized and implementations can define the exact input type. Read more here
  35. 35. Generics support in Converter interface (I) JSF 2.2 example: @FacesConverter(value = "UserConverter") public class UserConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { ... } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { ... } }
  36. 36. Generics support in Converter interface (II) JSF 2.3 example: @FacesConverter(value = "UserConverter") public class UserConverter implements Converter<User> { @Override public User getAsObject(FacesContext context, UIComponent component, String value) { ... } @Override public String getAsString(FacesContext context, UIComponent component, User value) { // no need to check value type and cast it ... } } Complete example here
  37. 37. Generics support in Validator interface (I) JSF 2.2 example: @FacesValidator(value = "UserValidator") public class UserValidator implements Validator { @Override public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException { ... } }
  38. 38. Generics support in Validator interface (II) JSF 2.3 example: @FacesValidator(value = "UserValidator") public class UserValidator implements Validator<User> { @Override public void validate(FacesContext fc, UIComponent uic, User o) throws ValidatorException { // no need to check value type and cast it ... } } Complete example here
  39. 39. Generics for ExternalContext#getInitParameterMap
  40. 40. Generics for ExternalContext#getInitParameterMap (I) The JSF 2.2 ExternalContext#getInitParameterMap() returns a raw Map. JSF 2.2 example: Map initParamMap = FacesContext.getCurrentInstance() .getExternalContext().getInitParameterMap();
  41. 41. Generics for ExternalContext#getInitParameterMap (II) In JSF 2.3, this method was extended to support generics types. JSF 2.3 example: Map<String, String> initParamMap = FacesContext.getCurrentInstance() .getExternalContext().getInitParameterMap(); Read more here
  42. 42. The new PostRenderViewEvent system event
  43. 43. The new PostRenderViewEvent system event (I) Starting with JSF 2.0 we had, among others, the PreRenderViewEvent that gets invoked on every HTTP request (including AJAX), right before a view is rendered. Starting with JSF 2.3, a PostRenderViewEvent is available, that gets invoked right after the view is rendered.
  44. 44. The new PostRenderViewEvent system event (II) JSF 2.3 example: <h:body> <f:event type="preRenderView" listener="#{fooBean.preRenderAction()}" /> <f:event type="postRenderView" listener="#{fooBean.postRenderAction()}" /> </h:body> Complete example here Read more here
  45. 45. Indefinitely Facelets cache during Production stage by default
  46. 46. Indefinitely Facelets cache during Production stage by default (I) JSF can cache Facelets using the javax.faces.FACELETS_REFRESH_PERIOD context parameter in web.xml, which by default, is set to 2 seconds. There are also two special values: • -1, cache indefinitely • 0, no caching at all
  47. 47. Indefinitely Facelets cache during Production stage by default (II) In the development stage, the default 2 second value is quite handy, but when the project reaches the production stage, the time for periodic checks of Facelets views changes should be bigger. This value can be changed as follows: <context-param> <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name> <param-value>60</param-value> <!-- Cache for 60 seconds --> </context-param>
  48. 48. Indefinitely Facelets cache during Production stage by default (III) For both cases, JSF 2.3 will use the javax.faces.application.ProjectStage context parameter and when the project stage is set to Production, the Facelets refresh period is set to -1. If you explicitly add the javax.faces.FACELETS_REFRESH_PERIOD context parameter in web.xml, changing to Production stage will not override it. Read more here
  49. 49. Align <f:convertDateTime/> to JDK 8 date and time classes
  50. 50. Align <f:convertDateTime/> to JDK 8 date and time classes (I) In JSF 2.2 and prior versions, the type attribute of the <f:convertDateTime/> built-in converter only supported the values date, time and both. JSF 2.2 example (type="date"): <h:inputText value="#{fooBean.date}"> <f:convertDateTime pattern="dd-MM-yyyy" /> </h:inputText> <h:outputText value="#{fooBean.date}"> <f:convertDateTime dateStyle="short" /> </h:outputText> Complete example here
  51. 51. Align <f:convertDateTime/> to JDK 8 date and time classes (II) JSF 2.2 example (type="time"): <h:inputText value="#{fooBean.date}"> <f:convertDateTime type="time" pattern="h:mm" /> </h:inputText> <h:outputText value="#{fooBean.date}"> <f:convertDateTime type="time" timeStyle="short" /> </h:outputText> Complete example here
  52. 52. Align <f:convertDateTime/> to JDK 8 date and time classes (III) JSF 2.2 example (type="both"): <h:inputText value="#{fooBean.date}"> <f:convertDateTime type="both" pattern="dd-MM-yyyy, h:mm" /> </h:inputText> <h:outputText value="#{fooBean.date}"> <f:convertDateTime type="both" dateStyle="short" timeStyle="short" /> </h:outputText> Complete example here
  53. 53. Align <f:convertDateTime/> to JDK 8 date and time classes (IV) In JSF 2.3, the <f:convertDateTime/> built-in converter has been updated in order to support the following new types for the type attribute: • offsetTime • offsetDateTime • zonedDateTime • localDate • localTime • localDateTime
  54. 54. Align <f:convertDateTime/> to JDK 8 date and time classes (V) JSF 2.3 example (type="offsetTime"): <h:inputText value="#{fooBean.offsetTime}"> <f:convertDateTime type="offsetTime" pattern="h:mm" /> </h:inputText> <h:outputText value="#{fooBean.offsetTime}"> <f:convertDateTime type="offsetTime" pattern="h:mm a" /> </h:outputText> Complete example here
  55. 55. Align <f:convertDateTime/> to JDK 8 date and time classes (VI) JSF 2.3 example (type="localDate"): <h:inputText value="#{fooBean.localDate}"> <f:convertDateTime type="localDate" pattern="dd-MM-yyyy" /> </h:inputText> <h:outputText value="#{fooBean.localDate}"> <f:convertDateTime type="localDate" dateStyle="short" /> </h:outputText> Complete example here
  56. 56. Align <f:convertDateTime/> to JDK 8 date and time classes (VII) If the converter type attribute value is date, time or both, JSF (2.2 and 2.3) uses the java.text.SimpleDateFormat class for formatting. If the converter type attribute value is localDate, localTime, localDateTime, offsetTime, offsetDateTime or zonedDateTime, JSF 2.3 uses the java.time.format.DateTimeFormatter class for formatting. Read more here
  57. 57. Class level bean validation
  58. 58. Class level bean validation (I) JSF has support for bean validation since version 2.0. However, this support is somehow limited and we can only validate individual properties bound to individual components (only field constrains). Other constrains defined in the Java API for JavaBean Validation, like class or method level validation are not supported by default.
  59. 59. Class level bean validation (II) For those using OmniFaces, they can validate a given bean at the class level by using the <o:validateBean> tag, which makes a copy of the bean, stores the new values in that bean, and then validate the result. JSF 2.3 follows <o:validateBean/> approach and introduces a new tag, named <f:validateWholeBean/>. As its name suggest, this tag can validate a given bean at the class level.
  60. 60. Class level bean validation (III) To use the new JSF 2.3 <f:validateWholeBean> tag, it needs to be explicitly enabled in web.xml, or else using it will do nothing: <context-param> <param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name> <param-value>true</param-value> </context-param> •The special default group (javax.validation.groups.Default)
  61. 61. Class level bean validation (IV) JSF 2.3 example: <h:form> Name: <h:inputText value="#{contactBean.name}"> <f:validateBean validationGroups="validateBean.ContactGroup"/> </h:inputText> E-mail: <h:inputText value="#{contactBean.email}"> <f:validateBean validationGroups="validateBean.ContactGroup"/> </h:inputText> <h:commandButton value="Contact Admin" action="contact_admin"/> <f:validateWholeBean value="#{contactBean}" validationGroups="validateBean.ContactGroup"/> </h:form> •The special default group (javax.validation.groups.Default)
  62. 62. Class level bean validation (V) JSF 2.3 example (ContactValidator class implementation): public class ContactValidator implements ConstraintValidator<ValidContact, ContactBean> { @Override public void initialize(ValidContact constraintAnnotation) { // NOOP } @Override public boolean isValid(ContactBean value, ConstraintValidatorContext context) { return value.getEmail().startsWith(value.getName()); } } •The special default group (javax.validation.groups.Default)
  63. 63. Class level bean validation (VI) JSF 2.3 example (ValidContact class implementation): @Constraint(validatedBy = {ContactValidator.class}) @Documented @Target(TYPE) @Retention(RUNTIME) public @interface ValidContact { String message() default "Invalid contacts (e-mail should start with name) !"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } •The special default group (javax.validation.groups.Default)
  64. 64. Class level bean validation (VII) JSF 2.3 example (ContactBean class implementation – the backing bean): @Named @RequestScoped @ValidContact(groups = validateBean.ContactGroup.class) public class ContactBean implements Serializable, Cloneable { private static final long serialVersionUID = 1L; @Size(min = 3, max = 20, message = "Please enter a valid name (between 3- 20 characters)!", groups = validateBean.ContactGroup.class) private String name; @Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+", message = "Please enter a valid formated e-mail !", groups = validateBean.ContactGroup.class) private String email; // Getters and setters // clone() } •The special default group (javax.validation.groups.Default)
  65. 65. Class level bean validation (VIII) JSF 2.3 example (ContactGroup interface implementation): package validateBean; public interface ContactGroup { // NOPE } Complete example here Read more here •The special default group (javax.validation.groups.Default)
  66. 66. References • Arjan Tijms' Weblog: What's new in JSF 2.3? • JSF & OmniFaces Fans: JSF 2.3 Tutorial • JSF 2.3 examples: JSF-2.3 repository
  67. 67. Thank you! Thank you for watching! If you like it, please share! You can find me on: Twitter: @ConstantinMAlin LinkedIn: here

×