DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Advantages of Independent Testing in Comparison with Traditional QA Methods
  • Cross-Platform vs. Native Mobile Development: Navigating the Trade-Offs
  • Advantages and Disadvantages of Test Automation
  • Advantages and Disadvantages of Data Replication in Distributed Databases

Trending

  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Multithreading in Modern Java: Advanced Benefits and Best Practices
  • Why Developers Must Be Part of the Customer Validation Process
  • Anti-Patterns of Microservices Architecture From Real Production Experience

Take Advantage of New Generic Parameters in JSF 2.3

How to use the new JSF converter and validator parameters.

By 
Anghel Leonard user avatar
Anghel Leonard
DZone Core CORE ·
Nov. 29, 15 · Opinion
Likes (8)
Comment
Save
Tweet
Share
13.0K Views

Join the DZone community and get the full member experience.

Join For Free

As you probably know, in JSF 2.2 we can write a custom converter by extending the Converter interface, and a custom validator by extending the Validatorinterface. The methods defined in these interfaces works with the Object class, as you can see in the below skeletons:

JSF 2.2 Custom Converter Skeleton

@FacesConverter(value = "fooConverter")
public class FooConverter implements Converter {
 @Override
 public Object getAsObject(FacesContext context, UIComponent component, String value) {
  ...
 }

 @Override
 public String getAsString(FacesContext context, UIComponent component, Object value) {
  // check value type and cast it
  ...
 }
}

JSF 2.2 Custom Validator Skeleton

@FacesValidator(value = "fooValidator")
public class FooValidator implements Validator {
 @Override
 public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
  // check value type and cast it
  ...
 }
}

Starting with JSF 2.3 Converter and Validator have now been parameterized and implementations can concisely define the exact input type.

// Mojarra 2.3.0-m04 Converter snippet of source code
public interface Converter<T> {
 T getAsObject(FacesContext context, UIComponent component, String value);
 String getAsString(FacesContext context, UIComponent component, T value);
}

// Mojarra 2.3.0-m04 Validator snippet of source code
public interface Validator<T> {
 void validate(FacesContext context, UIComponent component, T value);
}

For example, let's suppose that we have the following simple class:

public class User implements Serializable {
 private String name;
 private String email;

 public User(String name, String email) {
  this.name = name;
  this.email = email;       
 }

 // getters and setters  
 // equal and hash code
 ...
}

Now, we can convert/validate a string against this class by indicating the User type in our custom converter/validator:

JSF 2.3 Custom Converter With Defined Type 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
  ...
 }
}

JSF 2.3 Custom Validator With Defined Type 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
  ...
 }
}

Note In JSF 2.3, you can still write custom converters/validators  as in JSF 2.2. If you don't want to take advantage of the new generic parameters, you can use the classical approach.

A complete and functional example can be found here. I've tested under Payara 4.1.

Advantage (cryptography)

Published at DZone with permission of Anghel Leonard. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advantages of Independent Testing in Comparison with Traditional QA Methods
  • Cross-Platform vs. Native Mobile Development: Navigating the Trade-Offs
  • Advantages and Disadvantages of Test Automation
  • Advantages and Disadvantages of Data Replication in Distributed Databases

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook