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
Newsletter
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

  • How an AI Software Factory Works: From Developer Intent to Production
  • Avoiding AI Debt as a Team
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Take Advantage of New Generic Parameters in JSF 2.3

How to use the new JSF converter and validator parameters.

By 
Anghel Leonard
DZone Core CORE ·
Nov. 29, 15 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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